2015-05-19 44 views
0

我正在尝试完成库存管理器的任务。它实现了CRUD(创建,检索,更新和删除)三种不同类别的功能 - book,cd,dvd。我试图通过使用JComboBox来实现这一点,以允许用户选择他们想要的CRUD选项,然后在其中输入适当的信息的JTextField。对于创建选项,我希望用户输入类别并使用新的JTextField打开新的JFrame,以便他们可以输入作者/艺术家/标题。在事件处理程序中添加新的JTextField

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

public class GUIProductView extends JFrame 
{ 
    //private ProductController controller; 
    //private Product model; 

    private final JComboBox<String> dropDown; //drop down menu for multiple options 
    private final JTextField textField1; //text field for create option 
    private final JTextField textField2; //text field for retrieve option 
    private final JTextField textField3; //text field for update option 
    private final JTextField textField4; //text field for delete option 

    private final JTextField bookField; //text field for new book entry 
    private final JFrame newFrame; //new JFrame for when appropriate drop down option is selected 

    private static final String[] dropDownText = 
     {"1. Create", "2. Retrieve", "3. Update", "4. Delete"}; //text for the JComboBox options 

    public GUIProductView() 
    { 
     super("inventory manager"); 
     setLayout(new FlowLayout()); 

     dropDown = new JComboBox<String>(dropDownText); 
     dropDown.setMaximumRowCount(3); 
     textField1 = new JTextField("Choose an option from drop down list"); 
     textField2 = new JTextField(); 
     textField3 = new JTextField(); 
     textField4 = new JTextField(); 

     add(dropDown); 
     add(textField1); 
     bookField = new JTextField(); 
     newFrame = new JFrame(); 

     TextFieldHandler handler = new TextFieldHandler(); 
     textField1.addActionListener(handler); 

     dropDown.addItemListener(
       new ItemListener() 
       { 
        //handler for JComboBox 
        @Override 
        public void itemStateChanged(ItemEvent event) 
        { 
         JComboBox dropDown = (JComboBox) event.getSource(); 
         Object selected = dropDown.getSelectedItem(); 

         if(selected.toString().equals("1. Create")) 
         { 
          textField1.setText("Enter type of item to Create (book, cd or dvd"); 
          textField1.setVisible(true); 
          textField2.setVisible(false); 
          textField3.setVisible(false); 
          textField4.setVisible(false); 
         } 
         else if(selected.toString().equals("2. Retrieve")) 
         { 
          textField2.setText("Enter item name to Retrieve"); 
          add(textField2); 
          textField2.setVisible(true); 
          textField1.setVisible(false); 
          textField3.setVisible(false); 
          textField4.setVisible(false); 
         } 
         else if(selected.toString().equals("3. Update")) 
         { 
          textField3.setText("Enter item name to Update"); 
          add(textField3); 
          textField3.setVisible(true); 
          textField1.setVisible(false); 
          textField2.setVisible(false); 
          textField4.setVisible(false); 
         } 
         else 
         { 
          textField4.setText("Enter item name to Delete"); 
          add(textField4); 
          textField4.setVisible(true); 
          textField1.setVisible(false); 
          textField2.setVisible(false); 
          textField3.setVisible(false); 
         } 
        } 
       } 
      ); 
     } 
    private class TextFieldHandler implements ActionListener 
    { 
     String string = ""; 
     @Override 
     public void actionPerformed(ActionEvent event) 
     { 
      if(event.getSource() == textField1) 
      { 
       //create text field 
       //open new JFrame with text field for user to enter appropriate info 
       //creating three text fields where the user can choose 
       newFrame.setSize(250, 100); 
       newFrame.setVisible(true); 

       string = event.getActionCommand(); //variable to store the user input 

       //JTextField bookField = new JTextField(); 
       if (string.equals("book")) 
       { 
        add(bookField); 
        bookField.setVisible(true); 
        bookField.setText("enter author and title"); 
       } 
       if (string.equals("cd")) 
       { 
        JTextField cdField = new JTextField(); //text field for new cd entry 
        add(cdField); 
        cdField.setVisible(true); 
        cdField.setText("enter artist and album"); 
       } 
       if (string.equals("dvd")) 
       { 
        JTextField dvdField = new JTextField(); //text field for new dvd entry 
        add(dvdField); 
        dvdField.setVisible(true); 
        dvdField.setText("enter title"); 
       } 
      } 
      else if(event.getSource() == textField2) 
      { 
       //retrieve text field 
       //maybe use JOptionPane to display search result? 
      } 
      else if(event.getSource() == textField3) 
      { 
       //update text field 
       //new window with text field for new update info 
      } 
      else 
      { 
       //delete text field 
       //JOptionPane to show confirm message 
      } 
     } 
    } 

    public static void main(String[] args) 
    { 
     Product model = new Product(); 
     //ProductController controller = new ProductController(model); 

     GUIProductView view = new GUIProductView(); 
     view.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     view.setSize(350, 100); 
     view.setVisible(true); 

     //model.setView(view); 

     //view.setModel(model); 

     //view.start(); 
    } 
} 

我想我需要有4个单独的文本字段,因为我需要它们的功能不同。我试图添加事件监听器中的文本字段,但他们不显示。如果我将它们添加到事件处理程序之外,那么它可以工作,但它在首次运行时会给我4个单独的文本字段。我希望它看起来比第一次打开时更干净。另外,当新的JFrame在从文本字段接收输入后打开时,它将在该JFrame中没有新文本字段的情况下打开。那么,是否有可能在事件处理程序中添加新的文本字段?如果是这样,请告诉我如何!

+1

在添加文本字段之前,您是否尝试调用'revalidate()'?你必须让窗口知道它需要更新它的布局。 –

+1

您确定要做所有这些组件可见性的交换吗?它看起来非常复杂,看起来像是在以一种非常复杂的方式做某件事情,而用CardLayout或不同的程序结构可以轻松完成。 –

+0

我一直在阅读这里的东西,CardLayout已经出现了好几次,但这不是我们在课程中学到的东西,所以我一直在避免它。也许我应该停止避免它... – tylerandrewandersen

回答

-1

我建议
1.创建扩展的JFrame
2.添加两个字段另一个类:你的JTextField和指示动作的哪种类型的索引([1 4])。索引在构造函数中设置
3.将TextFieldHandler添加到此类,但是这次与索引配合使用
4.在GUI(您的主GUI)中:每次在下拉列表中选择一个元素时,都会隐藏GUI(只是一个选项),然后打开新的自定义JFrame
5.当客户单击“确定”或“取消”时,关闭JFrame并显示GUI
6.在GUI中,访问自定义JFrame类的成员以检索你需要的值。

相关问题