【问题标题】:Changing the background color of a panel inside a frame更改框架内面板的背景颜色
【发布时间】:2015-01-26 08:24:07
【问题描述】:

就像标题所说的那样,我正在尝试在框架内设置面板的背景颜色,以便它们都有不同的颜色。到目前为止我尝试的是单独或同时使用setBackground 方法,我得到的结果总是只显示一种颜色,这很奇怪,因为内框不应该能够更改外框的设置对吧?

代码示例:

public class frameStuff{

    private JFrame frame;
    private frame1 in_frame;

    @SuppressWarnings("serial")
    class frame1 extends JPanel  {
        frame1(){
            super();
        }

        public void paint(Graphics g){

        }
    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    frameStuff window = new frameStuff();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public frameStuff() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 500, 350);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
//      frame.getContentPane().setBackground(Color.GRAY);/*if this line wasn't a comment then everything would be grey instead of white as it's now.

        in_frame = new frame1();
        in_frame.setBounds(100, 100, 350, 220);
        in_frame.setBackground(Color.BLUE);
        in_frame.setVisible(true);//this doesn't seem to matter whatever the case
        frame.getContentPane().add(in_frame);

    }
}

【问题讨论】:

  • 删除 public void paint(Graphics g){ } .. 不要重写一个工作方法只是让它什么都不做!
  • 请注意,class frame1 extends JPanel { 不是框架,不应该这样称呼!
  • “这就是我想要一个服装JPanel类开始的原因..” 如果没有必要显示问题,那就是多余的。否则,它现在正在引起问题。鉴于这是 JComponentJPanel 扩展 JComponent),永远覆盖 paint(..) 是正确的 - 而是覆盖 paintComponent(..) 并立即调用 super.paintComponent(..);
  • frame.getContentPane().setLayout(null); 这一天会出现一两次。 (所以再次)Java GUI 必须在不同的操作系统、屏幕尺寸、屏幕分辨率等上工作。因此,它们不利于像素完美布局。而是使用布局管理器,或 combinations of them 以及 white space 的布局填充和边框。
  • "重写paint(..)永远是不正确的——而是重写paintComponent(..)并立即调用super.paintComponent(..);"似乎添加了这一行“super.paint(g);”到paint方法解决了这里的问题,你还建议按照你说的去做吗?这种方式更快吗?顺便说一句,您可以将对此的回复作为答案发布,我会接受它:p

标签: java swing jframe jpanel paint


【解决方案1】:
class frame1 extends JPanel  {
    frame1(){
        super();
    }

    public void paint(Graphics g){
    }
}

应该是:

// Should be called a Panel (since it is one) and use correct case
class Panel1 extends JPanel  {
    Panel1(){
        super();
    }

    // The method to override for custom painting in ANY JComponent
    @Override // handy to check we got the method signature right
    public void paintComponent(Graphics g){
        // paints the background, borders etc.
        super.paintComponent(g);
        // all custom drawing AFTER this line..
    }
}

好像在paint方法中加入这一行super.paint(g);就可以解决这里的问题,你还建议按照你说的那样做吗?

是的。

这样更快吗?

是的。如果只是因为您仍然必须更改代码中的方法并将项目重新编译/构建成可以可靠渲染的东西,而不会出现奇怪的工件。现在,这对你来说可能是正确的,但这纯粹是偶然的。

【讨论】:

    猜你喜欢
    • 2021-12-13
    • 2017-11-30
    • 2018-09-23
    • 2015-09-06
    • 1970-01-01
    • 2011-01-30
    • 1970-01-01
    • 2012-12-07
    • 2020-06-02
    相关资源
    最近更新 更多