“切换场景"在Java Swing中 [英] "Switch Scenes" in Java Swing

查看:75
本文介绍了“切换场景"在Java Swing中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不是问题.

我没有在互联网上找到任何有关它的信息,所以我尝试自己做,我有一个很好的主意,我想与以后搜索它的人分享.

I didn't find anything about it on the internet, so I tried to do it myself and I had a pretty good idea that I want to share for the people who search it in the future.

当您在Google上切换Java场景"时,它说:使用JavaFX切换场景",但是我不想使用JavaFX并继续使用Java Swing.

When you google "Switch scenes Java" it says: "Switch scenes using JavaFX", but I don't want to work with JavaFX and stay with Java Swing.

然后我有了一个绝妙的主意,下面将向您展示:(我不是专业的程序员,也不是其他任何人,我16岁,还是小学生,所以我的代码不会很完美)

Then I had that brilliant idea, which I want to show you in the following: (Im not a professional programmer or anything, Im 16 years old and a pupil, so my code won't be perfect)

这个想法是使用多个JFrame并设置相对于彼此的位置.然后将未运行的JFrame设置为visible(false)并对其进行dispose().

The idea is to use multiple JFrames and set the location relative to eachother. Then set the not running JFrame to visible(false) and dispose() it.

这是我写的代码:

  1. 为第一个JFrame和ActionListener创建类以在两个JFrame之间切换:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class mainTest {
 
    public static JFrame frame1;

    JLabel hello1;

    JButton b1;
    
    
    static Handler handler;
    
    
    mainTest(){
        frame1 = new JFrame();
        frame1.setSize(900,600);
        frame1.setResizable(false);
        frame1.setTitle("Frame 1");
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame1.setLocationRelativeTo(GUI2.frame2);
        frame1.setLayout(null);
        frame1.setVisible(true);
            
    
        handler = new Handler();
        
        hello1 = new JLabel("Hello Frame 1");
        hello1.setBounds(100,100,100,20);
        frame1.add(hello1);
        
        
        b1 = new JButton("Go to Frame 2");
        b1.setBounds(100,300,200,50);
        b1.addActionListener(handler);
        frame1.add(b1);
        
        
    }
    
    public class Handler implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            if(e.getSource() == b1) {
                
                new GUI2();
                
                frame1.setVisible(false);
                frame1.dispose();
                
            }if(e.getSource() == GUI2.b2) {
                
                
                new mainTest();
                
                
                GUI2.frame2.setVisible(false);
                GUI2.frame2.dispose();
            }
            
            
        }
        
    }
    
    public static void main(String args[]) {
        new mainTest();
    }
    
}

  1. 为第二个JFrame创建类:

mport javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class GUI2 {

    public static JFrame frame2;
    public static JButton b2;
    public JLabel hello2;
    
    GUI2(){
        
        frame2 = new JFrame();
        frame2.setSize(900,600);
        frame2.setResizable(false);
        frame2.setTitle("Frame 2");
        frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame2.setLocationRelativeTo(mainTest.frame1);
        frame2.setLayout(null);
        frame2.setVisible(true);
        
        b2 = new JButton("Got to Frame 1");
        b2.setBounds(100,300,200,50);
        b2.addActionListener(mainTest.handler);
        frame2.add(b2);
        
        hello2 = new JLabel("Hello Frame 2");
        hello2.setBounds(100,100,100,20);
        frame2.add(hello2);
    }
    
}


工作原理:

  1. 班级:

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