黑马程序员技术交流社区
标题:
哪能帮我下,我的按扭事件怎么不好用
[打印本页]
作者:
姚伟涛
时间:
2012-4-13 11:32
标题:
哪能帮我下,我的按扭事件怎么不好用
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Zhuanhuan extends Frame{
MenuBar menuBar =new MenuBar();
Panel p1 = new Panel();
Panel p2 = new Panel();
Menu m = new Menu("Application");
Label l1 = new Label("Enter Decimal:");
Label l2 = new Label("Converted Value:");
TextField t1 = new TextField(40);
TextField t2 = new TextField(50);
Button b1 = new Button("Binary");
Button b2 = new Button("Octal");
Button b3 = new Button("Hex");
Button b4 = new Button("Base36");
public Zhuanhuan (){
this.setMenuBar(menuBar);
menuBar.add(m);
this.setLayout(new FlowLayout());
this.add(l1);
this.add(t1);
this.add(l2);
this.add(t2);
this.add(p2);
p2.setLayout(new GridLayout(1,4));
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
this.setSize(500,200);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
Zhuanhuan.this.dispose();
}
});
}
public void addactionlistener(){
b1.addActionListener(buttonhandler);
b2.addActionListener(buttonhandler);
b3.addActionListener(buttonhandler);
b4.addActionListener(buttonhandler);
}
ActionListener buttonhandler = new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource() == b1){
int tz1 = Integer.parseInt(t1.getText());
//t2.setEditable(true);
t2.setText(Integer.toString(tz1,2));
}
else if(e.getSource() == b2){
int tz1 = Integer.parseInt(t1.getText());
t2.setText(Integer.toString(tz1,8));
}
else if(e.getSource() == b3){
int tz1 = Integer.parseInt(t1.getText());
t2.setText(Integer.toString(tz1,16));
}
else if(e.getSource() == b4){
int tz1 = Integer.parseInt(t1.getText());
t2.setText(Integer.toString(tz1,36));
}
}
};
public static void main(String[] args) {
new Zhuanhuan();
}
}
复制代码
作者:
薛飞飞
时间:
2012-4-13 12:22
import java.awt.*;
import java.awt.event.*;
public class Zhuanhuan extends Frame{
MenuBar menuBar =new MenuBar();
Panel p1 = new Panel();
Panel p2 = new Panel();
Menu m = new Menu("Application");
Label l1 = new Label("Enter Decimal:");
Label l2 = new Label("Converted Value:");
TextField t1 = new TextField(40);
TextField t2 = new TextField(50);
Button b1 = new Button("Binary");
Button b2 = new Button("Octal");
Button b3 = new Button("Hex");
Button b4 = new Button("Base36");
public Crisis (){
this.setMenuBar(menuBar);
menuBar.add(m);
this.setLayout(new FlowLayout());
this.add(l1);
this.add(t1);
this.add(l2);
this.add(t2);
this.add(p2);
p2.setLayout(new GridLayout(1,4));
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
this.setSize(500,200);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
Zhuanhuan.this.dispose();
}
});
addactionlistener();--------------->
你按钮事件没有添加...当然点了没反应..加上这句就好了
}
public void addactionlistener(){
b1.addActionListener(buttonhandler);
b2.addActionListener(buttonhandler);
b3.addActionListener(buttonhandler);
b4.addActionListener(buttonhandler);
}
ActionListener buttonhandler = new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource() == b1){
int tz1 = Integer.parseInt(t1.getText());
//t2.setEditable(true);
t2.setText(Integer.toString(tz1,2));
}
else if(e.getSource() == b2){
int tz1 = Integer.parseInt(t1.getText());
t2.setText(Integer.toString(tz1,8));
}
else if(e.getSource() == b3){
int tz1 = Integer.parseInt(t1.getText());
t2.setText(Integer.toString(tz1,16));
}
else if(e.getSource() == b4){
int tz1 = Integer.parseInt(t1.getText());
t2.setText(Integer.toString(tz1,36));
}
}
};
public static void main(String[] args) {
new Zhuanhuan();
}
}
作者:
τ、高童鞋ゝ
时间:
2012-4-13 13:18
this.setSize(500,200);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.addactionlistener();-------------------------->没有调用方法
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2