我正在嘗試完成庫存管理器的任務。它實現了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中沒有新文本字段的情況下打開。那麼,是否有可能在事件處理程序中添加新的文本字段?如果是這樣,請告訴我如何!
在添加文本字段之前,您是否嘗試調用'revalidate()'?你必須讓窗口知道它需要更新它的佈局。 –
您確定要做所有這些組件可見性的交換嗎?它看起來非常複雜,看起來像是在以一種非常複雜的方式做某件事情,而用CardLayout或不同的程序結構可以輕鬆完成。 –
我一直在閱讀這裏的東西,CardLayout已經出現了好幾次,但這不是我們在課程中學到的東西,所以我一直在避免它。也許我應該停止避免它... – tylerandrewandersen