帮忙写一个java程序,用户输入一个十进制数,转换为一个十六进制数 用户输入一个八进制数转化为十六

2024-11-19 14:26:49
推荐回答(1个)
回答(1):

public class Calc extends Frame implements ActionListener {
TextField text1, text3, text4, text5;
Button btn;

public Calc() {
setLayout(new GridLayout(7, 3));
// 第一行
text1 = new TextField(10);
add(text1);

btn = new Button("do");
btn.addActionListener(this);
add(btn);

text3 = new TextField(30);
add(text3);

text4 = new TextField(30);
add(text4);

text5 = new TextField(30);
add(text5);

}

public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(btn)) {
text3.setText("二进制数是:" + String.valueOf(Integer.toBinaryString(Integer.valueOf(text1.getText()))));
text4.setText("八进制数是:" + String.valueOf(Integer.toOctalString(Integer.valueOf(text1.getText()))));
text5.setText("十六进制数是:" + String.valueOf(Integer.toHexString(Integer.valueOf(text1.getText()))).toUpperCase());
}
}

public static void main(String[] agrs) {
Calc my = new Calc();
my.setSize(250, 150);
my.setVisible(true);
}

}