Java JOptionPane有可滚动的checkbutton列表

时间:2014-10-16 16:50:48

标签: java swing joptionpane jcheckbox scrollable

我这里有一点问题。我目前正在使用GUI来组织我的系列,我正在观看/观看。在这部分中,我希望在JCheckBox'es中打开一个包含所有不同类型的框架,它应该返回一个boolean - 数组,其中每个索引代表该索引的类型,如果它是{{点击{1}}。

我决定这样做JCheckBox,因为JOptionPane - funktion提供了一个JOptioPane.showInputPane() - 数组作为参数,它将显示在Object中。到目前为止,我的代码工作正常:

JOptionPane

问题是 String[] genres = IO.getGenres(); JCheckBox[] check = new JCheckBox[genres.length]; for(int i = 0; i < genres.length; i++) check[i] = new JCheckBox(genres[i]); boolean[] ret = new boolean[genres.length]; int answer = JOptionPane.showConfirmDialog(null, new Object[]{"Choose gernes:", check}, "Genres" , JOptionPane.OK_CANCEL_OPTION); if(answer == JOptionPane.OK_OPTION) { for(int i = 0; ; i++) ret[i] = check[i].isSelected(); }else if(answer == JOptionPane.CANCEL_OPTION || answer == JOptionPane.ERROR_MESSAGE) { for(int i = 0; i < genres.length; i++) ret[i] = false; } return ret; 增长到很大,有很多类型。那么如何制作JOptionPane es的可滚动列表,该列表显示在JCheckBox中?或者有更好的解决方法吗?

1 个答案:

答案 0 :(得分:2)

您可以在面板中简单地布置复选框,并将包装滚动窗格传输到JOptionPane。像这样:

JPanel layoutPanel = new JPanel(new GridLayout(genres.length, 1));
for (JCheckBox c : check) {
  layoutPanel.add(c);
}
JScrollPane scroller = new JScrollPane(layoutPanel);
scroller.setPreferredSize(new Dimension(300, 500));
int answer = JOptionPane.showConfirmDialog(null, scroller, "Genres" , JOptionPane.OK_CANCEL_OPTION);