如何更改JFrame的背景颜色

时间:2014-04-25 10:13:13

标签: java swing jframe

我无法更改JFrame或JFrame中JPanel的背景颜色,这是我的代码:

public class MyFrame extends JFrame {

    public MyFrame(){
        setSize(600,600);
        setResizable(false);
        panel = new MyPanel();
        panel.setBackground(Color.BLACK);
        //getContentPane().setBackground(Color.BLACK); doesn't work
        getContentPane().add(panel);
        addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent ev){
                System.exit(-1);
            }
            public void windowClosed(WindowEvent ev){
                System.exit(-1);
            }
        });
    }

 public void paint(Graphics g){
        super.paint(g);
        g.clearRect(0,0,600,600);
        synchronized (this){
            if (positions!=null){
                for (int i=0; i<positions.length; i++){
                    P2d p = positions[i];
                    //int x0 = (int)(180+p.x*180);
                    //int y0 = (int)(180-p.y*180);
                    g.drawOval((int)p.x,(int)p.y,5,5);
                }
            }
        }
    }
}

我曾尝试过多种方式,但我无法改变颜色,它总是白色的,我该怎么做?

4 个答案:

答案 0 :(得分:0)

不确定这是否适合您,但请尝试

 JPanel panel = new JPanel();
 panel.setBackground(Color.BLACK);

或者您可以尝试在JFrame中设置颜色并将颜色从面板中取出

 JFrame frame = new JFrame();
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setTitle("MyFirstFrame");
 frame.setBackground(Color.BLACK);
 frame.setLocation(0,0);
 frame.setSize(150,150);

答案 1 :(得分:0)

试试这个。画一个填充了背景颜色的矩形。

  • 使用SwingUtilities.invokeLater()EventQueue.invokeLater()
  • 使用paintComponent()方法进行自定义绘画
  • 不要直接在JFrame上画画。
  • 使用setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)关闭JFrame

示例代码:

public class MyFrame extends JFrame {

    public MyFrame() {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                setSize(new Dimension(600, 600));
                setResizable(false);
                JPanel panel = new JPanel() {
                    @Override
                    public void paintComponent(Graphics g) {
                        super.paintComponent(g);

                        g.clearRect(0, 0, 600, 600);
                        Color prevColor = g.getColor();

                        g.setColor(Color.BLUE); // background color
                        g.fillRect(0, 0, 600, 600); // fill a rectangle with background color
                        g.setColor(prevColor);

                        // your custom painting
                        ...
                    }
                };
                getContentPane().add(panel);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                setVisible(true);
            }
        });
    }
}

答案 2 :(得分:0)

查看此答案以更改背景颜色

import javax.swing.JFrame;
import java.awt.Color;
import java.awt.EventQueue;

public class ColoredFrame {

  public static void main( String[] args ) {
    EventQueue.invokeLater( new Runnable() {
      @Override
      public void run() {
        JFrame frame = new JFrame( "TestFrame" );
        frame.getContentPane().setBackground( Color.PINK );
        //frame contains nothing, so set size
        frame.setSize( 200, 200 );
        frame.setVisible( true );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      }
    } );
  }
}

Robin's answer的参考。

答案 3 :(得分:-1)

至于您的MyPanel,您似乎没有设置尺寸,因此无法看到它。
对于您的jFrame,因为您覆盖了绘画,为什么不尝试设置g.setBackground(Color.BLACK);