2014-12-01 60 views
0

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

的问题是,我不明白里面的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

的屏幕尺寸和可视面积你永远不加入'p'在浏览器的构造。 'p'也没有BorderLayout。你的约束没有影响 – 2014-12-01 19:10:57

+0

是真的!我忘了添加它!我可以用什么来代替BorderLayout ...? – 2014-12-01 19:15:44

+0

只需将BorderLayout的“p”的布局设置为如何? – 2014-12-01 19:16:47

回答

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

尼特挑选...

氏s ...

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

是一个非常糟糕的方式来确定你的框架的大小/位置。不是每个人都有任务栏/码头大小相同或位置相同,并且不应该依赖“魔术”数字(您编制的数字),相反,您应该找到适当的方式来确定环境的当前状态。

How to set present screensize in Java Swing?了解更多详细的检测屏幕

+0

非常感谢,我会尝试它 – 2014-12-07 16:05:47