选择JComboBox时如何获取图像?

时间:2019-02-23 20:11:41

标签: java image swing jframe jcombobox

当选择JComboxBox时,我想显示图像。 当我从组合框中选择"Tesla Model S"时,我想在组合上方显示一个Image

将价格添加到JTextField可以正常工作。我正在编写一个程序,可以在其中选择Car和附件。 选择两个项目后,价格将显示在两个文本字段中,我可以添加它们。

很遗憾,我无法发布任何图片来向您展示我的示例。

很抱歉,我的代码中有很多乱七八糟的东西。我尝试了不同的方法,但没有一个起作用

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
    }
}

1 个答案:

答案 0 :(得分:0)

如果要显示一张图像,则只需要一张JLabel,因此请删除label2label3
初始化label1,不显示任何图标:label1 = new JLabel();
并让动作监听器设置图标:

   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);
        }
    });

您还需要更改清除按钮的代码。