【问题标题】:How change background color of JFrame [duplicate]如何更改JFrame的背景颜色[重复]
【发布时间】:2014-04-25 10:13:12
【问题描述】:

我无法更改 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);
                }
            }
        }
    }
}

我尝试了好几种方法,但我不能改变颜色,它总是白色的,我该怎么办?

【问题讨论】:

  • panel.setBackground(Color.BLACK); 应该可以工作。显示您的 MyPanel 类
  • @kleopatra 您链接的问题与JFrame 相关,尽管此问题的标题可能具有误导性,但假设OP 的MyPanel 遵循MyFrame 并扩展JPanel 这个问题似乎是关于改变颜色JPanel 的核心无法正常工作。

标签: java swing jframe


【解决方案1】:

试试这个。绘制一个填充有背景颜色的矩形。

  • 使用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);
            }
        });
    }
}

【讨论】:

  • @kleopatra 你看过链接了吗?
  • @kleopatra 我已经编辑了我的帖子。
  • @kleopatra 非常感谢。现在看起来不错。
  • 谢谢,它有效,为什么我必须使用 SwingUtilities.invokeLater() 或 EventQueue.invokeLater()?为什么我必须使用paintComponent 而不是paint?
  • 只需搜索StackOverflow。你一定会找到答案的。请阅读SwingUtilities.invokeLater
【解决方案2】:

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

 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);

【讨论】:

    【解决方案3】:

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

    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的引用。

    【讨论】:

      【解决方案4】:

      至于您的MyPanel,您似乎没有设置大小,因此 atm 不可见。
      对于你的jFrame,既然你覆盖了paint,为什么不尝试设置g.setBackground(Color.BLACK);。

      【讨论】:

      • 调整大小是 LayoutManager 的专有任务(默认情况下,它是 contentPane 上的一个borderLayout,它调整中心组件的大小以填充容器)。另外,覆盖框架的绘制是错误的方法——自定义绘制应该在组件的 paintComponent 中处理,f.i.我的面板
      猜你喜欢
      • 2015-11-16
      • 2016-04-07
      • 2020-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多