我正在尝试完成作为库存管理员的作业。它实现了三种不同类别的CRUD(创建,检索,更新和删除)功能 - book,cd,dvd。我试图通过使用JComboBox来允许用户选择他们想要的CRUD选项,然后在JTextField中输入相应的信息。对于创建选项,我希望用户输入类别并使用新的JTextField打开一个新的JFrame,他们可以输入作者/艺术家/标题。
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中打开而没有新的文本字段。那么,是否可以在事件处理程序中添加新的文本字段?如果是的话,请告诉我如何!!