JAVA怎么写代码使一个窗口打开另一个窗口
需要做一个客户端程序从一个窗口点击按钮进入到另一个窗口,就是登录界面,注册界面和主界面,现在不知道,怎么从登录界面转到注册界面,注册后返回登陆界面,或从登陆界面登陆后到主...
需要做一个客户端程序从一个窗口点击按钮进入到另一个窗口,就是登录界面,注册界面和主界面,现在不知道,怎么从登录界面转到注册界面,注册后返回登陆界面,或从登陆界面登陆后到主界面,求JAVA高手提供代码提示。
展开
展开全部
1、首先,我们需要在代码中导入相应的包,以便能够使用 JFrame 类。然后,新建一个窗口类继承自 JFrame 类。
2、在窗口类中创建一个初始化方法,我们需要在该方法中初始化窗口类对象,并将其显示出来。
3、对窗口对象进行初始化时,我们先设置好窗口的标题。
4、再设置窗口的大小,参数分别为窗口的长和宽,单位是像素。
5、接着设置窗口左上角的坐标位置,以确定窗口的位置。参数分别为窗口左上角顶点的 x 坐标和 y 坐标。
6、最后,调用 setVisible 方法将窗口显示出来。参数为 true 表示显示,为 false 表示隐藏。
7、窗口类写好后,我们在 main 方法中创建一个窗口类对象,然后调用该对象的初始化方法就可以将窗口显示出来了。
展开全部
要写两个窗体,第一个窗体:
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) {
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
提供一个思路
//登录窗口
class Login extends JFrame{
JButton register;
JButton sure;
register.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
this.setVisible(false);
new RegisterFrame(this);
}
});
sure.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
this.setVisible(false);
new MainFrame(this);}
});
}
//注册窗口
class RegisterFrame extends JFrame{
////省略主要内容
Login login;
RegisterFrame(Login l){
login = l;
}
JButton goback;
goback.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
this.setVisible(false);
login.setVisible(true);}
});
}
//主窗口
class MainFrame extends JFrame{
////省略主要内容
Login login;
MainFrame(Login l){
login = l;
}
JButton goback;
goback.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
this.setVisible(false);
login.setVisible(true);}
});
}
//登录窗口
class Login extends JFrame{
JButton register;
JButton sure;
register.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
this.setVisible(false);
new RegisterFrame(this);
}
});
sure.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
this.setVisible(false);
new MainFrame(this);}
});
}
//注册窗口
class RegisterFrame extends JFrame{
////省略主要内容
Login login;
RegisterFrame(Login l){
login = l;
}
JButton goback;
goback.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
this.setVisible(false);
login.setVisible(true);}
});
}
//主窗口
class MainFrame extends JFrame{
////省略主要内容
Login login;
MainFrame(Login l){
login = l;
}
JButton goback;
goback.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
this.setVisible(false);
login.setVisible(true);}
});
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询