我试图通过点击我的 JButton 从我的 JTextField 中获取一个 int,但我不知道该怎么做。我正在尝试获取 int 并将其设置为一个变量,以便我可以在我的程序中进一步使用它。
这是代码(这是整个方法):
JFrame presets = new JFrame("Presets");
presets.setVisible(true);
presets.setSize(500, 500);
JPanel gui = new JPanel(new BorderLayout(2,2));
JPanel labelFields = new JPanel(new BorderLayout(2,2));
labelFields.setBorder(new TitledBorder("Presets"));
JPanel labels = new JPanel(new GridLayout(0,1,1,1));
JPanel fields = new JPanel(new GridLayout(0,1,1,1));
labels.add(new JLabel("Place values on Cat.2/Cat.3 at"));
JTextField f1 = new JTextField(10);
String text = f1.getText();
int first = Integer.parseInt(text);
labels.add(new JLabel("and place follow up value at"));
fields.add(new JTextField(10));
labelFields.add(labels, BorderLayout.CENTER);
labelFields.add(fields, BorderLayout.EAST);
JPanel guiCenter = new JPanel(new BorderLayout(2,2));
JPanel submit = new JPanel(new FlowLayout(FlowLayout.CENTER));
submit.add( new JButton("Submit") );
guiCenter.add( submit, BorderLayout.NORTH );
gui.add(labelFields, BorderLayout.NORTH);
gui.add(guiCenter, BorderLayout.CENTER);
JOptionPane.showMessageDialog(null, gui);