JAVASwing设计了三个Frame界面在同一包中 想点第一个界面里“进入”就出现第二个界面 怎么连接呢代码吗?

2024-12-14 23:59:45
推荐回答(4个)
回答1:

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) {
}
}
照此再加第三个界面就可以了

回答2:

为什么不用JDialog呢?个人觉得比JFrame要好用,一般情况下一个程序只有一个JFrame的,new一个新的Frame,对其sevVisible(true),然后将前一个对前一个Frame进行setVisible(false)隐藏掉,dispose()掉就好了

回答3:

1) 下面放一个面板,用cardlayout布局,然后里面add你所要展示的各个面板,然后在每个按钮的监听器里面获取这个面板,用show方法显示add进来的对应面板.
2) 干嘛不直接用 JTabbedPane (切换面板)

回答4:

百度搜索一下视频看看,应该有吧,久供参考,谢谢