【问题标题】:Why can't I change the JButton (disabled) text color?为什么我不能更改 JButton(禁用)文本颜色?
【发布时间】:2021-02-11 13:12:34
【问题描述】:

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

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

【问题讨论】:

    标签: java swing


    【解决方案1】:

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

    有几种解决方法可以修改此行为(如 this similar question 所示)。

    这是一个简短的演示(当然没有最终的游戏逻辑),它通过 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”构建一些ImageIcons,然后通过setIcon()/setDisabledIcon() 在按钮上设置它们。这样可以省去修改按钮 UI 的麻烦。

    【讨论】:

    • 不错的一个!比我预期的更全面。 :)
    • 谢谢 :) 感谢您的反馈。
    猜你喜欢
    • 2015-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-29
    • 1970-01-01
    • 2021-10-26
    相关资源
    最近更新 更多