好久不写界面了给你一个怎么触发按Button的操作吧,之后你自己写吧import java.awt.Button; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;import javax.swing.JFrame; public class Test extends JFrame { private Button btn = new Button("This is a button");
public Test() { btn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { btn.setLabel("Clicked"); } });
this.add(btn); }
public static void main(String[] args) { Test t = new Test(); t.setSize(400, 300); t.setVisible(true); } }
不懂如何对文本框赋值。 public void actionPerformed(ActionEvent ae) { if(ae.getSource()==number0) { //比如这里,当点击了0按钮,文本框怎么出现O呢?
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JFrame;
public class Test extends JFrame { private Button btn = new Button("This is a button");
public Test() {
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
btn.setLabel("Clicked");
}
});
this.add(btn);
}
public static void main(String[] args) {
Test t = new Test();
t.setSize(400, 300);
t.setVisible(true);
}
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==number0)
{
//比如这里,当点击了0按钮,文本框怎么出现O呢?
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==number0)
{
//按下number0按钮了,文本框怎么出现 0 这个数字呢??初学JAVA 很多类还是不太清楚。
文本框的名字.setText(number0.getText());
http://apps.hi.baidu.com/share/detail/23202056
public class Tst extends JFrame {
private static final long serialVersionUID = 4034670144827122207L;
/** * @param args */
private static JPanel panel = null;
private static JButton button = null;
private static JTextField field = null;
private static Tst tst = null;
public static void main(String[] args) {
tst = new Tst();
tst.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
button = new JButton("1");
field = new JTextField(" ");
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
String str = button.getText();
field.setText(str);
}
});
panel.add(field);
panel.add(button);
tst.add(panel);
tst.setSize(400, 400);
tst.setVisible(true);
}
}
主要就是获取按钮的text,然后赋值给textfield就OK了,看着办吧,给分哦,嘿嘿!
JTextField text = new JTextField();
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
text.setText("数值");
}
});