代码之家  ›  专栏  ›  技术社区  ›  LaPalme

JOptionPane。具有JScrollPane和最大大小的showConfirmDialog

  •  1
  • LaPalme  · 技术社区  · 7 年前

    它似乎完美地满足了我的需求:从第一个JFrame开始,打开一个“窗口”,其中有一个包含我的单选按钮的滚动条,当我单击“OK”时,在我的第一个JFrame中获得所选的一个。

    • 我试着给JOptionPane打电话。showConfirmDialog(myScrollPane),而不是将JScrollPane添加到JPanel并使用JPanel调用该方法。。。它不起作用

    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();
    }
    

    谢谢你抽出时间。

    1 回复  |  直到 7 年前
        1
  •  1
  •   trashgod    7 年前

    如图所示 here getPreferredSize() 建立所需大小的方法。如果任一维度的内容都较大,则会根据需要显示相应的滚动条。

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