java中如何从一个窗口通过点击按钮进入另外一个窗口? (两个窗口不在一个class中。)
2个回答
展开全部
要写两个窗体,第一个窗体:
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) {
}
}
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) {
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询