gpt4 book ai didi

java - 有没有办法为 JOptionPane.showOptionDialog 设置布局?

转载 作者:行者123 更新时间:2023-11-29 03:07:35 25 4
gpt4 key购买 nike

如果您使用的数组,比方说长度为 15,并且您想一次显示它们,它会在一个很长的显示框中并排显示它们:

String[] options = {"Option 1","Option 2","Option 3","Option 4",
"Option 5","Option 6","Option 7","Option 8","Option 9",
"Option 10","Option 11","Option 12","Option 13","Option 14",
"Option 15"};

int displayoptions = JOptionPane.showOptionDialog (null, "select one", "Title",
JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options [0]);

有没有办法显示它,使其具有 Flow Layout?

最佳答案

对于 15 个选项,我会更像这样:

enter image description here

import java.awt.*;
import javax.swing.*;

public class ManyOptions {

ManyOptions() {
initUI();
}

public void initUI() {
String[] options = {"Option 1", "Option 2", "Option 3", "Option 4",
"Option 5", "Option 6", "Option 7", "Option 8", "Option 9",
"Option 10", "Option 11", "Option 12", "Option 13", "Option 14",
"Option 15"};

JComboBox combo = new JComboBox(options);
int result = JOptionPane.showConfirmDialog(
null,
combo,
"Select One",
JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
System.out.println("Selected Index: " + combo.getSelectedIndex());
} else {
System.out.println("choice cancelled..");
}

// old way, for comparison..
int displayoptions = JOptionPane.showOptionDialog(
null,
"select one",
"Title",
JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE,
null,
options,
options[0]);
System.out.println("displayoptions: " + displayoptions);
}

public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
ManyOptions o = new ManyOptions();
}
};
SwingUtilities.invokeLater(r);
}
}

关于java - 有没有办法为 JOptionPane.showOptionDialog 设置布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31307194/

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