java中如何从一个窗口通过点击按钮进入另外一个窗口? (两个窗口不在一个class中。)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java中如何从一个窗口通过点击按钮进入另外一个窗口? (两个窗口不在一个class中。)相关的知识,希望对你有一定的参考价值。
要写两个窗体,第一个窗体: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)
参考技术A 在action的监听中直接new另外窗口即可
如何在java程序中,当点击一个按钮后,关闭当前窗口,开启一个新的窗口?
JButton btn=new JButton(new AbstractAction("关闭并打开") @Override public void actionPerformed(ActionEvent e) oldFrame.dispose();// 关闭并销毁,无需销毁可采用oldFrame.setVisible(false); newFrame.setVisible(true);// 打开新窗口 );import javax.swing.*;
import java.awt.Rectangle;
import java.awt.event.*;
public class Swing7 extends JFrame implements ActionListener
JButton jb = new JButton();
public Swing7()
this.setTitle("Java——");
jb.setText("确定");
jb.setMnemonic('a');
this.add(jb);
this.setBounds(200, 300, 250, 300);
ctionListener就是Swing7实例。
public void actionPerformed(ActionEvent e) // 实现ActionListener接口的actionPerformed接口。
JFrame frame = new JFrame("新窗口");//构造一个新的JFrame,作为新窗口。
frame.setBounds(// 让新窗口与Swing7窗口示例错开50像素。
new Rectangle(
(int) this.getBounds().getX() + 50,
(int) this.getBounds().getY() + 50,
(int) this.getBounds().getWidth(),
(int) this.getBounds().getHeight()
)
);
JLabel jl = new JLabel();// 注意类名别写错了。
frame.getContentPane().add(jl);
jl.setText("这是新窗口");
jl.setVerticalAlignment(JLabel.CENTER);
jl.setHorizontalAlignment(JLabel.CENTER);// 注意方法名别写错了。
frame.setVisible(true);
public static void main(String args[])
Swing7 s = new Swing7();
假设本身是Form1,第二个窗口是Form2,你双击按钮进入那个按钮的点击事件,编写以下代码:
Form2 f2=new Form2();
f2.Show();
this.Hide();
简单的程序代码,运用代码就可以完美解决
以上是关于java中如何从一个窗口通过点击按钮进入另外一个窗口? (两个窗口不在一个class中。)的主要内容,如果未能解决你的问题,请参考以下文章
如何在java程序中,当点击一个按钮后,关闭当前窗口,开启一个新的窗口。