나는 GridBagLayout으로 장난하고있다. 나는 그것을 다소 이해했으며, 따라서이 배치를 만들 수있었습니다. 그러나 나의 존재는 내 것과 일치하지 않는다. 여기 화면이 있습니다.GridBagConstraints를 사용하는 방법과 방법을 정확하게 이해할 수 없습니다. 있는 그대로 대해야한다
있는 그대로 :(
은 다음과 같아야의
내가 크기가 (setSize()
)을 설정되도록 내가 그것을 조금 조정할 필요가 있음을 이해하지만 진짜 까다로운. 하나는 상단 중앙에있을 JLabel
"연락처 추가하기"지고 있습니다.
당신의 응답을 기다리는 중. 사전에 감사.
package SimpleCRUD;
import java.awt.Component;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class ContactListFrame extends JFrame{
JButton Button1, Button2;
JTextField textField1, textField2, textField3;
JLabel label1, label2, label3 , label4;
GridBagLayout layout = new GridBagLayout();
GridBagConstraints Constraint = new GridBagConstraints();
public ContactListFrame() {
super("All Contacts");
Button1 = new JButton("Add");
Button2 = new JButton("Cancel");
textField1 = new JTextField(15);
textField2 = new JTextField(15);
textField3 = new JTextField(15);
label4 = new JLabel("Add Contact");
label4.setFont (new Font("fallan", 1, 25));
label1 = new JLabel("First Name:");
label2 = new JLabel("Last Name:");
label3 = new JLabel("Phone Number:");
setLayout(layout);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setSize(400, 200);
setResizable(false);
Constraint.fill = GridBagConstraints.NONE;
Constraint.anchor = GridBagConstraints.NORTH;
addComponent(label4, 0, 1, 1, 1);
addComponent(textField1, 1, 1, 1, 1);
addComponent(textField2, 2, 1, 1, 1);
addComponent(textField3, 3, 1, 1, 1);
addComponent(label1, 1, 0, 1, 1);
addComponent(label2, 2, 0, 1, 1);
addComponent(label3, 3, 0, 1, 1);
addComponent(Button1, 4, 0, 2, 1);
addComponent(Button2, 4, 1, 2, 1);
}
public void addComponent (Component comp, int row, int col, int width, int height){
Constraint.gridx = col;
Constraint.gridy = row;
Constraint.gridwidth = width;
Constraint.gridheight = height;
layout.setConstraints(comp, Constraint);
add(comp);
}
}
모양을 사용하도록 수정이야? –
SwingConstants.CENTER를 두 번째 매개 변수로 사용하여 '연락처 추가'JLabel을 구성 했습니까? 그리드 폭을 가지고 놀았습니까? – Doug
Java 6에서 소개 된 GroupLayout을 확인해보십시오. GrindBagLayout이 수행하는 대부분의 작업을 수행 할 수 있으며 IMO를 사용하기가 훨씬 쉽습니다. –