gpt4 book ai didi

java - 如何在 Windows 10 的 Java Swing JButton 中获得白色背景

转载 作者:行者123 更新时间:2023-11-29 04:09:06 25 4
gpt4 key购买 nike

我想在 Windows 10 中为 Java Swing 应用程序获取一个白色背景的 JButton。

我试过以下方法。我包括一个带有两个框架的功能性基本示例,都有一个面板和一个按钮,第一个不包括任何自定义。按钮和面板具有浅蓝色/灰色(实际感知颜色取决于屏幕)。对于第二帧,我尝试明确绘制白色面板和按钮。

package com.example.test2;

import java.awt.Color;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test2 {

public Test2() {

// Frame 1, standard background
JFrame frame1 = new JFrame();
JPanel panel1 = new JPanel();
JButton button1 = new JButton("Button 1");

// Frame 2, white background
JFrame frame2 = new JFrame();
JPanel panel2 = new JPanel();
JButton button2 = new JButton("Button 2");
panel2.setBackground(Color.WHITE);
button2.setBackground(Color.WHITE);

// Add Components for Frame1 and show it
frame1.add(panel1);
panel1.add(button1);
frame1.setSize(300, 200);
frame1.setVisible(true);

// Add Components for Frame2 and show it
frame2.add(panel2);
panel2.add(button2);
frame2.setSize(300, 200);
frame2.setLocation(250,150);
frame2.setVisible(true);

}

public static void main(String[] args) {

try {
UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
} catch (ClassNotFoundException ex) {
Logger.getLogger(Test2.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(Test2.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(Test2.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(Test2.class.getName()).log(Level.SEVERE, null, ex);
}
Test2 t = new Test2();
}
}

这是两帧的结果:

Frames 1 and 2 - White background

如何让button2有白色背景?

请注意,我已经尝试过此解决方案 ( Changing the background color of JButton always shows as grey ),但它无法使用 Windows 10 并设置 L&F UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );

最佳答案

setContentAreaFilled(false) 在这里施展魔法。

在 Windows 10 上看到的。

enter image description here

此代码使用青色“中间”面板(更容易区分 PLAF 的默认浅灰色)以允许将按钮设置为与容器不同的颜色。如果您确实希望按钮与显示它的父容器颜色相同,则可以省略该中间面板。

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class Test2 {

public Test2() {
// Frame, orange background
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel();
JButton button = new JButton("Button 2");
button.setContentAreaFilled(false);
panel.setBackground(Color.ORANGE);

// Add Components
frame.add(panel);
panel.setBorder(new EmptyBorder(10,100,10,100));
JPanel buttonColorPanel = new JPanel(new GridLayout());
// adjust color as needed
buttonColorPanel.setBackground(Color.CYAN);
panel.add(buttonColorPanel);
buttonColorPanel.add(button);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException |
IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
Test2 t = new Test2();
}
}

我做了一些其他的代码更改,这些更改只是很好的做法,但与最终效果无关。 Swing GUI 也应该在 EDT 上启动。 BNI.

关于java - 如何在 Windows 10 的 Java Swing JButton 中获得白色背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56192748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com