2

有人可以帮助我解释为什么没有设置框架的背景颜色。是否可以在构造函数中设置背景颜色Paint()或必须在JColor构造函数中完成

我应该为 BG 颜色执行以下操作-

编写一个 GUI 应用程序,显示您选择的单一JButton背景颜色和任何背景颜色。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

/**
 *
 * @author Curtis
 */
public class JColor extends JFrame implements ActionListener
{
Font myFont = new Font("Playbill", Font.PLAIN, 28);
JButton myButton = new JButton("Click Me!");
Color bgColor = new Color(255, 97, 3);
Color txtColor = new Color(0, 0, 205);
String firstName = "Curtis";
String lastName = "Sizemore";

public JColor()
        {
            super("String Painting Fun");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLayout (new BorderLayout());
            add(myButton, BorderLayout.SOUTH);
            setDefaultLookAndFeelDecorated(true);
            setBackground(Color.BLUE);
        }

@Override
public void paint(Graphics e)
{
    super.paint(e);
}

public static void main(String[] args) 
{
    final int TALL = 200;
    final int WIDE = 250;
    JColor frame = new JColor();
    frame.setSize(WIDE, TALL);

    frame.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) 
{
    throw new UnsupportedOperationException("Not supported yet.");
}
}
4

1 回答 1

4

尝试在ContentPane实例上调用它(更多信息在这里

public JColor() {
    super("String Painting Fun");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new BorderLayout());
    add(myButton, BorderLayout.SOUTH);
    setDefaultLookAndFeelDecorated(true);
    getContentPane().setBackground(Color.BLUE);//<- update
}
于 2012-07-03T18:40:23.893 回答