【问题标题】:How I change the color of JFrame?如何更改 JFrame 的颜色?
【发布时间】:2021-10-23 01:41:30
【问题描述】:

我知道如何更改 JFrame 的背景颜色,但是如何更改背景中的颜色,以便您可以在蓝色背景上看到白色或类似的颜色?

感谢您的帮助。 这是我尝试过的代码部分:

public class Malen extends JLabel {
    protected void painComponent(Graphics g) {
        super.paintComponent(g);  
        
        Graphics2D g2d = (Graphics2D) g;
        
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,     RenderingHints.VALUE_ANTIALIAS_ON);
        
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, Variablen.bildschirmbreite, Variablen.bildschirmgröße);
        
        g.setColor(Color.WHITE);
        
        for(int i = 0; i <= 30; i++) {
            g.fillRect(Variablen.bildschirmbreite / 2 - 5, i * 20, 10, 10);                
        }
    }
}

【问题讨论】:

  • 你的意思是,像渐变?
  • 你把paintComponent方法拼错了。
  • @GilbertLeBlanc "你把paintComponent方法拼错了。"这就是为什么当一个方法打算改变现有方法的行为时,代码应该使用@Override标记.如果方法名拼写错误或声明了错误的参数,编译器会发出警告。

标签: java swing user-interface colors


【解决方案1】:
JFrame frame = new JFrame();
JPanel panel = new JPanel();

public Gui() {

    JLabel label = new JLabel("white");
    // sets the label to opaque so that backround visible
    label.setOpaque(true);
    // sets the label to white
    label.setBackground(Color.WHITE);
    label.setBounds(0, 0, 500, 500);

    frame.setTitle("White label on blue backround");
    frame.setSize(800, 600);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    panel.setBackground(new Color(123, 50, 250));
    panel.add(label);
    frame.setContentPane(panel);

    frame.setVisible(true);

}

public static void main(String args[]) {
    new Gui();
}

【讨论】:

  • 写几句话来解释一下这是如何解决问题的。
猜你喜欢
  • 2022-10-03
  • 2014-08-31
  • 2012-12-24
  • 1970-01-01
  • 1970-01-01
  • 2011-01-29
  • 2020-04-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多