【问题标题】:JPanel doesn't appear inside JOptionPaneJPanel 没有出现在 JOptionPane 中
【发布时间】:2014-12-01 19:08:02
【问题描述】:

我有一个包含按钮的 JDesktopPane。当我点击它时,我希望出现一个自定义的 JOptionPane,我在其中添加了一个 JLabel 和一个 JTable。

问题是我在 JOptionPane 中没有得到任何东西

这是我的代码:

class Viewer extends JPanel{
  public Viewer(){
    JLabel l = new JLabel("Hi");
    JTable t = new JTable(2,2);
    JPanel p = new JPanel();
    p.add(l,BorderLayout.NORTH);
    p.add(t,BorderLayout.CENTER);
  }
}

public class JOptionPanesWithJPanel extends JFrame{
  JPanel desktoppane;
  public JOptionPanesWithJPanel(){
    desktoppane = new JPanel();
    int inset = 50;
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    setBounds(inset, inset, screenSize.width  - inset*2, screenSize.height - inset*2);
    JPanel butPanel = new JPanel();
    JButton but = new JButton("click");
    butPanel.setVisible(true);
    butPanel.add(but);
    desktoppane.add(butPanel);
    getContentPane().add(desktoppane, BorderLayout.CENTER);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setVisible(true);

    but.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent ae) {
            Viewer v = new Viewer();
            //make JOptionPane resisable
            UIManager.put("OptionPane.minimumSize",new Dimension(150,150));
            JOptionPane.showMessageDialog(null, v, null, JOptionPane.PLAIN_MESSAGE);
        }
    });
}

public static void main(String[] args) {
    for(javax.swing.UIManager.LookAndFeelInfo lookAndFeelInfo : javax.swing.UIManager.getInstalledLookAndFeels()){
        if("Windows".equals(lookAndFeelInfo.getName())){
            try {
                javax.swing.UIManager.setLookAndFeel(lookAndFeelInfo.getClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                System.out.println(ex);
            }
            break;
        }
    }
    JOptionPanesWithJPanel j = new JOptionPanesWithJPanel();
  }
}

我应该怎么做才能在 JOptionPane 中显示面板?提前谢谢你。

PS: 我使用 JoptionPane 的原因是禁止用户单击 JDesktopPane 中的其他任何位置(以及按钮),除非他关闭当前窗口。

【问题讨论】:

  • 您从未在查看器构造函数中添加pp 也没有 BorderLayout。您的约束没有影响
  • 真的!我忘了添加它!我可以用什么来代替 BorderLayout...?
  • p的布局设置为BorderLayout怎么样?
  • p.setLayout(new BorderLayout())
  • 我不会搞砸 UIManager 和最小尺寸属性,因为这也会影响所有未来的 JOptionPanes...

标签: java swing jpanel joptionpane


【解决方案1】:

您正在将组件添加到第二个面板,但没有将该面板添加到 Viewer...

    public Viewer() {
        JLabel l = new JLabel("Hi");
        JTable t = new JTable(2, 2);
        JPanel p = new JPanel(); // <-- Create the new panel
        p.add(l, BorderLayout.NORTH);
        p.add(t, BorderLayout.CENTER);
        // And now what??
    }

如果没有更多证据,我会扔掉第二个面板,直接将组件添加到Viewer...

    public Viewer() {
        setLayout(new BorderLayout());
        JLabel l = new JLabel("Hi");
        JTable t = new JTable(2, 2);
        add(l, BorderLayout.NORTH);
        add(t, BorderLayout.CENTER);
    }

挑剔...

这...

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(inset, inset, screenSize.width - inset * 2, screenSize.height - inset * 2);

是一种非常糟糕的调整框架大小/位置的方法。不是每个人的任务栏/停靠栏的大小或位置都相同,您不应该依赖“神奇”数字(您组成的数字),相反,您应该设法对当前的环境状态做出适当的确定.

有关检测屏幕尺寸和屏幕可视区域的更多详细信息,请参阅How to set present screensize in Java Swing?

【讨论】:

  • 非常感谢我去试试
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-11
  • 1970-01-01
相关资源
最近更新 更多