gpt4 book ai didi

java - 更改 ActionPerformed 上的 JButton 颜色

转载 作者:太空宇宙 更新时间:2023-11-04 06:50:31 24 4
gpt4 key购买 nike

尽管我努力改变按钮的颜色,并到处阅读如何做到这一点,但我的代码仍然没有产生预期的效果。如何更改 JButton 的颜色什么时候点击的?

public void generateButtons
{
//Global field
firstBoard = new JButton[9][9];

for(int x = 0; x < firstBoard.length; x++)
{
for(int y = 0; y < firstBoard[0].length; y++)
{
firstBoard[x][y] = new JButton();
firstBoard[x][y].setActionCommand("0|" + (x + (firstBoard.length * y)));
firstBoard[x][y].addActionListener(this);
//firstBoardPanel.add(firstBoard[x][y]);
}
}
}

@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() instanceof JButton )
{
System.out.println(parseActionCommand(((JButton)e.getSource()).getActionCommand()));
((JButton)e.getSource()).setBackground(Color.BLUE);
((JButton)e.getSource()).setContentAreaFilled(false);
((JButton)e.getSource()).setOpaque(true);
}
}

最佳答案

这不是一个答案,而是一个扩展测试用例,用于测试 Mac OS 外观和感觉与 Nimbus 之间的差异,后者在 Windows 操作系统上运行时有效

看来,这成了一个答案......

尝试从以下开始...

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
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.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;

public class ChangeButton {

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

public ChangeButton() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel implements ActionListener {

private final JButton[][] firstBoard;

public TestPane() {
setLayout(new GridLayout(9, 9));
firstBoard = new JButton[9][9];

for (int x = 0; x < firstBoard.length; x++) {
for (int y = 0; y < firstBoard[0].length; y++) {
firstBoard[x][y] = new JButton();
firstBoard[x][y].setActionCommand("0|" + (x + (firstBoard.length * y)));
firstBoard[x][y].addActionListener(this);
add(firstBoard[x][y]);
}
}
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof JButton) {
System.out.println((((JButton) e.getSource()).getActionCommand()));
((JButton) e.getSource()).setBackground(Color.BLUE);
((JButton) e.getSource()).setContentAreaFilled(false);
((JButton) e.getSource()).setOpaque(true);
}
}
}

}

然后将 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 替换为...

for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}

然后再试一次

关于java - 更改 ActionPerformed 上的 JButton 颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23355208/

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