如何点击按钮打开另一窗口 现有两个窗口,想要点击第一个窗口中的确定按钮打开第二个窗口,请问该用什么事件,如何实现呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;public class Test extends JFrame { private JButton button = null; private int i = 1; public Test() { button = new JButton("打开新窗口"); button.addActionListener(new ActionListener(){ JFrame frame = null; public void actionPerformed(ActionEvent e) { frame = new JFrame("New JFrame " + i++); frame.setSize(220, 120); frame.setVisible(true); }}); JPanel pane = new JPanel(); pane.add(button); this.getContentPane().add(pane); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(300, 200); this.setVisible(true); } public static void main(String[] args) { new Test(); }} 先谢谢了! 不过我是定义了两个独立的JFrame,想通过点击第一个JFrame中的确定按钮,来显示第二个JFrame,第一个JFrame被覆盖,不知道有没有其它的方法? 先谢谢了! 不过我是定义了两个独立的JFrame,想通过点击第一个JFrame中的确定按钮,来显示第二个JFrame,第一个JFrame被覆盖,不知道有没有其它的方法? 第一个被覆盖的话可以在Frame2.show()前把Frame1隐藏起来,hide()方法,然后用判断第二个Frame2是不是closing或closed~~如果是,Frame1在显示出来。 既然要覆盖,何必用两个JFrame以下代码可以实现这切换,在一个JFrame中操作,不知能否满足需要import javax.swing.*;import java.awt.event.*;public class NewWindow extends JFrame{ private JButton b1 = new JButton("更换窗口"); private JPanel p1 = new JPanel(); private JButton b2 = new JButton("窗口已更新"); private JPanel p2 = new JPanel(); private JPanel panel = (JPanel)this.getContentPane(); public NewWindow(){ p1.add(b1); p2.add(b2); panel.add(p1); b1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ p1.setVisible(false); panel.add(p2); } } ); } public static void main(String[] args) { NewWindow w = new NewWindow(); w.setSize(300,200); w.setVisible(true);}} 既然要覆盖,何必用两个JFrame以下代码可以实现这切换,在一个JFrame中操作,不知能否满足需要import javax.swing.*;import java.awt.event.*;public class NewWindow extends JFrame{ private JButton b1 = new JButton("更换窗口"); private JPanel p1 = new JPanel(); private JButton b2 = new JButton("窗口已更新"); private JPanel p2 = new JPanel(); private JPanel panel = (JPanel)this.getContentPane(); public NewWindow(){ p1.add(b1); p2.add(b2); panel.add(p1); b1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ p1.setVisible(false); panel.add(p2); } } ); } public static void main(String[] args) { NewWindow w = new NewWindow(); w.setSize(300,200); w.setVisible(true);}} 发一个回复,为什么会出现两个,bug?? 谢谢各位了,我决定用bitzhq(虹墙)的方法来实现了:) 新手关于用JEditorPane打不开网页的问题 工作不到一年,想要学习下设计模式,烦请各位前辈推荐一本书。 ubuntu 安装jdk报错 将数据插入到csv中的问题。在线等待! 如何使java Matcher线程安全 在Dialog关闭的同时是触发一个jframe中的jtable刷新???在线等待呀! webapplicaiton的部署问题 WEB数据批量录入、WEB数据打印、WEB图形(统计图),要要就进去下 scwd?? 让我们讨论这个系统吧 类clone()问题 jtable删除某一行记录的问题
import java.awt.event.ActionListener;import javax.swing.*;public class Test extends JFrame {
private JButton button = null;
private int i = 1; public Test() {
button = new JButton("打开新窗口");
button.addActionListener(new ActionListener(){
JFrame frame = null;
public void actionPerformed(ActionEvent e) {
frame = new JFrame("New JFrame " + i++);
frame.setSize(220, 120);
frame.setVisible(true);
}});
JPanel pane = new JPanel();
pane.add(button);
this.getContentPane().add(pane);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 200);
this.setVisible(true);
} public static void main(String[] args) {
new Test();
}
}
不过我是定义了两个独立的JFrame,想通过点击第一个JFrame中的确定按钮,来显示第二个JFrame,第一个JFrame被覆盖,不知道有没有其它的方法?
不过我是定义了两个独立的JFrame,想通过点击第一个JFrame中的确定按钮,来显示第二个JFrame,第一个JFrame被覆盖,不知道有没有其它的方法?
以下代码可以实现这切换,在一个JFrame中操作,不知能否满足需要
import javax.swing.*;
import java.awt.event.*;public class NewWindow extends JFrame{
private JButton b1 = new JButton("更换窗口");
private JPanel p1 = new JPanel();
private JButton b2 = new JButton("窗口已更新");
private JPanel p2 = new JPanel();
private JPanel panel = (JPanel)this.getContentPane();
public NewWindow(){
p1.add(b1);
p2.add(b2);
panel.add(p1);
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
p1.setVisible(false);
panel.add(p2);
}
} );
}
public static void main(String[] args) {
NewWindow w = new NewWindow();
w.setSize(300,200);
w.setVisible(true);
}
}
以下代码可以实现这切换,在一个JFrame中操作,不知能否满足需要
import javax.swing.*;
import java.awt.event.*;public class NewWindow extends JFrame{
private JButton b1 = new JButton("更换窗口");
private JPanel p1 = new JPanel();
private JButton b2 = new JButton("窗口已更新");
private JPanel p2 = new JPanel();
private JPanel panel = (JPanel)this.getContentPane();
public NewWindow(){
p1.add(b1);
p2.add(b2);
panel.add(p1);
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
p1.setVisible(false);
panel.add(p2);
}
} );
}
public static void main(String[] args) {
NewWindow w = new NewWindow();
w.setSize(300,200);
w.setVisible(true);
}
}