【问题标题】:JOptionPane.showConfirmDialog with a JScrollPane and a maximum size带有 JScrollPane 和最大尺寸的 JOptionPane.showConfirmDialog
【发布时间】: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


    【解决方案1】:

    here 所示,您可以覆盖滚动窗格的getPreferredSize() 方法来建立所需的大小。如果任一维度的内容较大,将根据需要显示相应的滚动条。

    JScrollPane myScrollPane = new JScrollPane(radioPanel){
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(600, 600);
        }
    };
    JOptionPane.showConfirmDialog(null, myScrollPane);
    

    【讨论】:

    • 伟大的@trashgod !这正是我所需要的,就这么简单! :) 祝你有美好的一天,谢谢。
    猜你喜欢
    • 1970-01-01
    • 2016-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-26
    相关资源
    最近更新 更多