gpt4 book ai didi

Java:图像上的 JComboBox 选择问题

转载 作者:行者123 更新时间:2023-11-30 06:58:43 26 4
gpt4 key购买 nike

在此 GUI 中,有四个图像放置在 DefaultComboBoxModel 数组和有助于显示图像的标签上。默认图像是显示在框架上的 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();
}
}

还有第二个问题这两行是干什么用if语法写的。

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

最佳答案

对于第一个问题:您有(最简单的方法)直接从 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();
}
});
}
}

关于Java:图像上的 JComboBox 选择问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32398963/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com