我有一个 jframe,其中包含三个文本字段和两个单选按钮(用于选择是或否)。
它应该是这样的:
但是,使用我的代码,它是这样的:
我的代码:
public class editFrame extends JFrame {
JButton saveButton;
JButton cancelButton;
JRadioButton radioB1;
JRadioButton radioB2;
public editFrame() {
JPanel wrapper = new JPanel();
wrapper.add(createForm());
add(wrapper, BorderLayout.WEST);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(100, 50, 500, 600);
this.setVisible(true);
}
public JPanel createForm() {
String[] labels = {"ID", "Name", "Date"};
JPanel panel = new JPanel();
JTextField idtf = new JTextField(10);
JTextField nametf = new JTextField(10);
JTextField datetf = new JTextField(10);
panel.add(idtf);
panel.add(nametf);
panel.add(datetf);
radioB1 = new JRadioButton("Yes");
radioB2 = new JRadioButton("No");
ButtonGroup group = new ButtonGroup();
group.add(radioB1);
group.add(radioB2);
panel.add(radioB1);
panel.add(radioB2);
saveButton = new JButton("update");
cancelButton = new JButton("Cancel");
panel.add(saveButton);
panel.add(cancelButton);
SpringLayout sL = new SpringLayout();
panel.setLayout(sL);
SpringUtilities.makeCompactGrid(panel, 3, 2, 100, 50, 15, 20);
return panel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
editFrame edF = new editFrame();
}
});
}
借用状态文本不显示在单选按钮旁边。我的第二个主要问题是使用此代码,我无法访问文本字段。我需要稍后访问文本字段...