여기에 몇 가지 코드가 있습니다.JTextFields를 제대로 표시 할 수 없으므로이를 수정해야합니다.
홈페이지 :
import javax.swing.SwingUtilities;
public class Main {
public static void main(String [] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Panel panel = new Panel();
}
});
}
}
패널 :
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
public class Panel extends JPanel{
private JFrame frame;
private JLabel label1;
private JLabel label2;
private JLabel label3;
private JTextField textField1;
private JTextField textField2;
private JTextField textField3;
private JButton button1;
private JButton button2;
private JButton button3;
private JTable table;
public Panel() {
label1 = new JLabel("label1");
label2 = new JLabel("label2");
label3 = new JLabel("label3");
textField1 = new JTextField("textField1", 20);
textField2 = new JTextField("textField2", 20);
textField3 = new JTextField("textField3", 100);
button1 = new JButton("Hello");
button2 = new JButton("Goodbye");
button3 = new JButton("Love");
table = new JTable(20,20);
frame = new JFrame("My application");
frame.setSize(1000, 1000);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setVisible(true);
this.setOpaque(true);
setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
gc.weightx = 1;
gc.weighty =1;
gc.fill = GridBagConstraints.NONE;
gc.gridx = 0;
gc.gridy = 0;
// gc.anchor = GridBagConstraints.WEST;
add(label1, gc);
gc.gridx = 2;
gc.gridy = 0;
//gc.anchor = GridBagConstraints.EAST;
add(textField1, gc);
gc.gridx = 0;
gc.gridy = 1;
//gc.anchor = GridBagConstraints.WEST;
add(label2, gc);
gc.gridx = 2;
gc.gridy = 1;
//gc.anchor = GridBagConstraints.EAST;
add(textField2, gc);
gc.gridx = 0;
gc.gridy = 2;
//gc.anchor = GridBagConstraints.WEST;
add(label3, gc);
gc.gridx = 2;
gc.gridy = 2;
//gc.anchor = GridBagConstraints.EAST;
add(textField3, gc);
gc.gridx = 0;
gc.gridy = 3;
//gc.anchor = GridBagConstraints.WEST;
add(button1, gc);
gc.gridx = 1;
gc.gridy = 3;
gc.anchor = GridBagConstraints.CENTER;
add(button2, gc);
gc.gridx = 2;
gc.gridy = 3;
// gc.anchor = GridBagConstraints.EAST;
add(button3, gc);
gc.gridx = 1;
gc.gridy = 4;
gc.anchor = GridBagConstraints.CENTER;
add(table, gc);
frame.add(this);
}
}
http://madbean.com/anim/totallygridbag/ – irreputable
이미지 –