gpt4 book ai didi

java - 如何在java中将选定的项目从2个单独的组合框获取到第3个组合框以及itemstatechanged?

转载 作者:太空宇宙 更新时间:2023-11-04 06:35:37 25 4
gpt4 key购买 nike

我在 netbeans 中创建了两个组合框,并将一些项目插入到第一个组合框和第二个组合框中,当我想在第一个和第二个组合框中选择一个项目时,两个选定的值都必须出现在第三个组合框中。如果我想更改第一个和第二个中的选择,它也应该在第三个组合框中做出相同的选择,i,e,。项目状态已更改。

提前致谢,请帮助我,任何帮助将不胜感激。

最佳答案

首先查看 How to Use Combo BoxesHow to Write an Action Listeners了解更多详情...

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Combos {

public static void main(String[] args) {
new Combos();
}

public Combos() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

String[] numbers = new String[10];
for (int index = 0; index < numbers.length; index++) {
numbers[index] = String.valueOf(index);
}
String[] letters = new String[10];
for (int index = 0; index < letters.length; index++) {
letters[index] = Character.toString((char)(index + 65));
}

JComboBox cb1 = new JComboBox(numbers);
JComboBox cb2 = new JComboBox(letters);

String[] everything = new String[numbers.length + letters.length];
System.arraycopy(numbers, 0, everything, 0, numbers.length);
System.arraycopy(letters, 0, everything, numbers.length, letters.length);

JComboBox cb3 = new JComboBox(everything);

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
frame.add(cb1, gbc);
gbc.gridx++;
frame.add(cb2, gbc);

gbc.gridx = 0;
gbc.gridy++;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
frame.add(cb3, gbc);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

cb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cb3.setSelectedItem(cb1.getSelectedItem());
}
});
cb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cb3.setSelectedItem(cb2.getSelectedItem());
}
});
}
});
}

}

已更新

好吧,基本上,对于第三个 JComboBox,您需要将其他两个的内容组合成一个统一的模型,例如......

String combined[] = new String[numbers.length * letters.length];
int index = 0;
for (int outter = 0; outter < numbers.length; outter++) {
for (int inner = 0; inner < numbers.length; inner++) {
combined[index] = numbers[outter] + " " + letters[inner];
index++;
}
}

然后,当前两个中的任何一个发生更改时,您可以合并结果并将其用作第三个中的选定项目...

protected void updateCombinedView() {
String value = cb1.getSelectedItem() + " " + cb2.getSelectedItem();
cb3.setSelectedItem(value);
}

我还是不知道为什么,但你就在那里......

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Combos {

public static void main(String[] args) {
new Combos();
}

private JComboBox<String> cb1;
private JComboBox<String> cb2;
private JComboBox<String> cb3;

public Combos() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

String[] numbers = new String[10];
for (int index = 0; index < numbers.length; index++) {
numbers[index] = String.valueOf(index);
}
String[] letters = new String[10];
for (int index = 0; index < letters.length; index++) {
letters[index] = Character.toString((char) (index + 65));
}

String combined[] = new String[numbers.length * letters.length];
int index = 0;
for (int outter = 0; outter < numbers.length; outter++) {
for (int inner = 0; inner < numbers.length; inner++) {
combined[index] = numbers[outter] + " " + letters[inner];
index++;
}
}

cb1 = new JComboBox(numbers);
cb2 = new JComboBox(letters);

cb3 = new JComboBox(combined);

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
frame.add(cb1, gbc);
gbc.gridx++;
frame.add(cb2, gbc);

gbc.gridx = 0;
gbc.gridy++;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
frame.add(cb3, gbc);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

cb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updateCombinedView();
}
});
cb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updateCombinedView();
}
});
}
});
}

protected void updateCombinedView() {
String value = cb1.getSelectedItem() + " " + cb2.getSelectedItem();
cb3.setSelectedItem(value);
}

}

关于java - 如何在java中将选定的项目从2个单独的组合框获取到第3个组合框以及itemstatechanged?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25440438/

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