gpt4 book ai didi

java - 如何让 JComboBox 创建一个或多个新的 JComboBox

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

如何从 JComboBox 中进行选择,删除或添加选择到另一个 JComboBox?

更具体地说,我有一个 JComboBox,其中包含一年中的月份作为选择。我还有另一个,里面有这个月的日子。虽然 31 日应该是 12 月的选择,但不应该是 2 月的选择。任何帮助将不胜感激。

    final JComboBox<String> monthBox = new JComboBox<String>();
monthBox.addItem("January");
monthBox.addItem("February");
monthBox.addItem("March");
monthBox.addItem("April");
monthBox.addItem("May");
monthBox.addItem("June");
monthBox.addItem("July");
monthBox.addItem("August");
monthBox.addItem("September");
monthBox.addItem("October");
monthBox.addItem("November");
monthBox.addItem("December");

JComboBox<String> dayBox = new JComboBox<String>();

最佳答案

只需将 ActionListener 附加到主 JComboBox 并在 actionPerformed 方法中,使用 JComboBox#getSelectedItem确定您应该采取什么行动。

您可以设置一个Map选项,主JCheckBox中的每个条目对应一个选项,这样可以更轻松地切换值

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

public class JoinedComboBoxes {

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

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

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

public TestPane() {
final JComboBox<String> monthBox = new JComboBox<String>();
monthBox.addItem("January");
monthBox.addItem("February");
monthBox.addItem("March");
monthBox.addItem("April");
monthBox.addItem("May");
monthBox.addItem("June");
monthBox.addItem("July");
monthBox.addItem("August");
monthBox.addItem("September");
monthBox.addItem("October");
monthBox.addItem("November");
monthBox.addItem("December");

setLayout(new GridBagLayout());
add(monthBox);

final JComboBox<Integer> days = new JComboBox<>();
days.setEnabled(false);
add(days);

monthBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Calendar cal = Calendar.getInstance();
String month = (String) monthBox.getSelectedItem();
days.setEnabled(month != null);
if (month != null) {
int calMonth = 0;
switch (month) {
case "January":
calMonth = Calendar.JANUARY;
break;
case "February":
calMonth = Calendar.FEBRUARY;
break;
case "March":
calMonth = Calendar.MARCH;
break;
case "April":
calMonth = Calendar.APRIL;
break;
case "May":
calMonth = Calendar.MAY;
break;
case "June":
calMonth = Calendar.JUNE;
break;
case "July":
calMonth = Calendar.JULY;
break;
case "August":
calMonth = Calendar.AUGUST;
break;
case "September":
calMonth = Calendar.SEPTEMBER;
break;
case "October":
calMonth = Calendar.OCTOBER;
break;
case "November":
calMonth = Calendar.NOVEMBER;
break;
case "December":
calMonth = Calendar.DECEMBER;
break;
}
days.removeAllItems();
cal.setTime(new Date()); // For the current year...
cal.set(Calendar.MONTH, calMonth);
int min = cal.getActualMinimum(Calendar.DATE);
int max = cal.getActualMaximum(Calendar.DATE);
for (int day = min; day <= max; day++) {
days.addItem(day);
}
}
}
});
monthBox.setSelectedItem(null);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.dispose();
}
}
}

关于java - 如何让 JComboBox 创建一个或多个新的 JComboBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15916273/

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