我有一个包含3个面板的JPanel。面板的布局是BorderLayout,使用BorderLayout.NORTH,.CENTER和.SOUTH划分3个面板。设置为.SOUTH的面板称为“底部”。在底部有一个textArea,它应该根据用户从上面面板中的comboBox中选择的内容显示信息。但是出于某种原因,我的文本区域没有更新信息。这个想法是,当用户选择“山地自行车”时,文本区域将显示有关山地自行车的信息。然后,如果用户在此之后立即选择“公路自行车”,它将显示有关公路自行车的信息。由于某些原因,它不是这样做的,而且我确定更新它是一个问题,因为我的所有类实例都是正确的,当我尝试为组合框选择不同的起始索引时,它们都在文本中显示正确的信息区域。这是我的建设者:
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();
}
}
答案 0 :(得分:3)
当我尝试为组合框选择不同的起始索引时, 他们都在文本区域显示正确的信息。
仔细看看你对工作代码所做的工作:
String returnedInfo = loadSelectedBikeInfo(selectedBikeName);
informationTextArea.setText(returnedInfo);
根据你的编辑:
编辑:我在另一个类中编写了一个actionPerformed方法并添加了 那个听我的组合框。为什么还没有更新?
@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(); }
loadSelectedBikeInfo(bikeTypePassed)
返回String
:
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;
}
所以你应该得到String
并在setText(String text)
上致电JTextArea
:
@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);
String info=maingui.loadSelectedBikeInfo(bikeTypePassed);
maingui.informationTextArea.setText(info);
}
请同时阅读Concurrency in Swing。
正如其他人所说updateUI
对你没有帮助:
使用当前外观中的值重置UI属性。
如果有人可以调用repaint()
,但我认为没有理由这样做。
setSize
可见pack()
实施正确的LayoutManager
并致电JFrame