양식을 생성하기 위해 찾은 샘플 코드를 편집하려고합니다. 샘플 코드에는 4 개의 레이블과 텍스트 필드가 완벽하게 정렬되어 있습니다. 마지막에 버튼을 추가하려고하지만 버튼 대신 화면의 왼쪽 상단 모서리에있는 레이블이 겹쳐 있습니다. 이 문제를 어떻게 해결할 수 있습니까?Jframe 레이블 및 단추를 정렬하는 방법
public class SpringDemo {
private static void createAndShowGUI() {
String[] labels = {"Name: ", "Fax: ", "Email: ", "Address: "};
int labelsLength = labels.length;
//Create and populate the panel.
JPanel p = new JPanel(new SpringLayout());
for (int i = 0; i < labelsLength; i++) {
JLabel l = new JLabel(labels[i], JLabel.TRAILING);
p.add(l);
JTextField textField = new JTextField(10);
l.setLabelFor(textField);
p.add(textField);
}
JButton l = new JButton("Submit");
p.add(l);
//Lay out the panel.
SpringUtilities.makeCompactGrid(p,
labelsLength, 2, //rows, cols
7, 7, //initX, initY
7, 7); //xPad, yPad
//Create and set up the window.
JFrame frame = new JFrame("SpringForm");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
p.setOpaque(true); //content panes must be opaque
frame.setContentPane(p);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
이것은 완벽하게 작동했습니다. 고맙습니다 :) – MrSnare