gpt4 book ai didi

Java按钮点击后消失

转载 作者:太空宇宙 更新时间:2023-11-04 11:33:06 26 4
gpt4 key购买 nike

我尝试在另一个按钮中使用 JOptionPane 添加按钮,但在单击原始按钮后,该按钮不断消失。

我尝试通过迭代 handPanel.buttons 来手动添加 JPanel,而不是使用“handPanelNPC.getHandPanel()”,但它仍然无法工作。我已经检查了 ArrayList 的大小,它已经正确插入。

布局测试.java

public class LayoutTesting extends JFrame{
HandPanel handPanelNPC = new HandPanel();

public LayoutTesting(HandPanel handPanel, int type){
Container pane = getContentPane();

if (type==1){
handPanelNPC = handPanel;
}
pane.setLayout(new BorderLayout());
pane.add(handPanelNPC.getHandPanel(), BorderLayout.NORTH);
validate();
repaint();
}

public LayoutTesting(){
Container pane = getContentPane();

pane.setLayout(new BorderLayout());
pane.add(handPanelNPC.getHandPanel(), BorderLayout.NORTH);
}
}

HandPanel.java

public class HandPanel implements ActionListener{   
JFrame frame = null;
JPanel panel = new JPanel();
ArrayList<JButton> buttons = new ArrayList<JButton>();

public HandPanel(){
addNewButton();
panel.setLayout(new FlowLayout());
panel.add(buttons.get(0));
}

public JComponent getHandPanel(){
panel = new JPanel();
for(int i=0; i<buttons.size(); i++){
JButton button = buttons.get(i);
panel.add(button);
}

return panel;
}

public void addNewButton(){
JButton button = new JButton();
button.setPreferredSize(new Dimension(40,58));
button.addActionListener(this);

buttons.add(button);
}

public void actionPerformed(ActionEvent e) {
String[] options = {"Summon", "Set", "Add Card"};

int messageType = JOptionPane.QUESTION_MESSAGE;
int code = JOptionPane.showOptionDialog(
frame,
"What would you like to do?",
"Card Name",
0, messageType, null,
options, options[1]);

if (code==2){
addNewButton();
LayoutTesting frame = new LayoutTesting(this, 1);
}
}
}

Main.java

public class Main extends JFrame{
public static void main(String[] args){
LayoutTesting frame = new LayoutTesting();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setVisible(true);
}
}

最佳答案

您的代码中发生了太多奇怪的事情,无法给出简单的答案。

尝试通过查看 HandPanel.java 中的这一行来调试您自己的代码:

LayoutTesting frame = new LayoutTesting(this, 1);

它到底有什么作用?现在删除该行,按钮将不会消失。现在尝试找出该行的作用以及按钮消失的原因。

还有“panel.add(buttons.get(0));”什么都不做,因为当您进行该调用时,数组中永远不存在按钮(您随后在另一个方法中添加按钮)。

这是一个粗略的工作演示,可以让您在第一帧中添加任意数量的新卡,并且每张卡都有一个按钮,可以让您召唤一张新卡。

public class LayoutTesting extends JFrame{
Container pane;

//Add card when button is pressed:
public void addCard(){
Container card = new JPanel();
card.setBackground(Color.red);
JButton newButton = addNewButton();
newButton.setBackground(Color.red);
card.add(newButton);
pane.add(card);
revalidate();
repaint();
}

//Setup window and add button:
public LayoutTesting(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
setVisible(true);
setLayout(new FlowLayout());
pane = new JPanel();
pane.setLayout(new GridBagLayout());
JButton firstButton = addNewButton();
firstButton.setBackground(Color.green);
add(pane);
add(firstButton);
}

//Create button and action listener:
public JButton addNewButton(){
JButton button = new JButton();
button.setPreferredSize(new Dimension(40, 58));
button.addActionListener(new java.awt.event.ActionListener(){
@Override
public void actionPerformed(java.awt.event.ActionEvent evt){
buttonAction(evt);
}
});
return button;
}

//Action fer each button:
public void buttonAction(ActionEvent e) {
String[] options = {"Summon", "Set", "Add Card"};

int messageType = JOptionPane.QUESTION_MESSAGE;
int code = JOptionPane.showOptionDialog(
this,
"What would you like to do?",
"Card Name",
0, messageType, null,
options, options[1]);

if (code==2){
addCard();
}
}
}

关于Java按钮点击后消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43576522/

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