为什么我无法更改 JButton(禁用)文本颜色?

0 java swing

我开始编写 Java 程序。这是我的第一个窗口应用程序。我做了一个简单的井字棋游戏,我希望“o”按钮字体颜色为不同的颜色。但这不起作用。我可以更改背景颜色,但不能更改字体,为什么?

\n
package moje;\n\nimport java.awt.Color;\nimport java.awt.Font;\nimport java.awt.GridLayout;\nimport java.awt.event.ActionEvent;\nimport java.awt.event.ActionListener;\nimport java.awt.event.MouseEvent;\nimport java.awt.event.MouseListener;\nimport java.awt.print.PrinterJob;\n\nimport javax.swing.JButton;\nimport javax.swing.JFrame;\nimport javax.swing.JLayeredPane;\nimport javax.swing.JTextField;\n\npublic class Kolko_i_krzyzyk extends JFrame implements ActionListener {\n    \n    static JTextField tekst;\n    static JLayeredPane ekran = new JLayeredPane();\n    static JButton button = new JButton();\n    static int licznik=0;\n\n    \n    public Kolko_i_krzyzyk () {\n        super("K\xc3\xb3\xc5\x82ko i krzy\xc5\xbcyk");\n        ekran = new JLayeredPane();\n        setVisible(true);\n        setSize(800, 800);\n        setDefaultCloseOperation(EXIT_ON_CLOSE);\n        setResizable(false);\n        \n        //Siatka podzielona 3 na 3\n        setLayout(new GridLayout(3,3));\n        \n        //Tworzenie 9 przycisk\xc3\xb3w\n        for(int i = 1; i<=9; i++) {\n        JButton button = new JButton();\n        add(button);\n        button.addActionListener(this);\n        }\n    }\n\n    public static void main(String[] args) {\n        JFrame okno = new Kolko_i_krzyzyk();\n    }\n\n    @Override\n    public void actionPerformed(ActionEvent e) {\n    JButton  button = (JButton) e.getSource();\n    if(licznik%2==0 ) {\n        button.setText("x");\n        button.setFont(new Font ("Arial", Font.BOLD, 90));\n    }\n    else {\n        button.setText("O");\n        button.setForeground(Color.RED);\n        button.setFont(new Font ("Arial", Font.BOLD, 90));\n    }\n    button.setEnabled(false);\n    licznik++;\n    }\n}\n    \n
Run Code Online (Sandbox Code Playgroud)\n

mal*_*ter 5

这里的问题是禁用JButtonvia时的默认行为setEnabled(false)。这将使按钮变灰并忽略您对文本(前景)所做的任何颜色格式。

有几种解决方法可以修改此行为(如类似问题中所示)。

这是一个简短的演示(当然没有最终的游戏逻辑),它改变了JButtonvia的 UI setUI()

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.plaf.metal.MetalButtonUI;

public class Test {

    private int counter = 0;

    public static void main(String[] args) {

        SwingUtilities.invokeLater(() -> new Test().buildGui());
    }

    private void buildGui() {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        frame.add(panel);
        panel.setLayout(new GridLayout(3, 3));

        for (int i = 1; i <= 9; i++) {
            JButton button = new JButton() {
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(150, 150);
                }
            };
            button.setFont(new Font("Arial", Font.BOLD, 90));
            panel.add(button);
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (counter % 2 == 0) {
                        button.setText("X");
                        button.setUI(new MetalButtonUI() {
                            // override the disabled text color for the button UI
                            protected Color getDisabledTextColor() {
                                return Color.BLUE;
                            }
                        });
                    } else {
                        button.setText("O");
                        button.setUI(new MetalButtonUI() {
                            protected Color getDisabledTextColor() {
                                return Color.RED;
                            }
                        });
                    }
                    button.setEnabled(false);
                    counter++;
                }
            });
        }
        frame.pack();
        frame.setVisible(true);
    }

}
Run Code Online (Sandbox Code Playgroud)

结果:

结果

另一种(更简单)的方法是为“X”和“O”构建一些s,然后通过/ImageIcon将它们设置在按钮上。这将为您省去修改按钮 UI 的麻烦。setIcon()setDisabledIcon()