gpt4 book ai didi

Java JCombobox 不更改 JPanel 中的图像

转载 作者:行者123 更新时间:2023-12-02 00:07:04 25 4
gpt4 key购买 nike

我有一个问题。我有一个 JPanel 元素。在此 JPanel 中将有四个 JButton 和图像。该图像是“绿色 LED 灯”,它会根据按下哪个 JButton 而变化。它运作得很好。但我也希望 JComboBox 更改此参数。

问题就在这里。虽然参数(从 1 到 4)正在改变,(这显示 JLabel 靠近 LED),但图片(LED灯)不想改成正确的。

screenshot

该计划分为3个类(class)。

MyFrame.class 这个主类。

public class MyFrame extends JFrame {

int ctrlTriggers = 1;
private JLabel label = new JLabel();
private Triggers triggers;
private String[] typeTrig = {"1", "2", "3", "4"};

public MyFrame() {
JFrame frame = new JFrame("JComboBox Problem");
frame.setBounds(50, 0, 800, 240);
frame.setLayout(new BorderLayout());


int tvalue = 1;
String tstr = Integer.toString(tvalue);
JPanel panelTrig = new JPanel(new BorderLayout());
label = new JLabel(tstr, 4);
panelTrig.add(label);
final JLabel et = label;

triggers = new Triggers(typeTrig, "trigger " + Integer.toString(1));

triggers.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
Triggers tr = (Triggers) e.getSource();
int vol;

vol = (int) tr.getValue();
et.setText(Integer.toString(vol));

}
});

panelTrig.add(triggers, BorderLayout.LINE_START);



// JCOMBOBOX
JPanel pCombo = new JPanel();
pCombo.setLayout(new FlowLayout());
Presets combo = new Presets();
combo.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
Presets presetcombo = (Presets) e.getSource();
int tval;
tval = (int) presetcombo.getValue(ctrlTriggers);
label.setText(Integer.toString(tval));
triggers.setValue(tval);
}
});

pCombo.add(combo, BorderLayout.CENTER);
frame.add(pCombo, BorderLayout.CENTER);
frame.add(panelTrig, BorderLayout.LINE_START);


frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

setDefaultCloseOperation(DISPOSE_ON_CLOSE);
frame.show();
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MyFrame();
}
});
}
}

Triggers.class 这个类是 JPanel,其中有 4 个 JButton 和“LED 类似图像”

public class Triggers extends JPanel
implements ActionListener, ItemListener {

public Triggers(String s[], String name) {

JPanel panel = new JPanel(new BorderLayout());
this.value = Integer.parseInt(s[selected]);

for (int i = 0; i < s.length; i++) {


ImageIcon cup = new ImageIcon();
button[i] = new JButton(z[i], cup);
button[i].setPreferredSize(new Dimension(70, 25));

Font font = button[i].getFont();
button[i].setFont(new Font(font.getFontName(), font.getStyle(), 10));
button[i].setActionCommand(s[i]);

if (i == selected) {
button[i].setSelected(true);
} else {
button[i].setSelected(false);
}
}



ButtonGroup group = new ButtonGroup();

for (int i = 0; i < s.length; i++) {
group.add(button[i]);
button[i].addActionListener(this);
button[i].addItemListener(this);

}



diody = new JLabel(createImageIcon("/resources/diody/"
+ this.value
+ ".png"));


diody.setPreferredSize(new Dimension(20, 100));

JPanel triggs = new JPanel(new FlowLayout());
triggs.setPreferredSize(new Dimension(80, 120));

for (int i = 0; i < s.length; i++) {
triggs.add(button[i]);
}

panel.add(triggs, BorderLayout.LINE_START);
panel.add(diody, BorderLayout.LINE_END);
add(panel);


}

public void actionPerformed(ActionEvent e) {

cmd = e.getActionCommand();
diody.setIcon(createImageIcon("/resources/diody/"
+ cmd
+ ".png"));



if (cmd.equals("1")) {

setValue(1);

} else if (cmd.equals("2")) {

setValue(2);

} else if (cmd.equals("3")) {

setValue(3);

} else if (cmd.equals("4")) {

setValue(4);
}
}

public static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = Triggers.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}

public int setController() {
return value;
}

