在按钮上单击打开一个新的jframe [英] open a new jframe on a button click

查看:239
本文介绍了在按钮上单击打开一个新的jframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用eclipse,正在创建一个具有两个jframe的Swing应用程序,一个是Login,另一个是欢迎.我想在单击登录jframe的提交按钮时打开欢迎jframe.两个jframe都在同一个包中

I am using eclipse and i am creating a Swing application which have two jframe, one is Login and second is welcome. i want to open the welcome jframe on clicking the submit button of login jframe. both the jframe are within the same package

推荐答案

Kapilkp,

在同一时间,我做了类似的事情.

在登录JFrame中,您必须将关闭操作设置为:

Hi Kapilkp,

Same time ago, I did something like that.

In login JFrame you must set the close operation as:

f.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);



然后,当您单击按钮时,就已经放置了该框架:



And when you click on the button, you have dispose this frame:

dispose();




并创建一个存储第二个JFrame的类的新实例




and create a new instance of your class that store the second JFrame

dispose();
JSecondFrame secondFrame = new JSecondFrame();



希望对您有帮助

最好的问候,
菲利普·马克斯

-更新-



I hope this helps you

Best regards,
Filipe Marques

--UPDATE--

// MyLogin.java
import javax.swing.*;
import java.awt.event.*;
public class MyLogin {
	private JFrame f = new JFrame("Login");
	private JButton bok = new JButton("OK");
	
	public MyLogin() {
	
		f.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		f.getContentPane().add(bok);
		
		bok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				f.dispose();
				new SecondFrame();
			}
		});
		f.setSize(100,100);
		f.setVisible(true);
	}
	
	public static void main(String[] args) {
		new MyLogin();
	}
}

// SecondFrame.java
import javax.swing.*;
public class SecondFrame {
	private JFrame f = new JFrame("Second");
	
	public SecondFrame() {
	
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setSize(300,300);
		f.setVisible(true);
	}
}



登录JFrame只是一个按钮,当您单击它时,将放置登录窗口并显示第二个窗口.



The login JFrame is only a button and when you click it, the login window is disposed and the second window appears.


//第一个JFrame

导入javax.swing.*;
导入java.awt.event.*;

公共类Pro实现ActionListener
{
JFrame f1 = new JFrame("Log In");
JLabel l1,l2;
JTextField t1,t2;
JButton b1;

Pro()
{
l1 = new JLabel(用户名");
l2 = new JLabel("Password");
t1 = new JTextField(20);
t2 = new JTextField(20);
b1 = new JButton("OK");

f1.setSize(400,400);
f1.setVisible(true);
f1.setLayout(null);

f1.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
f1.add(l1);
f1.add(t1);
f1.add(l2);
f1.add(t2);
f1.getContentPane().add(b1);

l1.setBounds(0,30,100,30);
t1.setBounds(110,30,100,30);
l2.setBounds(0,70,100,30);
t2.setBounds(110,70,100,30);
b1.setBounds(0,120,100,30);

b1.addActionListener(this);
}

公共无效actionPerformed(ActionEvent e)
{
f1.dispose();
Project1 p2 = new Project1();
}

公共静态void main(String ag [])
{
Pro p1 = new Pro();
}
}

//第二个JFrame
导入javax.swing.*;

公共课程Project1
{
JFrame f1 = new JFrame("Log In1");

Project1()
{
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setVisible(true);
f1.setSize(400,400);
}
}
//First JFrame

import javax.swing.*;
import java.awt.event.*;

public class Pro implements ActionListener
{
JFrame f1=new JFrame("Log In");
JLabel l1,l2;
JTextField t1,t2;
JButton b1;

Pro()
{
l1=new JLabel("User Name");
l2=new JLabel("Password");
t1=new JTextField(20);
t2=new JTextField(20);
b1=new JButton("OK");

f1.setSize(400,400);
f1.setVisible(true);
f1.setLayout(null);

f1.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
f1.add(l1);
f1.add(t1);
f1.add(l2);
f1.add(t2);
f1.getContentPane().add(b1);

l1.setBounds(0,30,100,30);
t1.setBounds(110,30,100,30);
l2.setBounds(0,70,100,30);
t2.setBounds(110,70,100,30);
b1.setBounds(0,120,100,30);

b1.addActionListener(this);
}

public void actionPerformed(ActionEvent e)
{
f1.dispose();
Project1 p2=new Project1();
}

public static void main(String ag[])
{
Pro p1=new Pro();
}
}

//Second JFrame
import javax.swing.*;

public class Project1
{
JFrame f1=new JFrame("Log In1");

Project1()
{
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setVisible(true);
f1.setSize(400,400);
}
}


这篇关于在按钮上单击打开一个新的jframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