如何实现点击一个窗口的按钮,弹出一个已有的另一个窗口啊? 我已经建立好了两个窗口,都是用JFrame建立的。A窗口上有一个按钮JButton,按下按钮后,A窗口关闭,已有的B窗口打开,哪位大侠能告诉我怎么做啊??多谢了!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import javax.swing.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class Test{ private JFrame frame_a = null, frame_b = null; private JButton button_1 = null, button_2 = null; public Test() { frame_a = new JFrame("Frame A"); frame_b = new JFrame("Frame B"); button_1 = new JButton("new Frame A"); button_2 = new JButton("new Frame B"); button_1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { frame_a.dispose(); frame_b.setVisible(true); }}); button_2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { frame_b.dispose(); frame_a.setVisible(true); }}); JPanel pane_1 = new JPanel(); JPanel pane_2 = new JPanel(); pane_1.add(button_1); pane_2.add(button_2); frame_a.getContentPane().add(pane_1); frame_b.getContentPane().add(pane_2); frame_a.setSize(300, 200); frame_b.setSize(200, 100); frame_a.setVisible(true); } public static void main(String[] args) { new Test(); }} 如果不想丢失frame_a的数据我觉得frame_a.dispose()这个地方改成setVisible(false)好点吧建议frame_a.dispose();和frame_b.setVisible(true);的顺序颠倒一下,窗口的切换会自然一点,对于一半配置的机器而言 窗口的名字可以在JFrame构造的时候创建new JFrame("Frame Title"); 也可以用setTitle("Frame Title");。 之后可用public String getTitle()来获取。 这种方法虽可实现,但不是很好,何不在按钮事件上new出一个JFrame,然后将主Frame进行隐藏而新Frame进行显示呢? java \r \n 区别 一个比较的问题~ 将不同的数据库中的表记录转到mysql数据库中? 两个简单问题先来先给分[网络 + string] ssh配置后启不了comcat请帮助忙呀?????????? JAVA写拨号程序有相应API吗? 斑竹帮我看看,这个问题扰我一个星期了 请问,怎样把Tomcat里的日志文件加到JBuilder里 谁能给我这样一个函数,去除字符串中所有回车和空格 大家帮我找一个错误好吗? 学会使用args 大家帮我看下这个Applet题,看看哪有错误!谢谢!
import java.awt.event.ActionListener;public class Test{ private JFrame frame_a = null, frame_b = null; private JButton button_1 = null, button_2 = null; public Test() {
frame_a = new JFrame("Frame A");
frame_b = new JFrame("Frame B");
button_1 = new JButton("new Frame A");
button_2 = new JButton("new Frame B");
button_1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
frame_a.dispose();
frame_b.setVisible(true);
}});
button_2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
frame_b.dispose();
frame_a.setVisible(true);
}});
JPanel pane_1 = new JPanel();
JPanel pane_2 = new JPanel();
pane_1.add(button_1);
pane_2.add(button_2);
frame_a.getContentPane().add(pane_1);
frame_b.getContentPane().add(pane_2);
frame_a.setSize(300, 200);
frame_b.setSize(200, 100);
frame_a.setVisible(true);
} public static void main(String[] args) {
new Test();
}
}
我觉得frame_a.dispose()这个地方改成setVisible(false)好点吧
建议frame_a.dispose();和frame_b.setVisible(true);的顺序颠倒一下,窗口的切换会自然一点,对于一半配置的机器而言