Swing Button Invisible直到actionPerformed

时间:2016-11-28 21:30:58

标签: java swing user-interface jbutton

是否可以创建一个在用户点击另一个按钮之前不会被看到的按钮?

我的目标是默认情况下按钮不可见,而不是点击时按钮。然后在执行另一个操作时变得可见。下面的代码是我创建此代码的原始尝试。

public void but_roll1ActionPerformed(java.awt.event.ActionEvent evt) 
{
    if (!bal_but.isEnabled() && !gamble_but.isEnabled()) {
        but_roll1.setVisible(true);
        but_roll1.setEnabled(true);         
        d1 = diceRoll();
        die1_display.setText(String.valueOf(d1));  
        but_roll1.setEnabled(false);
    } else {
        but_roll1.setVisible(false);
    }
}  

2 个答案:

答案 0 :(得分:2)

两个更好的策略:

  1. 将按钮放在带有第二个空白面板的CardLayout中,直到需要为止。
  2. 禁用按钮,直到单击第一个按钮。
  3. 我更喜欢第二个作为用户的“最少惊喜之路”。 YMMV。

    初始视图

    enter image description here

    在“效果”按钮操作后查看

    enter image description here

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    
    public class ButtonNotUsableTillAction {
    
        private JComponent ui = null;
    
        ButtonNotUsableTillAction() {
            initUI();
        }
    
        public void initUI() {
            if (ui!=null) return;
    
            ui = new JPanel(new GridLayout(1, 0, 4, 4));
            ui.setBorder(new EmptyBorder(4,4,4,4));
    
            // first demo, using card layout
            JPanel cardDemoPanel = new JPanel(new GridLayout(1, 0, 2, 2));
            cardDemoPanel.setBorder(new TitledBorder("Card Layout"));
            ui.add(cardDemoPanel);
    
            JButton actionCardButton = new JButton("Action");
            cardDemoPanel.add(actionCardButton);
    
            CardLayout cardLayout = new CardLayout();
            JPanel cardLayoutPanel = new JPanel(cardLayout);
            cardDemoPanel.add(cardLayoutPanel);
            cardLayoutPanel.add(new JPanel(), "panel");
            cardLayoutPanel.add(new JButton("Effect"), "button");
            cardLayout.show(cardLayoutPanel, "panel");
    
            ActionListener flipCardListener = new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    cardLayout.show(cardLayoutPanel, "button");
                }
            };
            actionCardButton.addActionListener(flipCardListener);
    
            // first demo, using disabled / enabled
            JPanel enabledDemoPanel = new JPanel(new GridLayout(1, 0, 2, 2));
            enabledDemoPanel.setBorder(new TitledBorder("Enabled"));
            ui.add(enabledDemoPanel);
            JButton actionEnabledButton = new JButton("Action");
            enabledDemoPanel.add(actionEnabledButton);
            JButton effectButton = new JButton("Effect");
            enabledDemoPanel.add(effectButton);
            effectButton.setEnabled(false);
            ActionListener enableComponentListener = new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    effectButton.setEnabled(true);
                }
            };
            actionEnabledButton.addActionListener(enableComponentListener);
        }
    
        public JComponent getUI() {
            return ui;
        }
    
        public static void main(String[] args) {
            Runnable r = new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (Exception useDefault) {
                    }
                    ButtonNotUsableTillAction o = new ButtonNotUsableTillAction();
    
                    JFrame f = new JFrame(o.getClass().getSimpleName());
                    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    f.setLocationByPlatform(true);
    
                    f.setContentPane(o.getUI());
                    f.pack();
                    f.setMinimumSize(f.getSize());
    
                    f.setVisible(true);
                }
            };
            SwingUtilities.invokeLater(r);
        }
    }
    

答案 1 :(得分:1)

正如@markspace所提到的,您需要在设置按钮可见后重新验证按钮的容器:

but_roll1.getParent().revalidate();