简体   繁体   English

无法设置背景色

[英]Can't set background color

For some reason my background just doesn't turn blue. 由于某种原因,我的背景不会变蓝。 Does anyone know how to solve this with keeping everything inside? 有谁知道如何通过保留所有内容来解决此问题?

I've been trying to fix this for ages already, but nothing works apparantly.. 我已经尝试修复此问题已有很长时间了,但是似乎没有任何效果。

public static void window() {
    JFrame frame = new JFrame("Sharp");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(
        new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
    JPanel b = new JPanel();
    JLabel label2 = new JLabel("Hello, World!", JLabel.CENTER);
    label2.setAlignmentY(0);
    label2.setAlignmentX(0);
    label2.setText("<html>Made by Julian</html>");
    JPanel a = new JPanel();
    b.add(label2);
    a.setAlignmentX(Component.CENTER_ALIGNMENT);
    a.setPreferredSize(new Dimension(850, 500));
    a.setMaximumSize(new Dimension(850, 850)); // set max = pref


    JToggleButton tb = new JToggleButton("SHARP Instructions");
    tb.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            JToggleButton btn =  (JToggleButton) e.getSource();


            if(btn.isSelected()) {
                Desktop d = Desktop.getDesktop();
                try {
                    d.browse(new URI("https://pastebin.com/nDdGZ0cJ"));
                } catch (IOException | URISyntaxException e2) {
                    e2.printStackTrace();
                } 
            }
        }
    }); 

    frame.setBackground(Color.BLUE);
    a.add(tb);

   // JPanel b = new JPanel();

    frame.add(a, new GridBagConstraints());
    frame.getContentPane().add(a);
    frame.getContentPane().add(b);
    //frame.getContentPane().add(b);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

Your JPanel a is on top of your JFrame , so just: 您的JPanel a位于JFrame之上,因此:

a.setBackground(Color.BLUE); will fix it. 将修复它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM