我想知道我们是否可以有一个JPanel,其布局不是它的父JFrame。例如。如果我有带边框布局的JFrame,并且我们有一个嵌入的JPanel,并且它有不同的布局。可能吗 ?JPanel具有不同的布局,它的JFrame
我正在努力做到这一点。但是这样JPanel的组件没有显示出来。
这里来详细的问题:我有一个JFrame和布局是边框式布局
。我在这个框架上添加一个JPanel。如果我不为JPanel设置任何布局。 JPanel的所有组件都显示在窗口上,但是当我为JPanel设置网格布局时,JPanel的组件不可见。我将Layout添加到JPanel以便对齐组件。以下是我的代码:
我有一个主类,一个框架类和Jpanel类。
public class AppMain {
public static void main(String[] args) {
AppPage1 page1 = new AppPage1("test");
page1.setVisible(true);
}
}
public class AppPage1 extends JFrame {
public AppPage1(String title) throws HeadlessException {
super(title);
this.setLayout(new BorderLayout());
addWindowListener(new WindowAdapter() {
public void windowOpened(WindowEvent e) {
setExtendedState(MAXIMIZED_BOTH);
}
});
//Panel for logo
JLabel testLogo = new JLabel("");
testLogo.setIcon(new javax.swing.ImageIcon("test.JPG"));
List<JComponent> componentList = new ArrayList<JComponent>();
componentList.add(testLogo);
PagePanel logoPanel = new PagePanel(componentList,null);
this.add(logoPanel, BorderLayout.NORTH);
//Panel for Button and checkboxes
JLabel panelTitle = new JLabel("test Wizard");
JRadioButton rdButton_ExistingConfigurationFile = new JRadioButton("Existing Configuration File");
JRadioButton rdButton_ConfigureNewPropertyFile = new JRadioButton("Configure new Property File");
componentList = new ArrayList<JComponent>();
componentList.add(panelTitle);
componentList.add(rdButton_ExistingConfigurationFile);
componentList.add(rdButton_ConfigureNewPropertyFile);
PagePanel buttonPanel = new PagePanel(componentList,new GroupLayout(this));
this.add(buttonPanel, BorderLayout.CENTER);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
validate();
}
}
public class PagePanel extends JPanel {
public PagePanel(List<JComponent> componentList, LayoutManager layOutManager) {
this.setBackground(Color.decode("#4E6987"));
if (layOutManager != null) {
this.setLayout(null);
}
for (JComponent jComponent : componentList) {
jComponent.setBackground(Color.decode("#4E6987"));
this.add(jComponent);
}
}
}
由于提前 拉维·库马尔
是的,这是可能的。虽然为什么它不会发生在你身边,为此你需要准确地展示你在做什么!这将有助于任何人回答你,所以你的一些代码将非常感激。默认情况下'JFrame具有BorderLayout'和'JPanel具有FLowLayout',所以不需要做任何事情,只需在你的'JPanel'上添加组件并将其添加到你的'JFrame'中而不需要其他任何行,你将得到“YES”作为答案您的问题:-) –
当然,您可以为层次结构的每个组件分配不同的布局管理器。默认情况下,JPanel使用FlowLayout。如果遇到麻烦,请发布[SSCCE](http://sscce.org)以获得更好的帮助。 –