[英]Hiding JFrame with anonymous ActionListener in a JButton
我有一個歡迎(或菜單)窗口(JFrame),其中包含一些按鈕(JButton),用於每個可能的操作。 其中每個都應該啟動一個新窗口並隱藏歡迎窗口。 我知道我可以用setVisible(false);
做到這一點setVisible(false);
。 但我還不能讓它發揮作用。
這是我的代碼的一個例子:
_startBtn.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
System.out.println("_startBtn pressed");
// Code to hide this JFrame and initialize another
}
我的問題是,我怎么能使用像這樣的匿名類呢?
提前致謝!
我正在為你發貼一個例子,我希望它會對你有所幫助。
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class windows_test {
JFrame login = null;
JFrame inner_frame = null;
public windows_test() {
login = new JFrame();
login.setBounds(10, 10, 300, 300);
login.setLayout(new BorderLayout());
JButton button = new JButton("Login");
login.add(button, BorderLayout.CENTER);
login.setVisible(true);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if (inner_frame == null) {
inner_frame = new JFrame();
}
inner_frame.setLayout(new FlowLayout(FlowLayout.CENTER));
inner_frame.add(new JButton("inner frame"));
inner_frame.setVisible(true);
login.setVisible(false);
inner_frame.setBounds(10, 10, 300, 300);
}
});
}
}
我建議你使用jpanel而不是jframes,但你已經要求框架,所以我用它們創建了它。 希望它會幫助你問我某處是否錯,或者你無法理解。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.