【问题标题】:JTabbedPane panels won't line up correctlyJTabbedPane 面板无法正确排列
【发布时间】:2014-01-05 23:18:29
【问题描述】:

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

如下所示,我将用于行标题的 JLabels 放在一个面板中,将用于信息的 JLabels 放在右侧面板中。问题在于它们不会彼此分离。我希望左标签左对齐,它们一起应该占据整个可用面板大小。 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;
}

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

【问题讨论】:

    标签: java layout user-interface jpanel jtabbedpane


    【解决方案1】:

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

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

    例如...

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-05
      • 1970-01-01
      • 2021-09-03
      • 2023-02-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多