![](/img/trans.png)
[英]Java ActionListener Return Variable To Method That Contains ActionListener
[英]Java ActionListener On Main Method
我創建了一個Java類,在其中使用了一個框架,一個面板,兩個文本框和兩個帶有一個按鈕的標簽,但是Jbutton actionListener無法正常工作,請幫忙我一個多小時,但是它不能用於圖形化使用界面使用javax.swing
public class GuiBasics {
/**
* @param args the command line arguments
*/
public GuiBasics(){
int count=0;
JFrame jf=new JFrame("Gui Basics");
JPanel jp=new JPanel();
JLabel jl1=new JLabel("Username");
JLabel jl2=new JLabel("Password");
JTextField jt1=new JTextField(20);
JTextField jt2=new JTextField(20);
jp.add(jl1);
jp.add(jt1);
jp.add(jl2);
jp.add(jt2);
jp.add(jb);
jf.add(jp);
jf.setSize(400, 600);
jf.setVisible(true);
JButton button = new JButton("Click");
button.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
System.out.println("Checking!");
}
});
}
public static void main(String[] args) {
new GuiBasics();
}
}
您需要像對其他小部件組件一樣將按鈕添加到JPanel。
代碼應為:
JButton button = new JButton("Click");
jp.add(button);
您應該會看到&能夠單擊您的按鈕
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.