讀值我想讀從Java中textfied值,但我不能讀它 這裏是我的代碼從文本字段
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.*;
public class TextField extends JDialog {
TextField() {
JFrame frm = new JFrame("SAMPLE PROGRAM");
frm.setBounds(150,150,420,400);
frm.setLayout(null);
Container content = frm.getContentPane();
content.setBackground(Color.cyan);
JTextField text = new JTextField();
text.setBounds(70,25,100,30);
JButton button1, button2;
button1 = new JButton("PROGRAMMER");
button2 = new JButton("USER");
button1.setBounds(270,25,120,50);
button2.setBounds(270,90,120,50);
button1.addActionListener(new ButtonHandler());
button2.addActionListener(new ButtonHandler());
frm.add(button1);
frm.add(button2);
frm.add(text);
frm.setVisible(true);
frm.setResizable(false);
}
public static void main(String[] args) {
new TextField();
}
class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
String str = new String();
str = e.getActionCommand();
System.out.println(" " + str);
}
}
}
我嘗試以下方法
1.In類文本字段我用button2.addactionlistener.It根據本方法得到的誤差
無法指非最終可變文本在不同的方法中定義的內部類內部
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if(text.getText().equals("joe"))
2.In類ButtonHandler
它說,文本無法解析
我應該用什麼方法來讀取文本框,並在類應該在閱讀
您能否讓您的課程更清晰些,並輸入所有必要的代碼?第一類沒有辦法。 – Russell 2012-01-28 10:48:27
爲什麼所有'>'字符都在代碼行之前?今後請多做點努力,那個地段就像狗的早餐一樣。 – 2012-01-28 11:07:10
沒辦法解讀基本的java教程/教科書並學習它的內容:-) http://docs.oracle.com/javase/tutorial/index.html – kleopatra 2012-01-28 11:10:21