从屏幕调用不同的屏幕直通按钮

时间:2016-11-14 04:37:12

标签: java listener

您好我想问一下如何从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 个答案:

答案 0 :(得分:0)

有什么问题?

Java中的单个文件中不能有两个主要方法。

<强>程序

这是一个改变窗口的演示程序。

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

<强>加成

First 类似乎没有遵循上述规范。我传递了一种奇怪的语法。这是Java 8中包含的新功能。

请参阅此Oracle教程,了解Lambda Expressions

相关问题