JTabbedPane面板无法正确排列

时间:2013-12-17 23:41:48

标签: java layout user-interface jpanel jtabbedpane

我正在尝试创建一个JTabbedPane,它可以显示与对象(在本例中为一个人)相关的所有字段以及与之相关的所有固有字段。这样做我试图让第一个面板(我目前遇到的问题)包含姓名,电话号码和电子邮件。

如下所示,我在一个面板中放置了行标题的JLabel,在右侧面板中放置了JLabel信息。这个问题是他们不会彼此分开。我希望左边的标签左对齐,它们应该占据整个可用的面板尺寸。 setAlignment似乎没有任何效果。

这是文本面板创建的代码:

    private static JComponent makeTextPanel(String text){
    JPanel panel = new JPanel(false);
    JLabel filler = new JLabel(text);
    filler.setHorizontalAlignment(JLabel.CENTER);
    panel.setLayout(new GridLayout(1,1));
    panel.add(filler);
    return panel;
}

这是第二篇TabbedPane的代码:

    public static void tabbedReadMenu(){
    JFrame readMenu = new JFrame("Student Records");
    JTabbedPane tabbedPane = new JTabbedPane();
    Dimension d = new Dimension(300,300);
    tabbedPane.setPreferredSize(d);

    JComponent basicInfoPane = makeTextPanel("Basic Info");
    tabbedPane.addTab("Basic Info", BasicInfoTab());
    tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);

    readMenu.add(tabbedPane);

    readMenu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    readMenu.pack();
    readMenu.setLocationRelativeTo(null);
    readMenu.setVisible(true);

}

这是实际制表符和嵌套面板的信息:

   private static JPanel BasicInfoTab(){
    JPanel basicInfoTab = new JPanel();
    JPanel leftPanel = new JPanel();
    JPanel rightPanel = new JPanel();
    JPanel buttons = new JPanel();
    JPanel spacing = new JPanel();
    JPanel title = new JPanel();
    JPanel mainPanel = new JPanel();

    LayoutManager singleLine = new GridLayout(5,1);
    leftPanel.setLayout(singleLine);
    rightPanel.setLayout(singleLine);
    buttons.setLayout(new FlowLayout());
    mainPanel.setLayout(new SpringLayout());

    spacing.setLayout(new GridLayout(3,1));


    JLabel jspacing = new JLabel("          ");
    JLabel jspacing1 = new JLabel("          ");
    JLabel jspacing2 = new JLabel("          ");
    spacing.add(jspacing);
    spacing.add(jspacing1);
    spacing.add(jspacing2);
    spacing.setLayout(new FlowLayout());
    JLabel header = new JLabel("Basic Student Information");
    title.add(header);

    JLabel FNLabel = new JLabel("First Name: ");                //Creates all Labels
    JLabel MILabel = new JLabel("Middle Initial: ");
    JLabel LNLabel = new JLabel("Last Name: ");
    JLabel IDLabel = new JLabel("Student ID: ");
    JLabel EmailLabel = new JLabel("E-mail Address: ");


    leftPanel.add(FNLabel);
    leftPanel.add(MILabel);
    leftPanel.add(LNLabel);
    leftPanel.add(IDLabel);
    leftPanel.add(EmailLabel);

    selectedStudent = SaveFunction.passStudent();
    JLabel FNInfo = new JLabel(selectedStudent.getFirstName());
    JLabel MIInfo = new JLabel(selectedStudent.getMidInitial());
    JLabel LNInfo = new JLabel(selectedStudent.getLastName());
    JLabel IDInfo = new JLabel(selectedStudent.getID());
    JLabel EmailInfo = new JLabel(selectedStudent.getEmail());

    rightPanel.add(FNInfo);
    rightPanel.add(MIInfo);
    rightPanel.add(LNInfo);
    rightPanel.add(IDInfo);
    rightPanel.add(EmailInfo);
    FNLabel.setHorizontalAlignment(JLabel.LEFT);
    MILabel.setHorizontalAlignment(JLabel.LEFT);
    LNLabel.setHorizontalAlignment(JLabel.LEFT);
    IDLabel.setHorizontalAlignment(JLabel.LEFT);
    EmailLabel.setHorizontalAlignment(JLabel.LEFT);

    JButton edit = new JButton("Edit");
    JButton close = new JButton("Close");
    buttons.add(edit);
    buttons.add(close);

    basicInfoTab.add(title, BorderLayout.NORTH);
    basicInfoTab.add(spacing, BorderLayout.CENTER);
    basicInfoTab.add(leftPanel, BorderLayout.WEST);
    basicInfoTab.add(rightPanel, BorderLayout.EAST);
    basicInfoTab.add(buttons, BorderLayout.SOUTH);  



    return basicInfoTab;
}

非常感谢任何帮助(包括关于我应该如何改进的任何更一般的编码提示)。

1 个答案:

答案 0 :(得分:0)

基本上,对于表单设计,我几乎坚持GridBagLayout。如果您不介意第三方布局,还应考虑MigLayout

查看How to use GridBagLayout了解更多详情......

例如......

enter image description here

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class FormLayout {

    public static void main(String[] args) {
        new FormLayout();
    }

    public FormLayout() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            JLabel FNLabel = new JLabel("First Name: ");
            JLabel MILabel = new JLabel("Middle Initial: ");
            JLabel LNLabel = new JLabel("Last Name: ");
            JLabel IDLabel = new JLabel("Student ID: ");
            JLabel EmailLabel = new JLabel("E-mail Address: ");

            JLabel FNInfo = new JLabel("My first name");
            JLabel MIInfo = new JLabel("My initial");
            JLabel LNInfo = new JLabel("My last name");
            JLabel IDInfo = new JLabel("My student id");
            JLabel EmailInfo = new JLabel("My Email");

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.insets = new Insets(2, 2, 2, 2);
            gbc.anchor = GridBagConstraints.WEST;

            add(FNLabel, gbc);
            gbc.gridy++;
            add(MILabel, gbc);
            gbc.gridy++;
            add(LNLabel, gbc);
            gbc.gridy++;
            add(IDLabel, gbc);
            gbc.gridy++;
            add(EmailLabel, gbc);

            gbc.gridx = 1;
            gbc.gridy = 0;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.weightx = 1;

            add(FNInfo, gbc);
            gbc.gridy++;
            add(MIInfo, gbc);
            gbc.gridy++;
            add(LNInfo, gbc);
            gbc.gridy++;
            add(IDInfo, gbc);
            gbc.gridy++;
            add(EmailInfo, gbc);                
        }
    }
}