gpt4 book ai didi

java - 当另一个 JButton 被按下时添加一个 JButton

转载 作者:行者123 更新时间:2023-12-01 14:37:56 26 4
gpt4 key购买 nike

我有一个 JButton,我想在按下它时创建一个新的 JButton,我添加了一个如下所示的 ActionListener,但它没有添加另一个 JButton。

public void actionPerformed(ActionEvent e){
Object command = e.getSource();
if(command.equals(play)){
ImageIcon i1 = new ImageIcon("NewGame.png");
width = i1.getIconWidth();
height = i1.getIconWidth();
newGame = new JButton(i1);
newGame.setBorderPainted(false);
newGame.setContentAreaFilled(false);
newGame.setSize(width, height);
newGame.setLocation(600,100);
add(newGame);
System.out.println("It Works");
}
}

我该如何做到这一点,以便当我按下已经存在的 JButton 时会添加这个 JButton?

最佳答案

确保在添加按钮后重新验证重新绘制

revalidate();
repaint();

setSizesetLocation 的使用来看,您似乎正在使用绝对定位或 null 布局。使用布局管理器。

关于java - 当另一个 JButton 被按下时添加一个 JButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16304888/

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