禁用Java GUI中的组件,但不使其变灰

时间:2018-07-31 20:57:20

标签: java user-interface jbutton jcomponent

我目前正在研究一个Java项目,该项目允许用户对程序打井字游戏。我想禁用已经单击的正方形。我想知道是否有任何方法可以禁用组件(JButton)而不会使其变灰,就像component.setEnabled(false)一样。

2 个答案:

答案 0 :(得分:0)

是的。除了禁用按钮之外,您还可以创建一个布尔变量来跟踪按钮是启用还是禁用。然后,当单击按钮时,检查布尔变量,然后运行该函数。

例如:

private boolean buttonEnabled = true;

buttonEnabled = false;

public void actionPerformed(ActionEvent event){
   if (buttonEnabled){
      //what the button does
   }
}

答案 1 :(得分:0)

通过覆盖UI和Paint Methods的某些部分,您可以在JButton上具有自定义外观。这样,当您禁用按钮时,它看起来是一样的,只是不会执行任何操作。

例如,以下代码将产生此结果。

enter image description here

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.plaf.metal.MetalButtonUI;

@SuppressWarnings("serial")
public class DisableButtonExample extends JFrame {

    public DisableButtonExample() {
        super("Disabled Button Example");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout());
        setUp();
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void setUp() {
        JPanel mainPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gc = new GridBagConstraints();
        gc.fill = GridBagConstraints.BOTH;
        gc.weightx = 1;
        gc.weighty = 0;
        gc.gridwidth = 2;

        JLabel standardButtons = new JLabel("Standard Buttons", SwingConstants.CENTER);
        gc.gridx = 0;
        gc.gridy = 0;
        mainPanel.add(standardButtons, gc);

        JLabel customButtons = new JLabel("Custom Buttons", SwingConstants.CENTER);
        gc.gridx = 2;
        gc.gridy = 0;
        mainPanel.add(customButtons, gc);

        gc.gridwidth = 1;
        gc.weighty = 1;

        JButton standardEnabled = new JButton("Standard Enabled Button");
        gc.gridx = 0;
        gc.gridy = 1;
        mainPanel.add(standardEnabled, gc);

        JButton standardDisabled = new JButton("Standard Disabled Button");
        standardDisabled.setEnabled(false);
        gc.gridx = 1;
        gc.gridy = 1;
        mainPanel.add(standardDisabled, gc);

        JButton customEnabled = new CustomButton("Custom Enabled Button");
        gc.gridx = 2;
        gc.gridy = 1;
        mainPanel.add(customEnabled, gc);

        JButton customDisabled = new CustomButton("Custom Disabled Button");
        customDisabled.setEnabled(false);
        gc.gridx = 3;
        gc.gridy = 1;
        mainPanel.add(customDisabled, gc);

        getContentPane().add(mainPanel);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new DisableButtonExample());
    }

    private class CustomButton extends JButton {
        private boolean mousePressed = false;
        private CustomButtonUI ui = new CustomButtonUI();

        //Values from UIManager.get("Button.gradient")
        private Color fontColor = Color.BLACK;
        private Color c1 = new Color(221, 232, 243);
        private Color c2 = Color.WHITE;
        private Color c3 = new Color(184, 207, 229);

        public CustomButton(String textToDisplay) {
            super(textToDisplay);

            //Make it so we can use our own background colour when the button is disabled
            setContentAreaFilled(false);

            //Set custom UI so text is same colour when button is disabled
            ui = new CustomButtonUI();
            setUI(ui);

            //Set the font colour
            setForeground(fontColor);

            //A way to find out when the mouse is pressed
            addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    mousePressed = true;
                }

                @Override
                public void mouseReleased(MouseEvent e) {
                    mousePressed = false;
                }
            });
        }

        @Override
        protected void paintComponent(Graphics g) {
            final Graphics2D g2 = (Graphics2D) g.create();
            if(!mousePressed || !isEnabled()) {
                //Best guess at recreating the standard look of a JButton
                GradientPaint grad1 = new GradientPaint(0, (getHeight() * 3) / 10, c2, 0, (getHeight() * 8) / 10, c1, true);
                g2.setPaint(grad1);
                g2.fillRect(0, 0, getWidth(), getHeight());
                GradientPaint grad2 = new GradientPaint(0, (getHeight() * 5) / 10, new Color(c3.getRed(), c3.getGreen(), c3.getBlue(), 0), 0, getHeight(), c3);
                g2.setPaint(grad2);
            } else if(mousePressed && isEnabled()) {
                g2.setPaint(c3);
            }
            g2.fillRect(0, 0, getWidth(), getHeight());
            g2.dispose();
            super.paintComponent(g);
        }

        //Override so if you change text colour, it gets updated in the disabled state of the button
        @Override
        public void setForeground(Color fg) {
            if(ui != null) {
                ui.setFontColour(fg);
            }
            super.setForeground(fg);
        }

        //UI that controls the disabled state font colour
        private class CustomButtonUI extends MetalButtonUI  {
            private Color fontColor = Color.BLACK;

            public void setFontColour(Color fontColor) {
                this.fontColor = fontColor;
            }

            @Override
            public Color getDisabledTextColor() {
                return fontColor;
            }
        }
    }
}