[英]Adding a JButton when another JButton is pressed
我有一个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时将其添加?
添加按钮后,请确保revalidate
并repaint
revalidate();
repaint();
从setSize
和setLocation
的使用看来,您正在使用绝对定位或null
布局。 使用布局管理器。
添加后必须调用repaint()
。
如果没有异常,则需要刷新或重新粉刷容器,该容器将使新按钮变旧。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.