【问题标题】:Disable Component in Java GUI w/o Graying it Out禁用 Java GUI 中的组件,不使其变灰
【发布时间】:2019-01-08 08:32:23
【问题描述】:

我目前正在开发一个允许用户对着程序玩井字游戏的 java 项目。我想禁用已经被点击的方块。我想知道是否有任何方法可以禁用组件(JButtons)而不使其变灰,就像component.setEnabled(false) 所做的那样。

【问题讨论】:

    标签: java swing user-interface jbutton jcomponent


    【解决方案1】:

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

    例如:

    private boolean buttonEnabled = true;
    

    .

    buttonEnabled = false;
    

    .

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

    【讨论】:

      【解决方案2】:

      通过覆盖部分 UI 和绘制方法,您可以在 JButton 上拥有自定义外观。这样当你禁用按钮时,它看起来是一样的,只是它不会做任何事情。

      例如,下面的代码会产生这个。

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

      【讨论】:

        猜你喜欢
        • 2011-07-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-08
        • 1970-01-01
        相关资源
        最近更新 更多