问题描述
我的程序中有一个错误示例.我创建了4个按钮:1、2、3、4.按钮2与按钮4重叠,并且我刚刚添加了2和4的事件.
I have an example of a error in my program. I created 4 buttons: 1, 2, 3, 4. Button 2 overlaps button 4, and i just added events for 2 and 4.
如果我单击按钮2,它将被隐藏,并且将显示按钮4.如果我单击按钮4,将显示按钮2,并且按钮4将再次被按钮2覆盖.好像发生了什么,但是,什么时候完成上述操作后,我单击按钮1或按钮3,按钮4将显示出来,当我指向它(不单击)时,它就会消失.
If Iclick on Button 2, it will be hidden, and Button 4 will be displayed.And if I click on Button 4, Button 2 will be displayed, and Button 4will be covered by Button 2 again. As if anything happened, but, whenI click on Button 1 or Button 3 after doing the above, Button 4 willbe displayed, when I point at it (not click), it will disappear.
public class UI extends JFrame {
public UI(String title) {
Container container = this.getContentPane();
container.setLayout(null);
JButton btn1 = new JButton("1");
btn1.setBounds(10, 10, 50, 50);
btn1.setBackground(Color.RED);
JButton btn2 = new JButton("2");
btn2.setBounds(10, 70, 50, 50);
btn2.setBackground(Color.GREEN);
JButton btn3 = new JButton("3");
btn3.setBounds(10, 130, 50, 50);
btn3.setBackground(Color.BLUE);
JButton btn4 = new JButton("4");
btn4.setBounds(10, 70, 50, 50);
btn4.setBackground(Color.YELLOW);
container.add(btn1);
container.add(btn2);
container.add(btn3);
container.add(btn4);
btn2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
btn2.setVisible(false);
}
});
btn4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
btn2.setVisible(true);
}
});
this.setSize(400, 500);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
}
推荐答案
您只是在actionPerformed()
方法中出错.您需要更改两者 JButton
的可见性,而不仅仅是一个.
You simply have an error in the actionPerformed()
methods. You need to change the visibility of both JButton
s, not just one.
这是您的代码.我只添加了两行,它们由注释// ADDED THIS LINE
Here is your code. I only added two lines and they are indicated by the comment // ADDED THIS LINE
public class UI extends JFrame {
public UI(String title) {
Container container = this.getContentPane();
container.setLayout(null);
JButton btn1 = new JButton("1");
btn1.setBounds(10, 10, 50, 50);
btn1.setBackground(Color.RED);
JButton btn2 = new JButton("2");
btn2.setBounds(10, 70, 50, 50);
btn2.setBackground(Color.GREEN);
JButton btn3 = new JButton("3");
btn3.setBounds(10, 130, 50, 50);
btn3.setBackground(Color.BLUE);
JButton btn4 = new JButton("4");
btn4.setBounds(10, 70, 50, 50);
btn4.setBackground(Color.YELLOW);
btn4.setVisible(false); // Initially we only want to see 'btn2'.
container.add(btn1);
container.add(btn2);
container.add(btn3);
container.add(btn4);
btn2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
btn2.setVisible(false);
btn4.setVisible(true); // ADDED THIS LINE
}
});
btn4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
btn2.setVisible(true);
btn4.setVisible(false); // ADDED THIS LINE
}
});
this.setSize(400, 500);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> new UI("UI"));
}
}
请注意,尽管代码没有它也可以工作,但我认为您应该将btn4
的初始可见性设置为 false .我也在上面的代码中完成了此操作.
Note that although the code works without it, I think you should set the initial visibility of btn4
to false. I have also done this in the above code.
这篇关于按钮重叠或消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!