2016-09-20 7 views
0

labelNameの下にlabelAddressを追加したいのですが、それはその右側にあります。私は新しいラベルを別のラベルの下に追加し続けたいと思っています。これは採用申請書のようなものです。私はBorderLayoutを使って各ラベルの位置を教えましたが、それは私が望むところには行っていません。 これはどのようにすべきです。おかげでここ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

期待される結果がJPanelの

にコンポーネントを追加する GridBagLayoutクラスを使用することができます得るために、
/* 
      * 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); 
+0

ありがとうございました –

関連する問題