所以我不知道如何将提交按钮放置在文本框下方,并且在我最初运行它时,窗口打开的很小。同样,当按下“提交”按钮时,getText不起作用。
我已经尝试过.putConstraint(),但是却没能定位按钮。
/ ** *在这里编写类Springdemo1的描述。 * * @author(您的名字) * @version(版本号或日期) * / 公共课Springdemo1 { 公共Springdemo1() { 在里面(); }
public static void init()
{
JFrame f = new JFrame ("Answer Box");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = f.getContentPane();
SpringLayout layout = new SpringLayout();
f.getContentPane().setLayout(layout);
JLabel label = new JLabel("Enter answer here: ");
JPanel p = new JPanel (new SpringLayout());
f.add(p);
p.add(label);
JTextArea textfield = new JTextArea ("",5, 5);
label.setLabelFor(textfield);
JButton button = new JButton("Submit");
p.add(new JLabel());
layout.putConstraint(SpringLayout.WEST, contentPane,
10, SpringLayout.WEST, textfield);
layout.putConstraint(SpringLayout.NORTH, contentPane,
10, SpringLayout.NORTH, textfield);
layout.putConstraint(SpringLayout.EAST, contentPane,
10, SpringLayout.EAST, textfield);
layout.putConstraint(SpringLayout.SOUTH, contentPane,
10, SpringLayout.SOUTH, textfield);
p.add(textfield);
layout.putConstraint(SpringLayout.SOUTH, f.getContentPane(), 30, SpringLayout.SOUTH, button);
layout.putConstraint(SpringLayout.EAST, f.getContentPane(), 100, SpringLayout.EAST, button);
layout.putConstraint(SpringLayout.NORTH, f.getContentPane(), 100, SpringLayout.NORTH, button);
//layout.putConstraint(SpringLayout.WEST, contentPane, 30, SpringLayout.WEST, button);
p.add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.out.println(label+"->"+textfield.getText());
}
});
JFrame frame = new JFrame("Answer Box");
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) {
init();
}
}
我希望该按钮位于教科书下面,并且当按下“提交”按钮时,我想将文本字段中的内容存储到变量中。