【发布时间】:2017-08-25 09:08:49
【问题描述】:
我试图用一个 JScrollPane 制作一个 JFrame,其中包含数百个 JRadioButtons 和下面的两个 JButtons(确定和取消)。最后我发现了 JOptionPane.showConfirmDialog(...) 方法。
这似乎是我想要的完美:从第一个 JFrame,打开一个“窗口”,其中包含我的单选按钮的滚动,当我单击“确定”时,在我的第一个 JFrame 中获取选定的一个。 但是,当showConfirmDialog出现时,没有JScrollPane,我们看不到窗口的底部(有数百个单选按钮)。所以:
我尝试调用 JOptionPane.showConfirmDialog(myScrollPane) 而不是将 JScrollPane 添加到 JPanel 并使用 JPanel 调用该方法...它不起作用
我尝试初始化一个 JOptionPane 对象,然后设置它的最大大小,然后使用初始化的对象调用方法 showConfirmDialog 但它不起作用,因为“必须以静态方式调用该方法”。
所以我需要你的帮助,这是我的代码,我不明白哪里出了问题,为什么我在确认对话框中没有带有单选按钮的滚动条。
public void buildMambaJobPathWindow(ArrayList<String> list) {
ButtonGroup buttonGroup = new ButtonGroup();
JPanel radioPanel = new JPanel(new GridLayout(0,1));
for(int i=0; i<list.size(); i++) {
JRadioButton radioButton = new JRadioButton(list.get(i));
buttonGroup.add(radioButton);
radioButton.addActionListener(this);
radioButton.setActionCommand(list.get(i));
radioPanel.add(radioButton);
}
JScrollPane myScrollPane = new JScrollPane(radioPanel);
myScrollPane.setMaximumSize(new Dimension(600,600));
JOptionPane.showConfirmDialog(null, myScrollPane);
}
// Listens to the radio buttons
public void actionPerformed(ActionEvent e) {
String result = e.getActionCommand();
}
感谢您的宝贵时间。
【问题讨论】:
标签: java swing joptionpane