当这个程序运行时,它应该在我输入之后列出数字,但它只输入第一个我放的5,我缺少什么
public class Numbers extends JFrame
{
private JTextField textField;
private JTextArea textArea, displayArea, finalArea;
private JPanel controlPanel, bottomPanel, southPanel, displayPanel, displayFinal;
private JButton enter, finalNumbers;
private String input;
private int intInput;
private int[] array = new int[5];
private int entered = 0;
public static void main(){
Numbers myFrame = new Numbers();
myFrame.setSize(600,600);
myFrame.setTitle("Numbers between 10-100 by Daniel Bendlin");
myFrame.createGUI();
myFrame.pack();
myFrame.setVisible(true);
}
public void createGUI()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new BorderLayout());
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
bottomPanel = new JPanel();
bottomPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
southPanel = new JPanel();
displayPanel = new JPanel();
textArea = new JTextArea("Please enter 5 numbers between 10-100");
textArea.setBackground(Color.white);
displayArea = new JTextArea("");
finalArea = new JTextArea("");
textField = new JTextField(2);
textField.setBackground(Color.white);
enter = new JButton("Enter");
enter.setBackground(Color.GREEN);
enter.addActionListener(new EnterNum());
finalNumbers = new JButton("Display Final Numbers");
finalNumbers.addActionListener(new DisplayNum());
controlPanel.add(textArea);
southPanel.add(textField);
southPanel.add(enter);
displayPanel.add(displayArea);
displayPanel.add(finalNumbers);
displayPanel.add(finalArea);
window.add(controlPanel,BorderLayout.NORTH);
window.add(southPanel,BorderLayout.CENTER);
window.add(displayPanel,BorderLayout.SOUTH);
textField.requestFocusInWindow();
}
private class EnterNum implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
input = textField.getText();
intInput = Integer.parseInt(input);
while (entered < array.length){
try{
if((intInput >= 10) && (intInput <= 100)){
for(int i = 0; i < array.length; i++){
array[i] = intInput;
entered = entered + 1;
textField.setText("");
displayArea.setText("Entered number(s)..." + array[i]);
}
}else{
displayArea.setText("Input numbers that range between 10 and 100");
}
}catch (NumberFormatException x){displayArea.setText("\"" + textField.getText() + "\" is not a legal number.");
textField.selectAll();
textField.requestFocus();
}
}
}
}
private class DisplayNum implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String intInput = String.valueOf(array[0]);
finalArea.setText("\n" + intInput);
}
}
}
答案 0 :(得分:1)
您的代码存在一些问题。我不确定是否可以列出所有这些,首先,如果您希望将代码作为Java应用程序运行,则主要方法应该采用String数组参数,即
public static void main(String[] args){
}
其次,您的while
方法中没有for
和actionPerformed(ActionEvent e)
循环。
DisplayNum类中的actionPerformed(ActionEvent e)方法仅显示数组的第一个元素。也许你想连接所有5个数字并显示?无论如何,这是带有修改的代码:
public class Numbers extends JFrame {
private JTextField textField;
private JTextArea textArea, displayArea, finalArea;
private JPanel controlPanel, bottomPanel, southPanel, displayPanel,
displayFinal;
private JButton enter, finalNumbers;
private String input;
private int intInput;
private int[] array = new int[5];
private int entered = 0;
public static void main(String[] args) {
Numbers myFrame = new Numbers();
myFrame.setSize(600, 600);
myFrame.setTitle("Numbers between 10-100 by Daniel Bendlin");
myFrame.createGUI();
myFrame.pack();
myFrame.setVisible(true);
}
public void createGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new BorderLayout());
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
bottomPanel = new JPanel();
bottomPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
southPanel = new JPanel();
displayPanel = new JPanel();
textArea = new JTextArea("Please enter 5 numbers between 10-100");
textArea.setBackground(Color.white);
displayArea = new JTextArea("");
finalArea = new JTextArea("");
textField = new JTextField(2);
textField.setBackground(Color.white);
enter = new JButton("Enter");
enter.setBackground(Color.GREEN);
enter.addActionListener(new EnterNum());
finalNumbers = new JButton("Display Final Numbers");
finalNumbers.addActionListener(new DisplayNum());
controlPanel.add(textArea);
southPanel.add(textField);
southPanel.add(enter);
displayPanel.add(displayArea);
displayPanel.add(finalNumbers);
displayPanel.add(finalArea);
window.add(controlPanel, BorderLayout.NORTH);
window.add(southPanel, BorderLayout.CENTER);
window.add(displayPanel, BorderLayout.SOUTH);
textField.requestFocusInWindow();
}
private class EnterNum implements ActionListener {
public void actionPerformed(ActionEvent e) {
input = textField.getText();
intInput = Integer.parseInt(input);
if (entered < array.length) {
try {
if ((intInput >= 10) && (intInput <= 100)) {
array[entered] = intInput;
textField.setText("");
displayArea.setText("Entered number(s)..."
+ array[entered]);
entered = entered + 1;
} else {
displayArea
.setText("Input numbers that range between 10 and 100");
}
} catch (NumberFormatException x) {
displayArea.setText("\"" + textField.getText()
+ "\" is not a legal number.");
textField.selectAll();
textField.requestFocus();
}
}
}
}
private class DisplayNum implements ActionListener {
public void actionPerformed(ActionEvent e) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < array.length; i++) {
sb.append(String.valueOf(array[i]));
if(i < array.length){
sb.append(",");
}
}
finalArea.setText("\n" + sb.toString());
}
}
}
答案 1 :(得分:0)
在您的DisplayNum课程中,您只显示array[0]
。如果要显示多个,则需要使用某种循环:
for(int i = 0; i < array.length; i++) {
// Do stuff with array[i] instead of array[0] here
}
答案 2 :(得分:0)
您的数组大小设置为5,因此只能存在五个元素。
答案 3 :(得分:0)
String intInput = String.valueOf(array [0]);
在执行方法操作时,你已经传递了数组[o],我认为你应该使用数组。
答案 4 :(得分:0)
lass DisplaYEnum方法不正确。您没有正确创建displayArray。
见下文。
private class DisplayNum implements ActionListener {
public void actionPerformed(ActionEvent e) {
String intInput = "";
for (int i = 0; i < array.length; i++) {
intInput = intInput.concat(" " + String.valueOf(array[i]));
}
finalArea.setText("\n" + intInput);
}
}