我这个学期正在我大学的项目上工作。我已经制作了3种方法,这些方法工作正常,直到我添加JButtons
。我通过这些按钮调用它们。这些方法正在等待用户输入(i使用扫描仪,我也试过BufferReader
)但现在程序停止运行,我不能在终端写。也没有从BlueJ.Here得到任何类型的错误代码对于JButtons
:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Window extends JFrame implements ActionListener {
JButton b1 = new JButton("Load from file");
JButton b2 = new JButton("Save to file");
JButton b3 = new JButton("Add hotel and resrvations");
JButton b4 = new JButton("Search hotel by id");
JButton b5 = new JButton("Search reservation by name");
Main m0 = new Main();
public Window(Main main0) {
super("Management");//Name
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
FlowLayout flow = new FlowLayout();
setLayout(flow);
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
pack();
setVisible(true);
m0 = main0;
}
public void actionPerformed(ActionEvent ae) {
Object source = ae.getSource();
if(source == b1) {
m0.loadFile();
}else if(source == b2) {
m0.saveFile();
}else if(source == b3) {
m0.addHotel();
}else if(source == b4) {
m0.searchById();
}else if(source == b5) {
m0.searchByName();
}
}}
更简化的代码导致同样的问题
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Window extends JFrame implements ActionListener {
JButton b1 = new JButton("Get input");
Main m0 = new Main();
public Window(Main main0) {
super("Management");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b1.addActionListener(this);
FlowLayout flow = new FlowLayout();
setLayout(flow);
add(b1);
pack();
setVisible(true);
m0 = main0;
}
public void actionPerformed(ActionEvent ae) {
Object source = ae.getSource();
if(source == b1) {
m0.userInput();
}
}
}
一个简单的主要
import java.io.*;
import java.util.*;
public class Main{
public void userInput(){
Scanner scan = new Scanner(System.in);
System.out.println("Give hotel's name : ");
String name = scan.nextLine();
System.out.println(name);
}
public static void main(){
Main m0 = new Main();
Window w = new Window(m0);
}
}
问题发生在最后3种方法。这些方法存在于主类。谢谢你的时间!
答案 0 :(得分:1)
gui工作正常:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Window extends JFrame implements ActionListener {
JButton b1 = new JButton("Load from file");
JButton b2 = new JButton("Save to file");
JButton b3 = new JButton("Add hotel and resrvations");
JButton b4 = new JButton("Search hotel by id");
JButton b5 = new JButton("Search reservation by name");
Main m0 ;
public Window(Main main0) {
super("Management");//Name
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
FlowLayout flow = new FlowLayout();
setLayout(flow);
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
pack();
setVisible(true);
m0 = main0;
}
@Override
public void actionPerformed(ActionEvent ae) {
Object source = ae.getSource();
if(source == b1) {
m0.loadFile();
}else if(source == b2) {
m0.saveFile();
}else if(source == b3) {
m0.addHotel();
}else if(source == b4) {
m0.searchById();
}else if(source == b5) {
m0.searchByName();
}
}
public static void main(String[] args) {
new Window(new Main());
}
}
class Main {
public void loadFile() {
System.out.println("loadFile pressed");
}
public void searchByName() {
System.out.println("searchByName pressed");
}
public void searchById() {
System.out.println("searchById() pressed");
}
public void addHotel() {
System.out.println("addHotel pressed");
}
public void saveFile() {
System.out.println("saveFile pressed");
}
}
所以问题必须在未发布的Main
中。
始终考虑发布mcve
<强> 修改 强> 发布的mcve也很好用:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main{
public void userInput(){
Scanner scan = new Scanner(System.in);
System.out.println("Give hotel's name : ");
String name = scan.nextLine();
System.out.println(name);
}
public static void main(String[] args){ //added String[] args
Main m0 = new Main();
Window w = new Window(m0);
}
}
class Window extends JFrame implements ActionListener {
JButton b1 = new JButton("Get input");
Main m0 = new Main();
public Window(Main main0) {
super("Management");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b1.addActionListener(this);
FlowLayout flow = new FlowLayout();
setLayout(flow);
add(b1);
pack();
setVisible(true);
m0 = main0;
}
@Override
public void actionPerformed(ActionEvent ae) {
Object source = ae.getSource();
if(source == b1) {
m0.userInput();
}
}
}
输出按预期显示在 控制台 上:
提供酒店名称:
皇冠酒店(The Crown Hotel)