我有一个包含 3 个面板的 JPanel。面板的布局是 BorderLayout,3 个面板使用 BorderLayout.NORTH、.CENTER 和 .SOUTH 进行划分。设置为 .SOUTH 的面板称为“底部”。在底部有一个 textArea 应该根据用户从其上方面板中的组合框中选择的内容显示信息。然而,出于某种原因,我的文本区域没有更新信息。这个想法是,例如,当用户选择“山地自行车”时,文本区域将显示有关山地自行车的信息。然后,如果用户在此之后立即选择“公路自行车”,它将显示有关公路自行车的信息。出于某种原因,它没有这样做,而且我确定更新它是一个问题,因为我所有的类实例都是正确的,当我尝试为组合框选择不同的起始索引时,它们都在文本区域中显示了正确的信息。这是我的构造函数:
public MainGUI(){
System.out.print("Loading...");
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.add(main);
int frameSize = frame.getWidth();
instructionField.setColumns(frameSize);
instructionField.setEnabled(false);
instructionField.setDisabledTextColor(Color.RED);
instructionField.setText("Welcome to the Bike App! Please choose from the following bikes!");
Color backColor = frame.getBackground();
instructionField.setBackground(backColor);
main.add(instructionField, BorderLayout.NORTH);
main.add(middle, BorderLayout.CENTER);
main.add(bottom, BorderLayout.SOUTH);
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10,10,10,10);
gbc.gridx=0;
gbc.gridy=0;
middle.add(bikeComboBox, gbc);
submit = new JButton("Load Selected Bike");
gbc.gridx=1;
gbc.gridy=0;
middle.add(submit, gbc);
bikeComboBox.addActionListener(this);
submit.addActionListener(this);
//bottom is a JPanel
//HERE IS WHERE THE TEXT AREA CODE IS
informationTextArea = new JTextArea();
JScrollPane sp = new JScrollPane(informationTextArea);
informationTextArea.setEditable(false);
bottom.add(informationTextArea, BorderLayout.CENTER);
//bikeComboBox.setSelectedIndex(1);
selectedBikeIndex = bikeComboBox.getSelectedIndex();
selectedBikeName = bikeTypes[selectedBikeIndex];
String returnedInfo = loadSelectedBikeInfo(selectedBikeName);
informationTextArea.setText(returnedInfo);
informationTextArea.updateUI();
bottom.updateUI();
informationTextArea.setBackground(backColor);
frame.setSize(400,400);
}
这是 loadSelectedBikeInfo(String selectedBikeName) 方法:
public String loadSelectedBikeInfo(String selectedBikeName){
String s = selectedBikeName;
String selectedInfo = "";
if(s.equalsIgnoreCase(this.mounBikeType)){//load mountain bike info
selectedInfo = mountainBike.getBikeInfo();
}
if(s.equalsIgnoreCase(this.roadBikeType)){//load mountain bike info
selectedInfo = roadBike.getBikeInfo();
}
if(s.equalsIgnoreCase(this.raciBikeType)){//load mountain bike info
selectedInfo = racingBike.getBikeInfo();
}
if(s.equalsIgnoreCase(this.cruisBikeType)){//load mountain bike info
selectedInfo = cruiserBike.getBikeInfo();
}
return selectedInfo;
}
这是按钮的 actionPerformed 方法,而不是组合框(我不知道如何为组合框编写一个):
public void actionPerformed(ActionEvent e){
if(e.getSource()==submit){
selectedBikeIndex = bikeComboBox.getSelectedIndex();
selectedBikeName = bikeTypes[selectedBikeIndex];
loadSelectedBikeObject(selectedBikeName);
}
}
编辑:我在另一个类中编写了一个 actionPerformed 方法,并将该侦听器添加到我的组合框中。为什么还是不更新?
public class ComboBoxUpdateListener implements ActionListener{
private MainGUI maingui;
ComboBoxUpdateListener(MainGUI _mainGUI){
this.maingui = _mainGUI;
}
@Override
public void actionPerformed(ActionEvent ae) {
JComboBox cb = (JComboBox)(ae.getSource());
String bikeTypePassed = maingui.bikeTypes[cb.getSelectedIndex()];
System.out.println("This is from the LISTENERCLASS: "+bikeTypePassed);
maingui.loadSelectedBikeInfo(bikeTypePassed);
maingui.informationTextArea.updateUI();
maingui.bottom.updateUI();
}
}