[英]How to get an Image when JComboBox is selected?
I want to display an Image, when a JComboxBox
is selected. I want to display an Image, when a JComboxBox
is selected. When I select "Tesla Model S"
from the combo box I want to display an Image
above the combo.当我从组合框中选择"Tesla Model S"
,我想在组合上方显示一个Image
。
Adding the Price into the JTextField
works fine.将价格添加到JTextField
工作正常。 I'm writing a Program where I can select a Car
and accessories.我正在编写一个程序,我可以在其中选择Car
和配件。 After I had selected the two Items, the price will show up in both text fields and I can add them.在我选择了两个项目后,价格将显示在两个文本字段中,我可以添加它们。
Unfortunately I can't post any picture to show you my example.不幸的是,我无法发布任何图片来向您展示我的示例。
I'm sorry if there is a lot of mess in my code.如果我的代码中有很多混乱,我很抱歉。 I tried different things but none of them worked我尝试了不同的东西,但没有一个奏效
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Auto extends JFrame implements ActionListener, ItemListener {
JTextField AutoFeld;
JTextField AussFeld;
JTextField GesamtFeld;
JButton ResButton;
JButton ClearButton;
JCheckBox displayButton;
ImageIcon image1;
ImageIcon image2;
ImageIcon image3;
JLabel label1;
JLabel label2;
JLabel label3;
JLabel Ergebnis;
JLabel GesamtLabel;
JPanel panel0;
StringBuffer choices;
public Auto() {
this.setTitle("Auto");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500, 500);
this.setLayout(new GridLayout(0, 10));
/*image1 = new ImageIcon(getClass().getResource("AMGgt.jpg"));
image2 = new ImageIcon(getClass().getResource("AudiA7.jpg"));
image3 = new ImageIcon(getClass().getResource("TeslaS.jpg"));
*/
GesamtLabel = new JLabel("Gesamt");
image1 = new ImageIcon(getClass().getResource("AMGgt.jpg"));
label1 = new JLabel(image1);
image2 = new ImageIcon(getClass().getResource("AudiA7.jpg"));
label2 = new JLabel(image2);
image3 = new ImageIcon(getClass().getResource("TeslaS.jpg"));
label3 = new JLabel(image3);
AutoFeld = new JTextField("0", 10);
AutoFeld.setEditable(true);
AussFeld = new JTextField("0", 10);
AussFeld.setEditable(true);
GesamtFeld = new JTextField("0", 15);
GesamtFeld.setEditable(false);
ResButton = new JButton("Ergebnis");
//ResButton.addActionListener(this);
ResButton.addActionListener(e -> {
Object source = e.getSource();
String s1 = AutoFeld.getText();
String s2 = AussFeld.getText();
int o1 = Integer.parseInt(s1);
int o2 = Integer.parseInt(s2);
if (source == ResButton) {
GesamtFeld.setText("" + (o1 + o2));
}
});
ClearButton = new JButton("Clear");
//ClearButton.addActionListener(this);
ClearButton.addActionListener(e -> {
Object source = e.getSource();
if (source == ClearButton) {
AutoFeld.setText("0");
AussFeld.setText("0");
GesamtFeld.setText("0");
}
});
displayButton = new JCheckBox("Helles Display");
displayButton.setSelected(true);
JPanel panel0 = new JPanel();
panel0.add(label1);
panel0.add(label2);
panel0.add(label3);
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(3, 2));
String[] Autos = {"---Bitte Model auswählen---", "Tesla Model S", "Mercedes-AMG GT", "Audi A7"};
JComboBox AutoList = new JComboBox(Autos);
AutoList.addActionListener(e -> {
Object source = e.getSource();
JComboBox selectedChoice = (JComboBox) e.getSource();
if ("Tesla Model S".equals(selectedChoice.getSelectedItem())) {
AutoFeld.setText("108420");
label1.getIcon();
} else if ("Mercedes-AMG GT".equals(selectedChoice.getSelectedItem())) {
AutoFeld.setText("134351");
label2.getIcon();
} else if ("Audi A7".equals(selectedChoice.getSelectedItem())) {
AutoFeld.setText("58350");
//label3.getIcon();
}
});
panel1.add(AutoList);
AutoList.setSelectedIndex(0);
//AutoList.addActionListener(this);
panel1.add(AutoFeld);
JPanel panel2 = new JPanel();
panel2.setLayout(new GridLayout(3, 2));
String[] Ausstattung = {"---Bitte Ausstattung auswählen---", "Sportsitze", "Navigationsgerät", "Kaffeehalter"};
JComboBox AussList = new JComboBox(Ausstattung);
panel2.add(AussList);
AussList.setSelectedIndex(0);
AussList.addActionListener(e -> {
JComboBox selectedChoice = (JComboBox) e.getSource();
if ("Sportsitze".equals(selectedChoice.getSelectedItem())) {
AussFeld.setText("1679");
} else if ("Navigationsgerät".equals(selectedChoice.getSelectedItem())) {
AussFeld.setText("90");
} else if ("Kaffeehalter".equals(selectedChoice.getSelectedItem())) {
AussFeld.setText("20");
}
});
panel2.add(AussFeld);
JPanel panel3 = new JPanel();
panel3.setLayout(new GridLayout(3, 2));
panel3.add(GesamtLabel);
panel3.add(GesamtFeld);
JPanel panel4 = new JPanel();
panel4.add(ResButton);
panel4.add(ClearButton);
JPanel panel5 = new JPanel();
panel5.add(displayButton);
displayButton.setSelected(true);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(panel0);
panel.add(panel1);
panel.add(panel2);
panel.add(panel3);
panel.add(panel4);
panel.add(panel5);
panel.setBorder(BorderFactory.createEmptyBorder(50, 50, 50, 50));
setContentPane(panel);
pack();
setResizable(true);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
//JComboBox selectedChoice = (JComboBox) e.getSource();
Object source = e.getSource();
JComboBox selectedChoice = (JComboBox) e.getSource();
String s1 = AutoFeld.getText();
String s2 = AussFeld.getText();
double o1 = Double.valueOf(s1);
double o2 = Double.valueOf(s2);
/*if(source == ClearButton) {
AutoFeld.setText("0");
AussFeld.setText("0");
GesamtFeld.setText("0");
} */
}
public static void main(String[] args) {
JFrame myApplication = new Auto();
myApplication.setVisible(true);
}
@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
}
}
If you want to display one image, you only need one JLabel
, so remove label2
and label3
.如果你想显示一张图片,你只需要一个JLabel
,所以去掉label2
和label3
。
Initialize label1
with out any icon : label1 = new JLabel();
不带任何图标初始化label1
: label1 = new JLabel();
and have the action listener set the icon:并让动作侦听器设置图标:
AutoList.addActionListener(e -> {
Object source = e.getSource();
JComboBox selectedChoice = (JComboBox) e.getSource();
if ("Tesla Model S".equals(selectedChoice.getSelectedItem())) {
AutoFeld.setText("108420");
label1.setIcon(image1);
} else if ("Mercedes-AMG GT".equals(selectedChoice.getSelectedItem())) {
AutoFeld.setText("134351");
label1.setIcon(image2);
} else if ("Audi A7".equals(selectedChoice.getSelectedItem())) {
AutoFeld.setText("58350");
label1.setIcon(image3);
}
});
You will also need to change the code of the clear button.您还需要更改清除按钮的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.