繁体   English   中英

JPanel没有出现在JOptionPane中

[英]JPanel doesn't appear inside 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中的其他任何位置(以及按钮)。

您是将组件添加到第二个面板,而不是将面板添加到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);

这是确定框架大小/位置的一种很差的方法。 并非每个人的任务栏/停靠点都具有相同的大小或位置,并且您不应该依赖“不可思议的”数字(由您组成的数字),相反,您应该找到适当的位置来确定环境的当前状态。

请参阅如何在Java Swing中设置当前屏幕尺寸? 有关检测屏幕尺寸和屏幕可视区域的更多详细信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM