java - Gridbaglayout、Insets或anchor不会生效

标签 java swing layout-manager gridbaglayout insets

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

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

此外,主面板内的“我的帐户”按钮、左面板和右面板不会获得我应用的插图。

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

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);
}
}

提前致谢。我确信我错过了一些东西,但我不知道。

enter image description here

INSETS 不适用。 =(??

更新:

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

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 的任何插入。

enter image description here

最佳答案

添加组件时应用约束

add(topTabbedPane, gbc);

关于java - Gridbaglayout、Insets或anchor不会生效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34708778/

相关文章:

java - 如何比较 android SD 卡上本地文件夹的内容与 dropbox 上文件夹的内容?

java - 在 SQLite 数据库上使用 Java 的 JDBCPreparedStatement 查询 NOT NULL

java - 如何访问:app class from library?

java - 如何在Swing中实现一个 "In Progress"的动画?

java - 如何覆盖单个按钮的 Nimbus 按钮边距

java - 如何将 JFreeChart 插入到 JFrame 中具有空布局的 JPanel 中?

java - java中使用谓词(java.util.function.Predicate)的 "best case scenario"是什么

java - BorderLayout 与 jlabel 并排

java - GridLayout 没有填满整个窗口

java - SwingUtilities.invokeLater()