Java:图像上的JComboBox选择问题

时间:2015-09-04 13:12:02

标签: java swing jcombobox

在此GUI中,有四个图像放在1.png和标签的数组上,有助于显示图像。默认图像为public class Combo extends JFrame { public Combo() { super("Combo 2 GUI"); setSize(400, 300); MethodG(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } private void MethodG() { JLabel l1; JComboBox box; JPanel p; Container pane = getContentPane(); p = new JPanel(); box = new JComboBox(); box.setModel(new DefaultComboBoxModel(new String[] { "1.png", "2.png", "3.png", "4.png" })); box.setMaximumRowCount(3); String boxoption = (String) box.getSelectedItem(); Icon[] icons = { new ImageIcon(getClass().getResource(boxoption)) }; // Default display l1 = new JLabel(icons[0]); box.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { l1.setIcon(icons[box.getSelectedIndex()]); } } }); pane.add(p); p.add(box); p.add(l1); } } ,显示在框架上。但是当我从组合列表中选择另一个图像时,它就不会出现。

代码

public class Main {

    public static void main(String[] args) {

        Combo obj = new Combo();
    }
}

主要方法

JComboBox schemaBox;
String schema = (schemaBox.isEnabled() ? schemaBox.getSelectedItem().toString() : null);
String selectTable = (schema == null ? "" : schema + ".") + tableName;

第二个问题是这两行是做什么以及如何写入语法。

{{1}}

2 个答案:

答案 0 :(得分:4)

关于第一个问题:您有(最简单的方法)直接从DefaultComboBoxModel获取图标或图像图标,例如,

Enter image description here

Enter image description here

import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import javax.swing.Icon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

public class ComboBoxModels {

    private JComboBox comboBoxDouble;
    private JComboBox comboBoxInteger;
    private JComboBox comboBoxBoolean;
    private JComboBox comboBoxIcon;
    private JComboBox comboBoxDate;
    private JLabel label =  new JLabel();
    private Vector<Double> doubleVector = new Vector<Double>();
    private Vector<Integer> integerVector = new Vector<Integer>();
    private Vector<Boolean> booleanVector = new Vector<Boolean>();
    private Vector<Icon> iconVector = new Vector<Icon>();
    private Vector<Date> dateVector = new Vector<Date>();
    private Icon icon1 = ((UIManager.getIcon("OptionPane.errorIcon")));
    private Icon icon2 = (UIManager.getIcon("OptionPane.informationIcon"));
    private Icon icon3 = (UIManager.getIcon("OptionPane.warningIcon"));
    private Icon icon4 = (UIManager.getIcon("OptionPane.questionIcon"));
    private SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");

    public ComboBoxModels() {
        doubleVector.addElement(1.001);
        doubleVector.addElement(10.00);
        doubleVector.addElement(0.95);
        doubleVector.addElement(4.2);
        comboBoxDouble = new JComboBox(doubleVector);
        integerVector.addElement(1);
        integerVector.addElement(2);
        integerVector.addElement(3);
        integerVector.addElement(4);
        comboBoxInteger = new JComboBox(integerVector);
        booleanVector.add(Boolean.TRUE);
        booleanVector.add(Boolean.FALSE);
        comboBoxBoolean = new JComboBox(booleanVector);
        iconVector.addElement(icon1);
        iconVector.addElement(icon2);
        iconVector.addElement(icon3);
        iconVector.addElement(icon4);
        comboBoxIcon = new JComboBox(iconVector);
        comboBoxIcon.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    Icon icon = (Icon) comboBoxIcon.getModel().getSelectedItem();
                    label.setIcon(icon);
                }
            }
        });
        dateVector.addElement(parseDate("25.01.2013"));
        dateVector.addElement(parseDate("01.02.2013"));
        dateVector.addElement(parseDate("03.03.2013"));
        dateVector.addElement(parseDate("18.04.2013"));
        comboBoxDate = new JComboBox(dateVector);
        comboBoxDate.setRenderer(new ComboBoxRenderer());
        JFrame frame = new JFrame("");
        frame.setLayout(new GridLayout(2, 2, 5, 5));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(comboBoxDouble);
        frame.add(comboBoxInteger);
        frame.add(comboBoxBoolean);
        frame.add(comboBoxIcon);
        frame.add(comboBoxDate);
        frame.add(label);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private Date parseDate(String str) {
        Date date = new Date();
        try {
            date = sdf.parse(str);
        } catch (ParseException ex) {
        }
        return date;
    }

    private class ComboBoxRenderer extends JLabel implements ListCellRenderer {

        private static final long serialVersionUID = 1L;

        public ComboBoxRenderer() {
            setOpaque(true);
            setBorder(new EmptyBorder(1, 1, 1, 1));
        }

        @Override
        public Component getListCellRendererComponent(JList list, Object value,
                int index, boolean isSelected, boolean cellHasFocus) {
            if (!(value instanceof Date)) {
                return this;
            }
            setText(sdf.format((Date) value));
            return this;
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                ComboBoxModels comboBoxModel = new ComboBoxModels();
            }
        });
    }
}

答案 1 :(得分:0)

您的图标数组未按预期初始化。您拥有的代码是创建一个只有一个条目的数组 - 1.png。您需要初始化数组以包​​含所有图像。像这样:

    Icon[] icons = { new ImageIcon(getClass().getResource("1.png")),  
            new ImageIcon(getClass().getResource("2.png")), 
            new ImageIcon(getClass().getResource("3.png")), 
            new ImageIcon(getClass().getResource("4.png"))};