[英]How do I update a JLabel everytime a JComboBox changes?
我有一個帶有12個不同選擇的JComboBox,並且根據選擇的內容,我希望問題(JLabel)更改與選擇匹配的內容。 我嘗試了一個if語句來查看選擇的內容,如果它與應選擇的內容相匹配,則問題會相應更改,但是JLabel在任何情況下都不會真正更改。
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Window extends JFrame{
private static final long serialVersionUID = 1L;
public Window(){
super("Area Finder v1.0");
BufferedImage image = null;
try {
image = ImageIO.read(getClass().getClassLoader().getResource("images/areafinder.png"));
} catch (IOException e) {
e.printStackTrace();
}
super.setIconImage(image);
setSize(400, 500);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JLabel Instr = new JLabel("What would kind of area would you like to find?");
String[] areaChoices = {"Circle", "Square", "Rectangle", "Triangle", "Trapezoid", "Parallelogram", "Hexagon", "Rhombus", "Pentagon", "Polygon", "Ellipse", "Sector"};
final JComboBox<?> choiceBox = new JComboBox(areaChoices);
final Object isSelected = choiceBox.getSelectedItem();
choiceBox.setToolTipText("Select which one you want to find the area of!");
choiceBox.setSelectedIndex(0);
final JLabel q = new JLabel("");
final JTextField inputFromUser = new JTextField("");
JButton findArea = new JButton("Find Area");
/* Question Changer*/
/*End of Question Changer*/
findArea.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0){
if(isSelected == "Circle"){
double radius = Double.parseDouble(inputFromUser.getText());
double area = 3.14 * (radius * radius);
JOptionPane.showMessageDialog(null, "Your Area is " + area);
}else if(isSelected == "Square"){
}
}
});
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(15,15,15,15);
panel.add(Instr);
panel.add(choiceBox);
panel.add(findArea);
panel.add(q);
panel.add(inputFromUser);
add(panel);
}
}
編輯:所以我用System.out.println()做了一些測試; 我發現它可以一次調用所有項目,但是被選中的項目將被首先調用。 例:
choiceBox.addItemListener(new ItemListener(){
@Override
public void itemStateChanged(ItemEvent e) {
if(e.getItem() == "Circle"){
System.out.println("Circle selected");
}else if(e.getItem() == "Square"){
System.out.println("Square selected");
}
}
});
如果選擇圓,則輸出“選擇圓選定的圓”,如果選擇“平方”,則打印出“選定圓的選定圓”。
將ItemListener
添加到JComboBox
以在選擇更改時做出反應。
另外,當您這樣做時:
Object isSelected = choiceBox.getSelectedItem();
...您當時只是獲得選定的值; 您不會神奇地綁定isSelected
變量以在組合框更新時進行更新。 如果需要新值,則需要再次調用getSelectedItem()
。
嘗試將動作偵聽器添加到JComboBox
choiceBox.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//put the code here
}
});
更改JLabel的文本
label.setText("Changed text");
告訴容器重新布局。
frame.invalidate();
frame.repaint();
這樣可以確保重新繪制框架以顯示更改后的標簽。 要知道組合框何時更改,請添加一個這樣的ActionListener
。
combo.addActionListener (new ActionListener ()
{
public void actionPerformed(ActionEvent e)
{
label.setText(combo.getText());
frame.invalidate();
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.