Swing 框架不显示使用MigLayout的面板
也许这是一个简单的错误,但我不知道是什么错了。我有一个类,它创建一个框架(大型机)并使用方法更改面板。我有另一个类,其中有面板描述。然而,由于某些原因,我只能看到没有面板的框架。有人能帮我吗?我是新来的,如果你能解释一下我的错误就太好了Swing 框架不显示使用MigLayout的面板,swing,jframe,jpanel,miglayout,Swing,Jframe,Jpanel,Miglayout,也许这是一个简单的错误,但我不知道是什么错了。我有一个类,它创建一个框架(大型机)并使用方法更改面板。我有另一个类,其中有面板描述。然而,由于某些原因,我只能看到没有面板的框架。有人能帮我吗?我是新来的,如果你能解释一下我的错误就太好了 public class MainFrame extends JFrame { private JPanel panel; //getting dimensions public static Dimension dim = Toolkit.getDefau
public class MainFrame extends JFrame
{
private JPanel panel;
//getting dimensions
public static Dimension dim = Toolkit.getDefaultToolkit().getScreenSize() ;
public MainFrame()
{
getContentPane().setLayout(new MigLayout());
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setTitle("Title");
this.setLocation((int)dim.getWidth()/3,(int)dim.getHeight()/4);
this.setSize(500, 500);
setNewPanel(new MainWindowPanel());
this.validate();
}
public final void setNewPanel(JPanel newPanel)
{
//to change the panel, old one has to be deleted
if (panel != null) remove(panel);
getContentPane().setLayout(new MigLayout());
add(newPanel);
//pack();
panel = newPanel;
this.setVisible(true);
}
}
我的小组课
public class MainWindowPanel extends JPanel
{
//Label
JLabel greeting = new JLabel("Welcome:");
//Buttons
JButton helpButton = new JButton("Help?");
public MainWindowPanel()
{
// the layout of the main screen
JPanel p = new JPanel(new MigLayout("fill", "[center]"));
p.setBackground(Color.lightGray);
p.add(greeting, "skip 1, gaptop 40, wrap");
greeting.setFont(times20);
p.add(helpButton, "bottom, span, tag help");
}
}
谢谢 在MainWindowPanel的构造函数中,您可以创建一个新面板,并将按钮/标签添加到该面板中,而无需添加新创建的面板。添加以下行:
add(p);
事实上,我不太明白你想用那些嵌套很深的面板做什么,为什么不呢
public MainWindowPanel() {
setLayout(new MigLayout( ... contraints);
add(greetings);
add(button);
}
当您在使用时:考虑不扩展JPAND,但使用它:
老实说,你的建议救了我一天!:)我不知道如何将面板拉伸到框架上,但一旦我使用了你的建议,省略了那些嵌套面板,一切都变得很好和清晰:)。多谢各位!
JComponent mainWindowPanel = new JPanel(new MigLayout(...));
JLabel greetings = ... // create and configure
mainWindowPanel.add(greetings);
JButton button = ... // create and configure
mainWindowPanel.add(button);