简体   繁体   English

另一个内部有多个JPanel

[英]Multiple JPanel inside another

look here I have to make a format like this, like make a 1,2 panel, then put a 3,1 panel on the left side. 看这里,我必须制作这样的格式,例如制作1,2面板,然后在左侧放置3,1面板。 Then, I put 4 text fields in the middle left panel and one button in the bottom left panel. 然后,我在左中间的面板中放置4个文本字段,在左下方的面板中放置一个按钮。

I'm not sure if I can just use GridLayout or if I have to use BorderLayout too. 我不确定是否只能使用GridLayout还是必须使用BorderLayout。

How can I get this organized because when I set it up, I can't get the layout right. 如何进行整理,因为在设置时无法正确布局。 The textfields are not in the right position, they end up on the right side. 文本字段不在正确的位置,它们最终出现在右侧。

 public class CreatePanel extends JPanel { private Vector projectList; private JButton button1; private ProjectSpendingPanel spendingPanel; private JFrame frame1; JPanel panel1; JPanel leftPanel; JPanel subPanel; GridLayout layout1; BorderLayout layout2; GridLayout layout3; JLabel message; JLabel labelName; JLabel labelNumber; JLabel labelLocation; JLabel labelFunding; JTextField textField1; JTextField textField2; JTextField textField3; JTextField textField4; //Constructor initializes components and organize them using certain layouts public CreatePanel(Vector projectList, ProjectSpendingPanel spendingPanel) { this.projectList = projectList; this.spendingPanel = spendingPanel; //organize components here layout1 = new GridLayout(1,2); layout2 = new BorderLayout(1,3); layout3 = new GridLayout(4,2); this.setLayout(layout1); panel1 = new JPanel(layout1); leftPanel = new JPanel(layout3); subPanel = new JPanel(layout2); add(panel1); panel1.add(subPanel); panel1.add(leftPanel); labelName = new JLabel("Project Name"); leftPanel.add(labelName); textField1 = new JTextField("", 15); leftPanel.add(textField1,BorderLayout.SOUTH); labelName = new JLabel("Project Number"); leftPanel.add(labelName); textField2 = new JTextField("",15); leftPanel.add(textField2); labelLocation = new JLabel("Project Location"); leftPanel.add(labelLocation); textField3 = new JTextField("",15); leftPanel.add(textField3); labelFunding = new JLabel("Initial Funding"); leftPanel.add(labelFunding); textField4 = new JTextField("",15); leftPanel.add(textField4); add(leftPanel); button1 = new JButton("Create a project"); subPanel.add(button1,BorderLayout.SOUTH); } 

To get the result you probably intended, you need to tweak your code just a little. 为了获得您可能想要的结果,您需要稍微调整一下代码。

Your lines 你的台词

panel1 = new JPanel(layout1);
leftPanel = new JPanel(layout3);
subPanel = new JPanel(layout2);

should be altered to read 应该改为阅读

panel1 = new JPanel(new GridLayout(2, 1));
leftPanel = new JPanel(layout3);
subPanel = new JPanel(layout2);

GridLayout takes rows first, then columns. GridLayout首先获取行,然后获取列。 Your layout1 defines one row and two columns but you use it for the text fields and the button, so I assume the two panels need to be above each other. 您的layout1定义了一行和两列,但是您将其用于文本字段和按钮,因此我假设两个面板必须位于彼此上方。

Second, you need to change the order from 其次,您需要更改订单

add(panel1);
panel1.add(subPanel);
panel1.add(leftPanel);

to

add(panel1);
panel1.add(leftPanel);
panel1.add(subPanel);

Finally, I think you need to change 最后,我认为你需要改变

add(leftPanel);
button1 = new JButton("Create a project");

to

add(spendingPanel);
button1 = new JButton("Create a project");

Adding leftPanel again seems to destroy the layout. 再次添加leftPanel似乎会破坏布局。 If you do not want to see your spendingPanel here, you could use new JPanel() instead. 如果您不想在这里看到您的spendingPanel面板,则可以改用new JPanel()

Here is a screenshot of the running program 这是正在运行的程序屏幕截图

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

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