有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从屏幕调用不同的screen thru按钮

你好,我想问一下如何从MainScreen调用我的主菜单屏幕?请再解释一下关于Listener的更多细节

下面是我准备好的代码:

public class MainScreen {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        frame.add(panel);
        placeComponents(panel);
        frame.setVisible(true);
    }
    private static void placeComponents(JPanel panel) {
        JLabel WelcomeNote = new JLabel("Welcome");
        panel.add(WelcomeNote);
        JButton Start = new JButton("Start");
        panel.add(Start);
        //Insert action for Start button here
    }
}


public class MainMenu {
    public static void main(String[] args){
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        frame.add(panel);
        placeComponents(panel);
        frame.setVisible(true);
    }
    private static void placeComponents(JPanel panel) {
        JLabel menuLbl = new JLabel("Main Menu");
        panel.add(menuLbl);
    }
}

共 (1) 个答案

  1. # 1 楼答案

    怎么了

    在Java中,一个文件中不能有两个主要方法


    节目

    下面是一个改变windows的演示程序

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    
    class First extends JFrame
    {
        JLabel jlb = new JLabel("Label in First Window");
        JButton jb = new JButton("Next Window");
        First()
        {
            super("First Windows");
            //Set this frame
            this.setSize(350,250);
            this.setLayout(null);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //Setting size of components
            jlb.setBounds(10,10,200,40);
            jb.setBounds(10,120,150,40);
            add(jlb);
            add(jb);
    
            jb.addActionListener((e)->{
                this.setVisible(false);
                new Second();
            });
    
            setVisible(true);
        }
    }
    
    class Second extends JFrame implements ActionListener
    {
        JLabel jlb = new JLabel("Label in Second Window");
        JButton jb = new JButton("Prev. Window");
        Second()
        {
            super("Second Window");
            this.setSize(350,250);
            this.setLayout(null);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //Setting size of components
            jlb.setBounds(10,10,200,40);
            jb.setBounds(10,120,150,40);
            add(jlb);
            add(jb);
    
            jb.addActionListener(this);
    
            setVisible(true);
        }
    
        public void actionPerformed(ActionEvent e)
        {
            this.setVisible(false);
            new First();
        }
    }
    
    class StartHere
    {
        public static void main(String[] args) {
            Runnable r = ()->{
                new First();
            };
            r.run();
        }
    }
    

    了解上述程序

    StartHere类有一个main方法。它只是用来调用你喜欢的第一个窗口。我甚至可以使用new Second()调用Second

    第一个和第二个是相似的代码
    它们都有按钮。在每个按钮(或JButton)上,我都添加了一个名为addActionListner(this)的方法。这个方法触发一个ActionEvent,正如您在第二个类中看到的那样,它被actionPerformed方法捕获。此方法在功能接口ActionListener中声明。在第二个类中传递的'this'告诉您代码中的actionPerformed方法在哪里。该参数是一个ActionListener。因此,您必须为定义actionPerformed的类实现ActionListener


    奖金

    第一个类似乎没有遵循上述规范。我通过了一个奇怪的语法。这是Java8中包含的一个新特性

    请参阅有关Lambda Expressions的Oracle教程