java swing - 如何改变 window 背景
java swing - how to change window background
我试图制作一个按钮,将 window 背景从默认设置切换为红色。
我没有找到任何预设颜色来匹配默认值,所以我在创建它时尝试从 panel.getBackground
获取它。我在 line 11
有一个错误,我不知道如何检查当前的背景颜色。
JPanel panel = new JPanel();
panel.setBounds(0, 0, 434, 262);
frame.getContentPane().add(panel);
panel.setLayout(null);
panel.setVisible(true);
Color c=panel.getBackground();
JButton btnRed = new JButton("Red");
btnRed.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(panel.getBackground(c));{
panel.setBackground(Color.RED);
}
else{
panel.setBackground(c);
}
}
});
您想比较 Color
,这是一个 Object
,所以 equals()
是这样...还有一个 if
需要一个 boolean
并在找到 ;
时完成,因此:
if(panel.getBackground(c));{
// ↑ not a boolean ↑
// ↑
// ; is wrong
必须
if(panel.getBackground().equals(c)){
if(panel.getBackground(c));{
不正确,它甚至无法编译。应该是if (c.equals(panel.getBackground()) {
注意,语句末尾缺少 ;
。
此外,没有getBackground(Color)
这样的方法,因此,加上else
语句,您的代码基本上无法编译。
不要使用 null
布局,您无法控制 UI 的各个方面,这将改变组件在不同系统上所需的大小或外观设置
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private Color baseColor;
public TestPane() {
baseColor = getBackground();
setLayout(new GridBagLayout());
JButton btn = new JButton("Click");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (baseColor.equals(getBackground())) {
setBackground(Color.RED);
} else {
setBackground(baseColor);
}
}
});
add(btn);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}
检查当前颜色的替代方法可能会在某些 systems/look 上引起一些问题,感觉是使用一个简单的计数器...
public class TestPane extends JPanel {
private int tick;
public TestPane() {
setLayout(new GridBagLayout());
JButton btn = new JButton("Click");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
tick++;
if ((tick % 2) != 0) {
setBackground(Color.RED);
} else {
setBackground(null);
}
}
});
add(btn);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
这将允许您独立于当前颜色翻转状态,这通常更安全
我试图制作一个按钮,将 window 背景从默认设置切换为红色。
我没有找到任何预设颜色来匹配默认值,所以我在创建它时尝试从 panel.getBackground
获取它。我在 line 11
有一个错误,我不知道如何检查当前的背景颜色。
JPanel panel = new JPanel();
panel.setBounds(0, 0, 434, 262);
frame.getContentPane().add(panel);
panel.setLayout(null);
panel.setVisible(true);
Color c=panel.getBackground();
JButton btnRed = new JButton("Red");
btnRed.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(panel.getBackground(c));{
panel.setBackground(Color.RED);
}
else{
panel.setBackground(c);
}
}
});
您想比较 Color
,这是一个 Object
,所以 equals()
是这样...还有一个 if
需要一个 boolean
并在找到 ;
时完成,因此:
if(panel.getBackground(c));{
// ↑ not a boolean ↑
// ↑
// ; is wrong
必须
if(panel.getBackground().equals(c)){
if(panel.getBackground(c));{
不正确,它甚至无法编译。应该是if (c.equals(panel.getBackground()) {
注意,语句末尾缺少 ;
。
此外,没有getBackground(Color)
这样的方法,因此,加上else
语句,您的代码基本上无法编译。
不要使用 null
布局,您无法控制 UI 的各个方面,这将改变组件在不同系统上所需的大小或外观设置
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private Color baseColor;
public TestPane() {
baseColor = getBackground();
setLayout(new GridBagLayout());
JButton btn = new JButton("Click");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (baseColor.equals(getBackground())) {
setBackground(Color.RED);
} else {
setBackground(baseColor);
}
}
});
add(btn);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}
检查当前颜色的替代方法可能会在某些 systems/look 上引起一些问题,感觉是使用一个简单的计数器...
public class TestPane extends JPanel {
private int tick;
public TestPane() {
setLayout(new GridBagLayout());
JButton btn = new JButton("Click");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
tick++;
if ((tick % 2) != 0) {
setBackground(Color.RED);
} else {
setBackground(null);
}
}
});
add(btn);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
这将允许您独立于当前颜色翻转状态,这通常更安全