繁体   English   中英

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

[英]Why can't I change the JButton (disabled) text color?

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

package moje;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.print.PrinterJob;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JTextField;

public class Kolko_i_krzyzyk extends JFrame implements ActionListener {
    
    static JTextField tekst;
    static JLayeredPane ekran = new JLayeredPane();
    static JButton button = new JButton();
    static int licznik=0;

    
    public Kolko_i_krzyzyk () {
        super("Kółko i krzyżyk");
        ekran = new JLayeredPane();
        setVisible(true);
        setSize(800, 800);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        
        //Siatka podzielona 3 na 3
        setLayout(new GridLayout(3,3));
        
        //Tworzenie 9 przycisków
        for(int i = 1; i<=9; i++) {
        JButton button = new JButton();
        add(button);
        button.addActionListener(this);
        }
    }

    public static void main(String[] args) {
        JFrame okno = new Kolko_i_krzyzyk();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
    JButton  button = (JButton) e.getSource();
    if(licznik%2==0 ) {
        button.setText("x");
        button.setFont(new Font ("Arial", Font.BOLD, 90));
    }
    else {
        button.setText("O");
        button.setForeground(Color.RED);
        button.setFont(new Font ("Arial", Font.BOLD, 90));
    }
    button.setEnabled(false);
    licznik++;
    }
}
    

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

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

这是一个简短的演示(当然没有最终的游戏逻辑),它通过setUI()更改JButton的 UI。

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);
    }

}

结果:

结果

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM