JButton被点击时如何获取到JTextField中的内容
当点击JButton时如何获取到JTextField中的内容 --------------------编程问答-------------------- 上代码public static void main(String[] args) {
JFrame jFrame = new JFrame();
JPanel jPanel = new JPanel();
jPanel.setLayout(new FlowLayout());
jFrame.setSize(400, 300);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = jFrame.getSize();
// textarea sets
JTextArea textArea = new JTextArea();
textArea.setText("是否关机");
Font font = new Font("textFont", Font.BOLD, 90);
textArea.setSize(frameSize.width, frameSize.height / 2);
textArea.setFont(font);
jPanel.add(textArea);
// JButton
JButton button_Y = new JButton("是");
JButton button_N = new JButton("否");
button_Y.setFont(new Font("button_Y", Font.BOLD, 40));
button_N.setFont(new Font("button_Y", Font.BOLD, 40));
button_Y.setPreferredSize(new Dimension(180, 70));
button_N.setPreferredSize(new Dimension(180, 70));
button_N.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
jPanel.add(button_Y);
jPanel.add(button_N);
// 关机延时时间
JTextArea textArea_ = new JTextArea("关机延时时间:");
textArea_.setFont(new Font("text_", Font.BOLD, 20));
jPanel.add(textArea_);
// inputText
JTextField delayTime = new JTextField("100");
delayTime.setPreferredSize(new Dimension(50, 30));
jPanel.add(delayTime);
// 秒
JTextArea textArea_1 = new JTextArea("秒");
textArea_1.setFont(new Font("text_1", Font.BOLD, 20));
jPanel.add(textArea_1);
// frame sets
jFrame.add(jPanel);
jFrame.setVisible(true);
jFrame.setTitle("关机");
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
delay = Integer.parseInt(delayTime.getText().trim());
button_Y.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Runtime.getRuntime().exec("shutdown -s -t " + delay);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
jFrame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
} --------------------编程问答-------------------- 先把界面放着,先学别的,最后学界面。 --------------------编程问答-------------------- 添加按钮响应事件
继承接口 ActionListener然后实现actionPerformed方法
{
文本框.getText()
} --------------------编程问答--------------------
--------------------编程问答-------------------- 3楼正解~~~~~~~~~~ --------------------编程问答-------------------- 按钮的话,实现ActionListener易做图接口就可以了,上面我写的多余了
JTextField jtf = new JTextField("");
//添加易做图
jtf.geDocument().addDocumentListener(DocumentTestListener e);
class DocumentTestListener implements DocumentListener
{
public void insertUpdate(DocumentEvent e)
{
jtf.getText();
}
public void removeUpdate(DocumentEvent e)
{
jtf.getText();
}
public void changedUpdate(DocumentEvent e)
{
//nothing
}
}
补充:Java , Java SE