我有一个 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();
从 setSize
和 setLocation
的使用来看,您似乎正在使用绝对定位或 null
布局。使用布局管理器。
关于java - 当另一个 JButton 被按下时添加一个 JButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16304888/