当我单击另一个 jbutton 时,我想获得一个 jbutton。
这里是示例代码的链接(以 jbutton 身份登录,asdf 作为密码登录)
//File Name= test1.java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class test1 extends JFrame {
public static void main(String[] args) {
new test1();
}
public test1() {
super("Using JButton");
Container content = getContentPane();
content.setBackground(Color.white);
content.setLayout(new FlowLayout());
JButton button = new JButton("First");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("You clicked first button");
}
});
content.add(button);
JButton button2 = new JButton("Second");
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("You clicked second button");
}
});
content.add(button2);
pack();
setVisible(true);
}
}
如果我单击“第一个”按钮,我想隐藏“第二个”按钮。我的期望就像,”
button.setName("something");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("You clicked first button");
btn2=getButtonByName("something");
btn2.setVisible(!btn2.isVisible());
}
});"