JOptionPane.showConfirmDialog 具有 JScrollPane 和最大尺寸
JOptionPane.showConfirmDialog with a JScrollPane and a maximum size
我试图制作一个带有 JScrollPane 的 JFrame,其中包含数百个 JRadioButton 和下面的两个 JButton(确定和取消)。最后我发现了 JOptionPane.showConfirmDialog(...) 方法。
它似乎非常适合我想要的:从第一个 JFrame,打开一个 "window" 和一个包含我的单选按钮的 Scroll 并在我单击 [=31 时在我的第一个 JFrame 中获得选定的一个=].
然而,当showConfirmDialog出现时,没有JScrollPane,我们也看不到window的底部(有数百个单选按钮)。所以:
我尝试调用 JOptionPane.showConfirmDialog(myScrollPane) 而不是将 JScrollPane 添加到 JPanel 并使用 JPanel 调用该方法...它不起作用
我试图初始化一个 JOptionPane 对象,然后设置它的最大大小,然后用初始化的对象调用方法 showConfirmDialog 但它不起作用,因为 "the method must be called in a static way".
所以我需要你的帮助,这是我的代码,我不明白哪里出了问题,为什么我在确认对话框中没有带单选按钮的滚动条。
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 所示,您可以覆盖滚动窗格的 getPreferredSize()
方法来建立所需的大小。如果任一维度的内容较大,会根据需要出现相应的滚动条。
JScrollPane myScrollPane = new JScrollPane(radioPanel){
@Override
public Dimension getPreferredSize() {
return new Dimension(600, 600);
}
};
JOptionPane.showConfirmDialog(null, myScrollPane);
我试图制作一个带有 JScrollPane 的 JFrame,其中包含数百个 JRadioButton 和下面的两个 JButton(确定和取消)。最后我发现了 JOptionPane.showConfirmDialog(...) 方法。
它似乎非常适合我想要的:从第一个 JFrame,打开一个 "window" 和一个包含我的单选按钮的 Scroll 并在我单击 [=31 时在我的第一个 JFrame 中获得选定的一个=]. 然而,当showConfirmDialog出现时,没有JScrollPane,我们也看不到window的底部(有数百个单选按钮)。所以:
我尝试调用 JOptionPane.showConfirmDialog(myScrollPane) 而不是将 JScrollPane 添加到 JPanel 并使用 JPanel 调用该方法...它不起作用
我试图初始化一个 JOptionPane 对象,然后设置它的最大大小,然后用初始化的对象调用方法 showConfirmDialog 但它不起作用,因为 "the method must be called in a static way".
所以我需要你的帮助,这是我的代码,我不明白哪里出了问题,为什么我在确认对话框中没有带单选按钮的滚动条。
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 所示,您可以覆盖滚动窗格的 getPreferredSize()
方法来建立所需的大小。如果任一维度的内容较大,会根据需要出现相应的滚动条。
JScrollPane myScrollPane = new JScrollPane(radioPanel){
@Override
public Dimension getPreferredSize() {
return new Dimension(600, 600);
}
};
JOptionPane.showConfirmDialog(null, myScrollPane);