我试图制作一个带有 JScrollPane 的 JFrame,其中包含数百个 JRadioButton 和下面的两个 JButton(确定和取消)。最后我发现了 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();
}
感谢您的宝贵时间。
最佳答案
如图here ,您可以重写滚动 Pane 的 getPreferredSize()
方法来确定所需的大小。如果任一维度的内容较大,则会根据需要出现相应的滚动条。
JScrollPane myScrollPane = new JScrollPane(radioPanel){
@Override
public Dimension getPreferredSize() {
return new Dimension(600, 600);
}
};
JOptionPane.showConfirmDialog(null, myScrollPane);
关于java - JOptionPane.showConfirmDialog 带有 JScrollPane 和最大尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45878032/