0

好的,所以我需要你们的帮助。我不知道我错过了什么,但即使我已将布局设置为 GridBag,插图和锚点也没有生效。

我需要将注销按钮放在选项卡窗格的正上方,并将注销按钮放在右上角。换句话说,选项卡式窗格的位置 gridx = 0, gridy = 1; 和注销按钮位置 gridx = 0, gridy = 0;

此外,主面板内的 myaccount 按钮、leftpanel 和 rightpanel 不会得到我应用的插图。

我错过了什么。请帮忙,因为我是这个布局的新手。

TopPanel.java

package MainComponents;

import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.border.Border;
import MainTab_TabbedPane.TopTabbedPane;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;

public class TopPanel extends JPanel {
//DECLARATION
JButton logOutButton = new JButton("Logout");
TopTabbedPane topTabbedPane = new TopTabbedPane();
private final Border myLineBorder = BorderFactory.createLineBorder(Color.BLACK, 2);

//CONSTRUCTOR    
public TopPanel(){
    setPanelInitialProperties();
    addComponents();
}

//METHODS
private void setPanelInitialProperties(){
    setLayout(new GridBagLayout());
    setBorder(myLineBorder); //sets a Line Border for this panel
}

private void addComponents(){
    GridBagConstraints gbc = new GridBagConstraints();

    gbc.gridx = 0;
    gbc.gridy = 1;
    this.add(topTabbedPane); //adds TabbedPane holding Home, Administration... to this Top Panel

    gbc.gridx = 0;
    gbc.gridy = 0;
    this.add(logOutButton);
}

}

HomeTopPanel.java

package HomeTab;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.Border;

public class HomeTopPanel extends JPanel {

private final JButton MyAccountButton = new JButton("My Account");
private final JPanel leftPanel = new JPanel(new GridBagLayout());
private final JPanel rightPanel = new JPanel(new GridBagLayout());
private final Border leftPanelLineBorder =   BorderFactory.createLineBorder(Color.BLACK, 2);
private final Border rightPanelLineBorder = BorderFactory.createLineBorder(Color.BLACK, 2);

//CONSTRUCTOR
public HomeTopPanel(){
    constructMyAccountButton();
    constructPanels();
    setLeftRightPanelBorders();
    this.setLayout(new GridBagLayout());
}

private void constructMyAccountButton(){
    GridBagConstraints MyAccountButton_gbc = new GridBagConstraints();
    MyAccountButton_gbc.gridx = 0; MyAccountButton_gbc.gridy = 0; 
    MyAccountButton_gbc.anchor = GridBagConstraints.NORTHWEST;
    this.add(MyAccountButton);
}

private void constructPanels(){
    GridBagConstraints leftPanelgbc = new GridBagConstraints();
    GridBagConstraints rightPanelgbc = new GridBagConstraints();

    leftPanelgbc.insets = new Insets(3,3,3,3);
    leftPanelgbc.gridx = 1; leftPanelgbc.gridy = 0;
    leftPanel.setPreferredSize(new Dimension(300, 500));
    this.add(leftPanel);

    rightPanelgbc.insets = new Insets(3,3,3,3);
    rightPanelgbc.gridx = 2; leftPanelgbc.gridy = 0;
    rightPanel.setPreferredSize(new Dimension(300, 500));
    this.add(rightPanel);

}

private void setLeftRightPanelBorders(){
    leftPanel.setBorder(leftPanelLineBorder);
    rightPanel.setBorder(rightPanelLineBorder);
    this.setBorder(leftPanelLineBorder);
}
}

先谢谢了。我确定我错过了一些东西,但我不知道。

在此处输入图像描述

插图将不适用。=(??

更新:

我使用以下代码添加了插图:

private void constructPanels(){
    GridBagConstraints gbc2 = new GridBagConstraints();

    gbc2.gridx = 1; gbc2.gridy = 0; 
    gbc2.insets = new Insets(5, 5, 5, 5);
    leftPanel.setPreferredSize(new Dimension(250, 300));
        this.add(leftPanel,gbc2);

    gbc2.gridx = 2; gbc2.gridy = 0; 
    gbc2.insets = new Insets(5, 5, 5, 5);    
    rightPanel.setPreferredSize(new Dimension(300, 500));
        this.add(rightPanel,gbc2);

}

但仍然没有得到 5 的任何插图。

在此处输入图像描述

4

2 回答 2

2

添加组件时应用约束

add(topTabbedPane, gbc);
于 2016-01-10T17:54:05.327 回答
2
GridBagConstraints MyAccountButton_gbc = new GridBagConstraints();

变量名不应以大写字符开头。你的大多数其他名字都是正确的。那么就没有理由马虎了。遵循 Java 约定。

constructMyAccountButton();
constructPanels();
setLeftRightPanelBorders();
this.setLayout(new GridBagLayout());

必须在将组件添加到面板之前设置布局。

GridBagConstraints MyAccountButton_gbc = new GridBagConstraints();
MyAccountButton_gbc.gridx = 0; MyAccountButton_gbc.gridy = 0; 
MyAccountButton_gbc.anchor = GridBagConstraints.NORTHWEST;
//this.add(MyAccountButton); // where is the constraint?
this.add(MyAccountButton, myAccountButton_gbc);

您实际上必须使用约束。

于 2016-01-10T18:04:29.727 回答