java中如何从一个窗口通过点击按钮进入另外一个窗口? (两个窗口不在一个class中。)

 我来答
给我七世等待5
2019-09-12 · TA获得超过607个赞
知道小有建树答主
回答量:1074
采纳率:76%
帮助的人:110万
展开全部
要写两个窗体,第一个窗体:
import javax.swing.SwingUtilities;
import javax.swing.JPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Rectangle;

public class Frame1 extends JFrame implements ActionListener {

private static final long serialVersionUID = 1L;

private JPanel jContentPane = null;

private JButton jButton = null;

public void actionPerformed(ActionEvent arg0) {
Frame2 f2=new Frame2(this);
f2.setVisible(true);
this.setVisible(false);
}
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(92, 62, 105, 35));
jButton.setText("进入子窗体");
jButton.addActionListener(this);
}
return jButton;
}
public static void main(String[] args) {
// TODO 自动生成方法存根
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Frame1 thisClass = new Frame1();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
public Frame1() {
super();
initialize();
}
private void initialize() {
this.setSize(300, 200);
this.setContentPane(getJContentPane());
this.setTitle("主窗体");
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJButton(), null);
}
return jContentPane;
}
}

第二个窗体(可以从第一个窗体进入第二个窗体,也可从第二个窗体回到第一个窗体)
import javax.swing.SwingUtilities;
import javax.swing.JPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Rectangle;

public class Frame2 extends JFrame implements ActionListener, WindowListener {

private static final long serialVersionUID = 1L;

private JPanel jContentPane = null;

private JButton jButton = null;

private Frame1 f1=null;

public void actionPerformed(ActionEvent arg0) {
f1.setVisible(true);
this.setVisible(false);

}
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(85, 48, 105, 39));
jButton.setText("回到主窗体");
jButton.addActionListener(this);
}
return jButton;
}
public static void main(String[] args) {
// TODO 自动生成方法存根
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Frame2 thisClass = new Frame2();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
public Frame2() {
super();
initialize();
}

public Frame2(Frame1 f) {
this();
f1=f;
}
private void initialize() {
this.setSize(300, 200);
this.setContentPane(getJContentPane());
this.setTitle("子窗体");
this.addWindowListener(this);
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJButton(), null);
}
return jContentPane;
}

public void windowActivated(WindowEvent arg0) {
}

public void windowClosed(WindowEvent arg0) {
}

public void windowClosing(WindowEvent arg0) {
f1.setVisible(true);
}

public void windowDeactivated(WindowEvent arg0) {
}

public void windowDeiconified(WindowEvent arg0) {
}

public void windowIconified(WindowEvent arg0) {
}

public void windowOpened(WindowEvent arg0) {
}
}
神牛码农
2019-09-11 · 用技术行走在移动互联网时代
神牛码农
采纳数:297 获赞数:1178

向TA提问 私信TA
展开全部
在action的监听中直接new另外窗口即可
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式