gpt4 book ai didi

java - JComboBox 下拉列表不显示

转载 作者:行者123 更新时间:2023-12-02 17:20:21 25 4
gpt4 key购买 nike

这可能是 JComboBox popup menu not appearing 的副本,但由于这是一个相当古老的问题,并且已经有一段时间没有活跃了,而且所有的答案都不是解决方案,这有助于解决我的问题。因此我决定创建一个新问题。

问题如下:我收到了一份前同事的申请,但在我的公司不再工作了。现在我尝试将 JComboBox 添加到 JPanel。 JCombobox 按预期显示,但它的行为方式与 Seth 在他的问题中描述的方式相同:

1) The first click on the expand button does nothing. The second click highlights the contents of the box, but the popup still doesn't appear.

2) Once I've clicked the button and given it focus, up/down keystrokes cycle through the entries correctly.

我已将代码分解为我认为需要编程的最少部分,以使问题发生。 (正如上述问题中的一条评论提到提供 SSCCE,这从未发生过)。

现在这是我可以提供的代码:

public static class CreateProjectDialog extends JFrame {

private Dimension size = Toolkit.getDefaultToolkit().getScreenSize();

public CreateProjectDialog() {

setDefaultCloseOperation(EXIT_ON_CLOSE);
int SZ_INCR = 1;

// Passe Fontgröße an Resolution an:
if (size.width > 1920) {
SZ_INCR = 2;
}

// Initialize Glass Layer
final JPanel panelGlass = (JPanel) getGlassPane();
panelGlass.setLayout(null);
panelGlass.setVisible(true);

private static JPanel licBorrowPanel = null;

licBorrowPanel = new JPanel();
licBorrowPanel.setBounds(0, 20, 1000, 500);
licBorrowPanel.setVisible(false);
licBorrowPanel.setBackground(Color.WHITE);
panelGlass.add(licBorrowPanel);
}

public static void main(String[] args) {
hauptFrame = new CreateProjectDialog();
}

public static void licenceBorrowDialog() {

int mainWidth = hauptFrame.getSize().width;
int mainHeight = hauptFrame.getSize().height;

// pick a Date
JComboBox dayList = new JComboBox();
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Calendar calToday = Calendar.getInstance();
Date dayToday = calToday.getTime();
int weekDay = calToday.get(Calendar.DAY_OF_WEEK);
String weekDayName = "";
for (int i = 1; i <= 22; i++){
dayToday.setDate(dayToday.getDate()+1);
weekDay = dayToday.getDay();
weekDayName = translateWeekDay(weekDay);
dayList.addItem(i + " day(s) until " + weekDayName + " " + df.format(dayToday));
}
dayList.setOpaque(true);
dayList.setSelectedIndex(2);
dayList.setBounds(mainWidth / 2 - (125*SZ_INCR), (165*SZ_INCR), (250*SZ_INCR), (100*SZ_INCR));
licBorrowPanel.add(dayList);

dayList.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int numberOfDays;
JComboBox dl = (JComboBox)e.getSource();
numberOfDays = dl.getSelectedIndex()+1;
labelSelectedDate.setText("<HTML><BODY><b>Count of days: </b>" + numberOfDays + "</HTML></BODY>");
}
});
}

//Translate weekday int to name
public static String translateWeekDay(int day){
String retDay;
switch (day) {
case 0: retDay = "Monday";
break;
case 1: retDay = "Truesday";
break;
case 2: retDay = "Wednesday";
break;
case 3: retDay = "Thursday";
break;
case 4: retDay = "Friday";
break;
case 5: retDay = "Saturday";
break;
case 6: retDay = "Sunday";
break;
default: retDay = "Invalid day";
break;
}
return retDay;
}
}

我尝试填充更多项目(如 jluzwick 所提议的那样)以查看 DropDown 是否只是隐藏在任何东西后面,但没有。

我绝对没有像 Sehtim 怀疑的那样使用 getRootPane() 而不是 getContentPane()。

还有JCombobox is not displayed ,其中可接受的答案是将 setVisible(true) 设置为构造函数的末尾。我试过了,在我的案例中它没有改变任何行为。

我需要回答的问题是:如何使下拉列表可见,以便用户能够轻松选择条目?

最佳答案

感谢 MadProgrammer 提供有关代码未编译的提示 - 我找到了解决方案并将在此处提供给遇到类似问题的任何人。

问题是混合重量级组件和重量级组件(awt/swing)的结果。

这导致使用轻量级弹出窗口,然后可能被其他组件遮挡,因此不可见。

解决方案(如果必须保留重型和轻型的混合)是禁用轻型弹出窗口,强制应用程序使用备用弹出窗口。这是通过替换以下行来完成的:

dayList.setSelectedIndex(2);

用这一行:

dayList.setLightWeightPopupEnabled (false);

我在这里找到了解决方案: http://de.comp.lang.java.narkive.com/t2GPS9vy/jcombobox-poppt-nicht-auf

关于java - JComboBox 下拉列表不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43276431/

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