我在我的university.I項目工作本學期取得了3種方法,直到我說JButtons
。我叫他們通過這些buttons.These方法從等待輸入這是工作的罰款用戶(我使用該掃描儀和我也都試過BufferReader
),但現在的程序只是停止運行,我不能在terminal.Also寫我沒有得到任何來自BlueJ.Here錯誤的是代碼所述JButtons
:Jbutton將 - 不能給輸入
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種methods.These方法出現該問題,在您的時間主要class.Thanks存在!
嗯,首先,你的IDE(Netbeans,Eclipse,IntelliJ)在停止運行時說什麼?如果沒有發生任何事情,它會以退貨0退出嗎?一切都很好? – Shiki
我在BlueJ.It工作停止運行,當我在第一Scanner.nextLine(調用addHotel()),但我應該給輸入,但我不能在terminal.Also寫我不能打電話任何其他方法。 –