我正在大学的这个学期工作。我做了3种方法,直到我添加JButtons为止,这些方法都可以正常工作。我通过这些按钮调用它们。这些方法正在等待用户的输入(我使用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个方法中。这些方法存在于Main类中,谢谢您的时间!!

最佳答案

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();
        }
    }
}


输出按预期出现在控制台上:


  提供酒店的名称:皇冠酒店皇冠酒店

10-07 13:49