java中两个窗口连接到同一个窗口

如题,用class ZXwin extends JFrame implements ActionListener{建了多个窗口现在在A和C窗口中各有一个Jbutton按钮,要使单击按钮弹出B窗口,如何实现?DLwin dlwin;dlwin=new DLwin("登陆");dlwin.setVisible(true);我用上面三句弹出窗口的,但是碰到我上面所说的情况,编译会成功,但运行不了。碰见上面说的这种情况时,运行程序会报错Exception in thread "main" java.lang.OutOfMemoryError: Java heap space只发监视器代码,长度不够JButton btnNewButton = new JButton("登陆"); btnNewButton.setBounds(170,130,60,30); btnNewButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ dlwin.setVisible(true); dispose(); } });public void actionPerformed(ActionEvent e){ if(e.getSource()==ZXbutton1){ dlwin.setVisible(true); dispose(); } else if(e.getSource()==ZXbutton2) this.dispose(); }}
2026年09月25日 23:54
有5个网友回答
网友(1):

刚才你好象问过这个问题不是采纳了一个人的吗?
我觉得你没有理清自己的程序关系。下边给你一个例子。

----------------------------------------------------------------------------------------------------
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Run {

public static void main(String[] args) {
BFrame b = new BFrame();
AFrame a = new AFrame(b);
CFrame C = new CFrame(b);
}

}

class AFrame extends JFrame implements ActionListener {

BFrame b = null;

public AFrame(BFrame b) {
this.b = b;
setTitle("AFrame");
setSize(298, 120);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setLayout(null);

JButton btnNewButton = new JButton("Open B");
btnNewButton.addActionListener(this);
btnNewButton.setBounds(185, 9, 93, 23);
getContentPane().add(btnNewButton);

setVisible(true);
}

public void actionPerformed(ActionEvent e) {
b.setVisible(true);
}

}

class BFrame extends JFrame {

public BFrame() {
setTitle("BFrame");
setSize(298, 120);
setDefaultCloseOperation(HIDE_ON_CLOSE);
}

}

class CFrame extends JFrame implements ActionListener {
BFrame b = null;

public CFrame(BFrame b) {
setTitle("CFrame");
this.b = b;
setSize(298, 120);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setLayout(null);

JButton btnNewButton = new JButton("Open B");
btnNewButton.addActionListener(this);
btnNewButton.setBounds(185, 9, 93, 23);
getContentPane().add(btnNewButton);

setVisible(true);
}

public void actionPerformed(ActionEvent e) {
b.setVisible(true);
}

}

网友(2):

dlwin.setBounds(30, 10, 350, 400);//设置位置和大小

网友(3):

最好能发一部分相对完整的代码。这里很难看得出你是怎么实现的,以至于把内存都干满了

网友(4):

import java.awt.event.*;

import javax.swing.*;

public class TestDialog extends JDialog implements ActionListener
{
private JButton jb1 = new JButton("Show Dialog2");
private JButton jb2 = new JButton("Show Dialog1");
private JDialog jd2 = new JDialog();

TestDialog()
{
initJD1();
initJD2();
}

private void initJD1()
{
setTitle("Dialog1");
getContentPane().setLayout(null);
getContentPane().add(jb1);
jb1.setSize(150, 25);
jb1.addActionListener(this);
setSize(400, 400);
setVisible(true);
}

private void initJD2()
{
jd2.setTitle("Dialog2");
jd2.getContentPane().setLayout(null);
jd2.getContentPane().add(jb2);
jb2.setSize(150, 25);
jb2.addActionListener(this);
jd2.setSize(400, 400);
}

public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if(source == jb1)
{
this.setVisible(false);
jd2.setVisible(true);
}
else if(source == jb2)
{
jd2.setVisible(false);
this.setVisible(true);
}

}

public static void main(String[] args)
{
new TestDialog();
}

网友(5):

内存溢出一般是自己调用自己或者死循环导致内存泄露了,所以检查下你的代码吧
特别是B窗口的代码