我使用JFrame
创建了一个GUI,其中包含JComboBox
个JTextFields
。当我在JComboBox
中选择一个项目时,文本字段会更改其值,我认为可以更改它们。问题是,当我选择一个项目并更改一个值时,组合框会被某个项目卡住而我无法从组合框中选择另一个项目。 (下拉菜单不会显示我按下了多少)。这是GUI类,ActionListener
类只调用GUI类中的方法。
我可以使用静态方法和变量吗?
package GUI;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.Toolkit;
import java.util.HashMap;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import GUIListeners.CloseListener;
import GUIListeners.RoleBoxListener;
import GUIListeners.SaveEditListener;
import Simulation.Project;
public class EditProjectGUI extends JFrame {
private static Project project;
private JLabel editLabel, selectRole, savedLabel;
private static JComboBox roleBox;
private JLabel preConceptLabel, conceptLabel, projectLabel, preRampLabel, rampLabel, highVolumeLabel, preEOLLabel, EOLLabel;
private static JTextField preConceptText, conceptText, projectText, preRampText, rampText, highVolumeText, preEOLText, EOLText;
private JButton saveButton, cancelButton;
public EditProjectGUI(Project project) {
this.editLabel = new JLabel("Edit Projects Details");
editLabel.setFont(new Font("sansserif", Font.BOLD, 20));
editLabel.setForeground(new Color(83, 142, 213));
selectRole = new JLabel("Select role: ");
this.project = project;
saveButton = new JButton("Save");
saveButton.addActionListener(new SaveEditListener());
cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new CloseListener(this));
savedLabel = new JLabel("Not saved");
savedLabel.setForeground(new Color(192, 80, 77));
setTitle("Edit Projects Data");
int xSize = 400;
int ySize = 300;
setSize(xSize, ySize);
setVisible(true);
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension currentScreen = kit.getScreenSize();
this.setBounds(currentScreen.width / 2 - xSize / 2, currentScreen.height / 2 - ySize / 2, xSize, ySize);
setLayout(new BorderLayout());
add(editLabel, "North");
String[] roles = { "OPM", "MM", "SE", "QM", "SCP" };
roleBox = new JComboBox(roles);
roleBox.addActionListener(new RoleBoxListener(this));
Panel centerPanel = new Panel();
centerPanel.setLayout(new GridLayout(9, 2));
preConceptLabel = new JLabel("Pre-Concept: ");
conceptLabel = new JLabel("Concept: ");
projectLabel = new JLabel("Project: ");
preRampLabel = new JLabel("Pre-Ramp: ");
rampLabel = new JLabel("Ramp: ");
highVolumeLabel = new JLabel("High Volume: ");
preEOLLabel = new JLabel("Pre-EoL: ");
EOLLabel = new JLabel("EoL: ");
preConceptText = new JTextField();
conceptText = new JTextField();
projectText = new JTextField();
preRampText = new JTextField();
rampText = new JTextField();
highVolumeText = new JTextField();
preEOLText = new JTextField();
EOLText = new JTextField();
centerPanel.add(selectRole);
centerPanel.add(roleBox);
centerPanel.add(preConceptLabel);
centerPanel.add(preConceptText);
centerPanel.add(conceptLabel);
centerPanel.add(conceptText);
centerPanel.add(projectLabel);
centerPanel.add(projectText);
centerPanel.add(preRampLabel);
centerPanel.add(preRampText);
centerPanel.add(rampLabel);
centerPanel.add(rampText);
centerPanel.add(highVolumeLabel);
centerPanel.add(highVolumeText);
centerPanel.add(preEOLLabel);
centerPanel.add(preEOLText);
centerPanel.add(EOLLabel);
centerPanel.add(EOLText);
add(centerPanel, "Center");
Panel southPanel = new Panel();
southPanel.setLayout(new FlowLayout());
southPanel.add(saveButton);
southPanel.add(cancelButton);
southPanel.add(savedLabel);
add(southPanel, "South");
updateTextField(this);
}
public static void updateTextField(JFrame frame) {
String role = (String) roleBox.getSelectedItem();
HashMap<String, Double> map = project.getMap(role);
preConceptText.setText(map.get("Pre-Concept").toString());
conceptText.setText(map.get("Concept").toString());
projectText.setText(map.get("Project").toString());
preRampText.setText(map.get("Pre-Ramp").toString());
rampText.setText(map.get("Ramp").toString());
highVolumeText.setText(map.get("High Volume").toString());
preEOLText.setText(map.get("Pre-EoL").toString());
EOLText.setText(map.get("EoL").toString());
frame.validate();
}
public static void save() {
String role = (String) roleBox.getSelectedItem();
HashMap<String, Double> map = new HashMap<String, Double>();
map.put("Pre-Concept", new Double(preConceptText.getText()));
map.put("Concept", new Double(conceptText.getText()));
map.put("Project", new Double(projectText.getText()));
map.put("Pre-Ramp", new Double(preRampText.getText()));
map.put("Ramp", new Double(rampText.getText()));
map.put("High Volume", new Double(highVolumeText.getText()));
map.put("Pre-EoL", new Double(preEOLText.getText()));
map.put("EoL", new Double(EOLText.getText()));
project.setMap(map, role);
}
}