我有一个包含按钮的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(和按钮)的原因。
的屏幕尺寸和可视面积你永远不加入'p'在浏览器的构造。 'p'也没有BorderLayout。你的约束没有影响 – 2014-12-01 19:10:57
是真的!我忘了添加它!我可以用什么来代替BorderLayout ...? – 2014-12-01 19:15:44
只需将BorderLayout的“p”的布局设置为如何? – 2014-12-01 19:16:47