Java JButtons可以';我不能提供意见
这学期我在我的大学里做一个项目。在我添加Java JButtons可以';我不能提供意见,java,input,Java,Input,这学期我在我的大学里做一个项目。在我添加JButtons之前,我已经制作了3种工作正常的方法。我通过这些按钮调用它们。这些方法正在等待用户的输入(我使用Scanner,我也尝试了BufferReader)但是现在程序停止运行,我无法在终端上写东西。而且我没有从BlueJ收到任何类型的错误。下面是JButtons的代码: import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Window ex
JButtons
之前,我已经制作了3种工作正常的方法。我通过这些按钮调用它们。这些方法正在等待用户的输入(我使用Scanner,我也尝试了BufferReader
)但是现在程序停止运行,我无法在终端上写东西。而且我没有从BlueJ收到任何类型的错误。下面是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个方法上。这些方法存在于主类中。谢谢您的时间 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
中。
始终考虑发布< /P>
编辑
发布的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();
}
}
}
输出按预期显示在控制台上:
提供酒店名称:皇冠酒店
皇冠酒店
首先,您的IDE(Netbeans、Eclipse、IntelliJ)在“停止运行”时会说什么?它是否会以返回0的方式退出,就像什么都没发生一样,一切正常?我在BlueJ中工作。当我在第一个Scanner.nextLine()调用addHotel()时,它会停止运行。我应该在那里输入,但我不能在终端上写入。我也不能调用任何其他方法。我可以在哪里发布新代码?(这是我第一次发布)您已经发布了代码。请编辑并添加刚刚发布的
Main
的最小版本。我希望它能帮助您,谢谢您的时间!!我在BlueJ工作。这有可能是BlueJ的错吗?因为我明白了:用我发布的简单代码。我就是不能马上写。