关于swing白窗口问题 现在我有1,2个窗口1窗口按jbutton new(窗口2),关将窗口1 setVisible(false);现在我要通过窗口2重新将窗口1设为 setVisible(true); 怎么得到窗口1这个对象 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你创建窗体2的时候需要在窗体1中定义一个成员变量 用来接收窗体1对象也就是把窗体1做为窗体2的一个属性创建时把this 传给它,这里的this带表窗体1new JFrame2(this) package com;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton; import javax.swing.JFrame;public class CSDNSwing2 extends JFrame { public CSDNSwing2() { JButton button = new JButton("创建Frame2"); button.addActionListener(new Testlistener(this)); this.add(button); this.setTitle("窗体1"); this.setSize(200,200); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(null); this.setVisible(true); } public static void main(String[] args) { new CSDNSwing2(); }}class Testlistener implements ActionListener{ private JFrame frame1; public Testlistener(JFrame frame1) { this.frame1 = frame1; } public void actionPerformed(ActionEvent e) { new JFrame2(frame1); } }//窗体2class JFrame2 extends JFrame{ private JFrame frame1; public JFrame2(JFrame frame1) { this.frame1 = frame1; frame1.setVisible(false); JButton button = new JButton("创建Frame1"); button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { JFrame2.this.frame1.setVisible(true); JFrame2.this.setVisible(false); } }); this.add(button); this.setTitle("窗体2"); this.setSize(200,200); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(null); this.setVisible(true); }} class JFrame2 extends JFrame{ private JFrame frame1; public JFrame2(JFrame frame1) { this.frame1 = frame1; frame1.setVisible(false); JButton button = new JButton("创建Frame1"); button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { JFrame2.this.frame1.setVisible(true); JFrame2.this.setVisible(false); } }); this.add(button); this.setTitle("窗体2"); this.setSize(200,200); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(null); this.setVisible(true); }} 为什么这个程序没有循环语句却能一直循环输出? 对类和方法的继承覆盖的求解。 project 与 project 之间调用类的情况有吗??有意义吗?? 问个eclipse的菜鸟问题。 AWT组件的相关问题 关于加JMenubar的问题 myeclipse包内建包的问题 急啊! 在java里怎样实现c语言里的ungetc()功能 关于接口的作用 java线程的静态变量共享问题 编译出错:cannot find symbol toString的用法
new JFrame2(this)
package com;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;public class CSDNSwing2 extends JFrame {
public CSDNSwing2() {
JButton button = new JButton("创建Frame2");
button.addActionListener(new Testlistener(this));
this.add(button);
this.setTitle("窗体1");
this.setSize(200,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args) {
new CSDNSwing2();
}
}class Testlistener implements ActionListener{
private JFrame frame1;
public Testlistener(JFrame frame1) {
this.frame1 = frame1;
}
public void actionPerformed(ActionEvent e) {
new JFrame2(frame1);
}
}//窗体2
class JFrame2 extends JFrame{
private JFrame frame1;
public JFrame2(JFrame frame1) {
this.frame1 = frame1;
frame1.setVisible(false);
JButton button = new JButton("创建Frame1");
button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
JFrame2.this.frame1.setVisible(true);
JFrame2.this.setVisible(false);
}
});
this.add(button);
this.setTitle("窗体2");
this.setSize(200,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
}
private JFrame frame1;
public JFrame2(JFrame frame1) {
this.frame1 = frame1;
frame1.setVisible(false);
JButton button = new JButton("创建Frame1");
button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
JFrame2.this.frame1.setVisible(true);
JFrame2.this.setVisible(false);
}
});
this.add(button);
this.setTitle("窗体2");
this.setSize(200,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
}