gpt4 book ai didi

java - Swing 按钮在执行操作之前不可见

转载 作者:行者123 更新时间:2023-12-01 21:12:46 25 4
gpt4 key购买 nike

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

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

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

最佳答案

两个更好的策略:

  1. 将按钮放入 CardLayout 中,并在需要时使用第二个空白面板。
  2. 禁用该按钮,直到单击第一个按钮。

我更喜欢第二条作为用户“最不意外的路径”。 YMMV。

初始 View

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

关于java - Swing 按钮在执行操作之前不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40853846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com