gui运作不正常

时间:2014-05-06 23:57:24

标签: java user-interface arraylist

我得到一个干净的编译,但当我运行它并输入一个名称并单击“添加”它只是给我错误消息“无效的条目”,它应该只是添加到arraylist,就是这样。发生了什么事?

 import javax.swing.*;
 import java.awt.*;
 import java.awt.event.*;
 import java.util.*;

 public class FinalB extends JFrame{
 private static int WIDTH = 500;
 private static int HEIGHT = 250;
 private static String nameEntered;
 private static ArrayList<String> list = new ArrayList<String>();

 private int row = 20;

 private JTextField textF = new JTextField(20);
 private JButton addB, removeB, searchB, printB;
 private JButton exitB;
 private ButtonEventHandler eventHandler;

 public FinalB(){
 setTitle("Very Important List");
 setSize(WIDTH,HEIGHT);

 Container pane = getContentPane();

 exitB = new JButton("Exit");
 addB = new JButton("Add");
 removeB = new JButton("Remove");
 searchB = new JButton("Search");
 printB = new JButton("print list");

 JLabel label = new JLabel("Enter a Name:");
  label.setFont(new Font("Arial", Font.BOLD, 18));
  pane.add(label);
  label.setLocation(0,50);
  label.setSize(200, 30);
  label.setForeground(Color.BLACK);

 eventHandler = new ButtonEventHandler();
 exitB.addActionListener(eventHandler);
 addB.addActionListener(eventHandler);
 removeB.addActionListener(eventHandler);
 searchB.addActionListener(eventHandler);
 printB.addActionListener(eventHandler);

 pane.setLayout(null); //sets pane to null

 //set the buttons (500,250)
 exitB.setLocation(125,150);
 addB.setLocation(125,100);
 removeB.setLocation(205,100);
 searchB.setLocation(300,100);
 printB.setLocation(303,150);
 textF.setLocation (125,50);

 //set size of buttons
 exitB.setSize(90, 30);
 addB.setSize(75, 30);
 removeB.setSize(90, 30);
 searchB.setSize(90, 30);
 printB.setSize(90, 30);
 textF.setSize(268,30);

 //add them to the pane
 pane.add(exitB);
 pane.add(addB);
 pane.add(removeB);
 pane.add(searchB);
 pane.add(printB);
 pane.add(textF);

 setVisible(true);
 setDefaultCloseOperation(EXIT_ON_CLOSE);
}//ends constructor

private class ButtonEventHandler implements ActionListener{
 public void actionPerformed(ActionEvent e){
  String text = textF.getText();
  if(e.getActionCommand().equals("Add")){
   list.add(text);
  }
  else{
   JOptionPane.showMessageDialog(new JFrame(),"invalid entry","invalid entry",JOptionPane.ERROR_MESSAGE);
  }
  if(e.getActionCommand().equals("Remove")){
   list.remove(text);
  }
  else{
   JOptionPane.showMessageDialog(new JFrame(),"invalid entry","invalid entry",JOptionPane.ERROR_MESSAGE);
  }
  if(e.getActionCommand().equals("Print list")){ 
   JOptionPane.showMessageDialog(new JFrame(),list,"Printed list",JOptionPane.ERROR_MESSAGE);
  }
  else{
   JOptionPane.showMessageDialog(new JFrame(),"invalid entry","invalid entry",JOptionPane.ERROR_MESSAGE);
  }
  if(e.getActionCommand().equals("Search")){
   ArrayList<String> newList = new ArrayList<String>();
   for(String searchVal : list){
   if(searchVal.matches(text)){
   newList.add(searchVal);
   }
  }
 }

否则{      JOptionPane.showMessageDialog(新的JFrame(),“无效条目”,“无效条目”,JOptionPane.ERROR_MESSAGE);     }      if(e.getActionCommand()。equals(“Exit”))                      System.exit(0);

}   }

}

2 个答案:

答案 0 :(得分:2)

您的add命令运行正常,问题在于您的if语句......

if (e.getActionCommand().equals("Add")) {
    list.add(text);
} else {
    JOptionPane.showMessageDialog(new JFrame(), "invalid entry", "invalid entry", JOptionPane.ERROR_MESSAGE);
}
if (e.getActionCommand().equals("Remove")) {
    list.remove(text);
} else {
    JOptionPane.showMessageDialog(new JFrame(), "invalid entry", "invalid entry", JOptionPane.ERROR_MESSAGE);
}
if (e.getActionCommand().equals("Print list")) {
    JOptionPane.showMessageDialog(new JFrame(), list, "Printed list", JOptionPane.ERROR_MESSAGE);
} else {
    JOptionPane.showMessageDialog(new JFrame(), "invalid entry", "invalid entry", JOptionPane.ERROR_MESSAGE);
}

基本上发生的是代码正在发生......

  • &#34;如果添加&#34; do list.add ...
  • &#34;如果删除&#34;,&#34;否则&#34;显示错误消息
  • &#34;如果打印&#34;,&#34;否则&#34;显示错误消息

所以,基本上,每个if分支都已执行,相反,你应该使用if-else语句......

if (e.getActionCommand().equals("Add")) {
    list.add(text);
} else if (e.getActionCommand().equals("Remove")) {
    list.remove(text);
} else if (e.getActionCommand().equals("Print list")) {
    JOptionPane.showMessageDialog(new JFrame(), list, "Printed list", JOptionPane.ERROR_MESSAGE);
} else {
    JOptionPane.showMessageDialog(new JFrame(), "invalid entry", "invalid entry", JOptionPane.ERROR_MESSAGE);
}

不要使用null布局。像素完美布局是现代UI设计中的一种幻觉,您无法控制字体,DPI,渲染管道或其他因素,这些因素将改变组件在屏幕上呈现的方式。

Swing旨在与布局管理员合作以克服这些问题。如果你坚持忽略这些功能并违背API设计,那就要做好准备应对很多令人头疼的事情,永远不要过时努力......

答案 1 :(得分:0)

我可能在这里错了,但是从袖口开始,list<String>看起来不正确 - 它应该是大写List<String>,因为java区分大小写。

作为快速的第二个注释,如果对话框可以采用数组列表而不仅仅是字符串,我会感到惊讶。您可能希望调查发送Arrays.toString(your_array_list.toArray()),因为它会为您提供一个真实的字符串进行打印。

我承认自己并没有阅读所有代码;这只是一个快速的答案:)