使用BorderLayout

时间:2016-09-20 00:13:04

标签: java user-interface

我想在labelName下添加labelAddress,但它位于右侧。我想继续在另一个下添加新标签,它就像招聘申请表一样。我使用BorderLayout来告诉每个jlabel的位置,但它不会去我想要的地方。 这应该是这样的:谢谢

  Enter your name: _________________

  Enter your address: _________________
    ........

这是我的代码:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class Home extends JFrame {

    private JButton btnClear, btnNext;
    private JTextField txtName, txtAddress, txtCity, txtState,
            txtZipCode, txtPhoneNumber, txtEmail;
    private JComboBox txtDate, txtYear, txtMonth;
    private JLabel labelName, labelAddress, labelCity, labelState,
            labelZipCode, labelPhoneNumber, labelEmail;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Home().setVisible(true);
            }
        });
    }

    public Home() {
        createView();

        setTitle("Job Hiring Systems");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(600, 600);
        setMinimumSize(new Dimension(600, 600));
        setLocationRelativeTo(null);
        setResizable(false);
    }

    private void createView() {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.setBorder(new EmptyBorder(25, 10, 25, 10));
        getContentPane().add(panel);

        /*
                 * North Panel
         */
        JPanel northPanel = new JPanel();
        northPanel.setBackground(Color.GRAY);
        panel.add(northPanel, BorderLayout.NORTH);

        JLabel labelWelcomeMessage = new JLabel("Welcome to the Job Hiring Systems");
        labelWelcomeMessage.setFont(new Font("Serif", Font.BOLD, 25));
        northPanel.add(labelWelcomeMessage, BorderLayout.CENTER);

        /*
                 * End of North Panel
         */

 /*
                 * Center Panel
         */
        JPanel centerPanel = new JPanel();
        centerPanel.setBackground(Color.ORANGE);
        panel.add(centerPanel, BorderLayout.CENTER);

        labelName = new JLabel("Enter your name: ");
        //labelName.setBorder(new EmptyBorder(25,50,10,10));
        labelName.setFont(new Font("Serif", Font.PLAIN, 15));
        centerPanel.add(labelName, BorderLayout.EAST);

        txtName = new JTextField(25);
        txtName.setHorizontalAlignment(JTextField.CENTER);
        txtName.setHorizontalAlignment(SwingConstants.LEFT);
        centerPanel.add(txtName, BorderLayout.EAST);

        labelAddress = new JLabel("Enter you address: ");
        labelAddress.setFont(new Font("Serif", Font.PLAIN, 15));
        // labelAddress.setBorder(new EmptyBorder(0,0,0,20)); // maybe add this, but doesn't align with the labelName
        centerPanel.add(labelAddress, BorderLayout.CENTER);

        txtAddress = new JTextField(25);
        txtAddress.setHorizontalAlignment(JTextField.CENTER);
        txtAddress.setHorizontalAlignment(SwingConstants.LEFT);
        centerPanel.add(txtAddress, BorderLayout.EAST);

        /*
                 * South Panel
         */
        JPanel southPanel = new JPanel();
        southPanel.setBackground(Color.GREEN);
        panel.add(southPanel, BorderLayout.SOUTH);

        btnClear = new JButton("CLEAR");
        southPanel.add(btnClear, BorderLayout.CENTER);

        btnNext = new JButton("NEXT");
        southPanel.add(btnNext, BorderLayout.WEST);

        /*
                 * End of South Panel
         */
    }

}

1 个答案:

答案 0 :(得分:1)

获取预期结果可以使用GridBagLayout类将组件添加到JPanel

   /*
             * Center Panel
     */
  JPanel centerPanel = new JPanel();
  centerPanel.setBackground(Color.yellow);
  centerPanel.setSize(300,300);
  GridBagLayout layout = new GridBagLayout();

  centerPanel.setLayout(layout);        
  GridBagConstraints gbc = new GridBagConstraints();

  gbc.fill = GridBagConstraints.HORIZONTAL;
  gbc.gridx = 0;
  gbc.gridy = 0;
  labelName = new JLabel("Enter your name: ");
    //labelName.setBorder(new EmptyBorder(25,50,10,10));
  labelName.setFont(new Font("Serif", Font.PLAIN, 15));
  centerPanel.add(labelName,gbc);

  gbc.gridx = 1;
  gbc.gridy = 0;
  txtName = new JTextField(25);
  txtName.setHorizontalAlignment(JTextField.CENTER);
  txtName.setHorizontalAlignment(SwingConstants.LEFT);
  centerPanel.add(txtName,gbc); 

  gbc.fill = GridBagConstraints.HORIZONTAL;
  gbc.ipady = 20;   
  gbc.gridx = 0;
  gbc.gridy = 1;
  labelAddress = new JLabel("Enter you address: ");
  labelAddress.setFont(new Font("Serif", Font.PLAIN, 15));
  centerPanel.add(labelAddress,gbc); 

  gbc.gridx = 1;
  gbc.gridy = 1;  
  txtAddress = new JTextField(25);
  txtAddress.setHorizontalAlignment(JTextField.CENTER);
  txtAddress.setHorizontalAlignment(SwingConstants.LEFT);
  centerPanel.add(txtAddress,gbc);  



  panel.add(centerPanel);