JPanel并没有出现在JOptionPane中

时间:2014-12-01 19:08:02

标签: java swing jpanel joptionpane

我有一个包含按钮的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中的任何其他位置(以及按钮),除非他关闭当前窗口。

1 个答案:

答案 0 :(得分:2)

您已将组件添加到第二个面板,但未将面板添加到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);
    }

Nit挑选......

此...

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

确定框架大小/位置的方法真的很差。不是每个人都有任务栏/码头相同的大小或相同的位置,你不应该依赖&#34;魔术&#34;数字(你编号的数字),相反,你应该找到适当的决定当前的环境状态。

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