JAVA 中怎么弹出别一个窗体
发布网友
发布时间:2023-08-12 21:26
我来回答
共3个回答
热心网友
时间:2024-12-12 14:59
重点地是窗体弹出类:WindowsCenter.java
文件一父窗文件名Frame3.java
package untitled1;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Frame3 extends JFrame {
JButton jButton1 = new JButton();
public Frame3() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
getContentPane().setLayout(null);
setSize(400,400);
jButton1.setBounds(new Rectangle(86, 125, 186, 52));
jButton1.setText("点击弹出一个窗口");
jButton1.addActionListener(new Frame3_jButton1_actionAdapter(this));
this.getContentPane().add(jButton1, null);
}
public void jButton1_actionPerformed(ActionEvent e) {
this.dispose();//释放当前窗口
WindowsCenter.Center(new Frame2());//弹出第三个窗口
}
public static void main (String[] args)
{
WindowsCenter.Center(new Frame3());//弹出主窗口
}
}
class Frame3_jButton1_actionAdapter implements ActionListener {
private Frame3 adaptee;
Frame3_jButton1_actionAdapter(Frame3 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
//要弹出的子窗口文件名:Frame2.java
package untitled1;
import java.awt.*;
import javax.swing.*;
import java.awt.Rectangle;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Frame2 extends JFrame
{
private JButton jButton1=new JButton();
public Frame2 ()
{
try {
jbInit();
}
catch(Exception exception) {
exception.printStackTrace();
}
this.setVisible(true);
}
private void jbInit () throws Exception
{
this.setBounds(300,180,400,300);
getContentPane().setLayout(null);
jButton1.setBounds(new Rectangle(127, 120, 139, 36));
jButton1.setMnemonic('C');
jButton1.setText("点我(C)");
jButton1.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
jButton1_actionPerformed(e);
}
});
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().add(jButton1);
}
public void jButton1_actionPerformed (ActionEvent e)
{
this.setVisible(false);
JFrame jf1=new JFrame("子窗口");
jf1.setBounds(100,50,800,600);
jf1.setDefaultCloseOperation(jf1.EXIT_ON_CLOSE);
jf1.setVisible(true);
}
}
//窗体居中弹出类文件名:WindowsCenter.java类
package untitled1;
import java.awt.Toolkit;
import java.awt.Dimension;
import javax.swing.*;
//窗体居中类
public class WindowsCenter {
public WindowsCenter() {
}
public static void Center(JFrame frame){
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
frame.show();
}
}
//将这三个文件分别编译后执行
第二个文件名是Frame1.java,用命令javac Frame1.java可以编译,命令java Frame1.class可以执行
热心网友
时间:2024-12-12 15:00
在点击事件里 FRAME F = new frame();this.setVisible(true); OK啦
热心网友
时间:2024-12-12 15:00
你可以 添加两个FRAME
为按钮设置个监听
当点击按钮时Frame2的setVisible(true);