public void setValue(int k) {

this.value = k;
cmd = Integer.toString(this.value);
repaint();
fireChangeEvent();
}

public int getValue() {
return this.value;
}

public void setSel(int k) {
selected = k;
repaint();
fireChangeEvent();
}

public void itemStateChanged(ItemEvent ie) {
String s = (String) ie.getItem();
}

public void addChangeListener(ChangeListener cl) {
listenerList.add(ChangeListener.class, cl);

}

public void removeChangeListener(ChangeListener cl) {
listenerList.remove(ChangeListener.class, cl);
}

protected void fireChangeEvent() {
Object[] listeners = listenerList.getListenerList();
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == ChangeListener.class) {
if (changeEvent == null) {
changeEvent = new ChangeEvent(this);
}
((ChangeListener) listeners[i + 1]).stateChanged(changeEvent);
}
}
}
private ChangeEvent changeEvent = null;
private EventListenerList listenerList = new EventListenerList();
private JLabel diody;
private int x = 1;
private int y = 4;
private JButton button[] = new JButton[y];
double border = 4;
double vborder = 1;
int controller[] = new int[4];
String z[] = {"1", "2", "3", "4"};
private int selected;
private int value;
private String cmd;
}

类预设这是 JCombobox。

public class Presets extends JPanel
implements ActionListener {
private int value;
private int[] ctrlIdx = new int[27];
private int controller;
private ChangeEvent changeEvent = null;
private EventListenerList listenerList = new EventListenerList();
private JLabel label;
private String[][] presets = {
{"0", "3"},
{"1", "2"},
{"2", "3"},
{"3", "1"},
{"4", "4"},
{"5", "2"},};
String[] ctrlName = {"preset 1 (value 3)", "preset 2 (value 2)", "preset 3 (value 3)", "preset 4 (value 1)", "preset 5 (value 4)", "preset 5 (value 2)"};

public Presets() {


JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setPreferredSize(new Dimension(600, 30));

JComboBox combolist = new JComboBox(ctrlName);
combolist.setSelectedIndex(2);
combolist.addActionListener(this);

combolist.setPreferredSize(new Dimension(300, 30));
label = new JLabel();
label.setFont(label.getFont().deriveFont(Font.ITALIC));
label.setHorizontalAlignment(JLabel.CENTER);
int indeks = combolist.getSelectedIndex();
updateLabel(ctrlName[combolist.getSelectedIndex()], indeks);

label.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));
label.setPreferredSize(new Dimension(300, 30));


panel.add(combolist, "East");
add(panel);
setPreferredSize(new Dimension(600, 40));

}

public void setSelectedItem(int a) {
}

public void setValue(int c, int v) {

controller = c;
value = v;
ctrlIdx[controller] = value;

repaint();
fireChangeEvent();

}

public int getController() {
return controller;
}

public int getValue(int c) {
int w = (int) ctrlIdx[c];
return (int) w;
}

public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox) e.getSource();
String pres = (String) cb.getSelectedItem();
int indeks = cb.getSelectedIndex();
updateLabel(pres, indeks);



for (int i = 0; i < ctrlName.length; i++) {
for (int j = 0; j < 2; j++) {

setValue(j, Integer.parseInt(presets[indeks][j]));
}
}


}

protected void updateLabel(String name, int ii) {

label.setToolTipText("A drawing of a " + name.toLowerCase());

}

protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = Presets.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}

public void addChangeListener(ChangeListener cl) {
listenerList.add(ChangeListener.class, cl);
}

public void removeChangeListener(ChangeListener cl) {
listenerList.remove(ChangeListener.class, cl);
}

protected void fireChangeEvent() {

Object[] listeners = listenerList.getListenerList();

for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == ChangeListener.class) {

if (changeEvent == null) {
changeEvent = new ChangeEvent(this);
}
((ChangeListener) listeners[i + 1]).stateChanged(changeEvent);
}
}
}

}

谁能帮帮我,我做错了什么吗?

最佳答案

您的图像仅在 actionPerformed 中开始加载你的方法Triggers类,这意味着当您调用setValue时响应stateChanged Presets 引发的事件类,没有开始加载新图像。

移动您的图像加载逻辑,使其位于 setValue 中方法或由 setValue 触发方法

关于Java JCombobox 不更改 JPanel 中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13617223/

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