모든 것이 가운데쪽으로 갈겨 대는 것 같지만 왜 그럴 수 없습니까. 여기 내 코드가있다. 가능한 한 쉽게 읽을 수 있도록 노력했습니다.Java GridBagLayout 레이아웃 문제
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JTextField;
/**
*
* @author rubixibuc
*/
public class RulesWindow extends JFrame {
public GridBagLayout layout = new GridBagLayout();
public GridBagConstraints constr = new GridBagConstraints();
public DefaultComboBoxModel modelE = new DefaultComboBoxModel();
public DefaultComboBoxModel modelA = new DefaultComboBoxModel();
public DefaultListModel modelL = new DefaultListModel();
public RulesWindow()
{
super("Rules Window");
/* layout for window
|_P_|__E__|_T_|__A__|_a_|
| |xxx|
| |xxx|
| |xxx|
|_________L_________|_M_|
X = no fill
P = pattern field
E = extension combobox
T = target action
A = action combobox
a = add rule
M = remove rule
L = rule list
*/
setLayout(layout);
JTextField P = new JTextField(); // 0,0,1,1
JComboBox E = new JComboBox(modelE); // 1,0,2,1
JTextField T = new JTextField(); // 3,0,1,1
JComboBox A = new JComboBox(modelA); // 4,0,2,1
JButton a = new JButton("+"); // 6,0,1,1
JList L = new JList(modelL); // 1,1,6,4
JButton M = new JButton("-"); // 6,4,1,1
changeConstraints(0,0,1,1);
add(P);
changeConstraints(1,0,2,1);
add(E);
changeConstraints(3,0,1,1);
add(T);
changeConstraints(4,0,2,1);
add(A);
changeConstraints(6,0,1,1);
add(a);
changeConstraints(1,1,6,4);
add(L);
changeConstraints(6,4,1,1);
add(M);
setSize(200,200);
setVisible(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public final void changeConstraints(int gridx, int gridy, int gridwidth, int gridheight)
{
constr.anchor = GridBagConstraints.NORTHEAST;
constr.fill = GridBagConstraints.BOTH;
constr.gridx = gridx;
constr.gridy = gridy;
constr.gridwidth = gridwidth;
constr.gridheight = gridheight;
}
@Override
public final Component add(Component comp)
{
layout.setConstraints(comp, constr);
super.add(comp);
return comp;
}
}
미리 감사드립니다. 저는 코멘트에서 어떻게 보이는지를 도표로 나타 냈습니다.
또한 오버라이드 된 메서드를 올바르게 호출하는 방법입니까?
방금 마지막 두 가지 방법을 변경하여 나중에 문제가 발생할 것이라고 생각했습니다. – rubixibuc