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 楼答案
怎么了
在Java中,一个文件中不能有两个主要方法
节目
下面是一个改变windows的演示程序
了解上述程序
StartHere类有一个main方法。它只是用来调用你喜欢的第一个窗口。我甚至可以使用
new Second()
调用Second第一个和第二个是相似的代码
它们都有按钮。在每个按钮(或JButton)上,我都添加了一个名为
addActionListner(this)
的方法。这个方法触发一个ActionEvent,正如您在第二个类中看到的那样,它被actionPerformed方法捕获。此方法在功能接口,ActionListener中声明。在第二个类中传递的'this'告诉您代码中的actionPerformed方法在哪里。该参数是一个ActionListener。因此,您必须为定义actionPerformed的类实现ActionListener奖金
第一个类似乎没有遵循上述规范。我通过了一个奇怪的语法。这是Java8中包含的一个新特性
请参阅有关Lambda Expressions的Oracle教程