该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
源代码:
package cn.edu.lsu;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class RadixSwitch {
public JFrame frame;
private JTextField fromTf, toTf;
private JRadioButton fromEr, fromBa, fromShi, fromShiLiu, toEr, toBa,
toShi, toShiLiu;
private ButtonGroup fromBg, toBg;
private JLabel fromLabel, toLabel;
private JButton btnOK, btnReset;
private Judge judge = new Judge();
private int locationWidth,locationHeight;//位置
private GetScreenSize getScreenSize = new GetScreenSize();
public RadixSwitch() {
MyActionListener myActionListener = new MyActionListener();
locationWidth = (int)(getScreenSize.width * 0.305);
locationHeight = (int)(getScreenSize.height * 0.26);
frame = new JFrame("进制转换");
fromTf = new JTextField(35);
fromTf.setHorizontalAlignment(4);
toTf = new JTextField(35);
toTf.setEditable(false);
toTf.setHorizontalAlignment(4);
fromEr = new JRadioButton("二进制");
fromBa = new JRadioButton("八进制");
fromShi = new JRadioButton("十进制");
fromShi.setSelected(true);
fromShiLiu = new JRadioButton("十六进制");
fromBg = new ButtonGroup();
fromBg.add(fromEr);
fromBg.add(fromBa);
fromBg.add(fromShi);
fromBg.add(fromShiLiu);
toEr = new JRadioButton("二进制");
toBa = new JRadioButton("八进制");
toShi = new JRadioButton("十进制");
toShi.setSelected(true);
toShiLiu = new JRadioButton("十六进制");
toBg = new ButtonGroup();
toBg.add(toEr);
toBg.add(toBa);
toBg.add(toShi);
toBg.add(toShiLiu);
fromLabel = new JLabel("选择输入的进制: ");
toLabel = new JLabel("选择输出的进制: ");
btnOK = new JButton("确定转换");
btnOK.addActionListener(myActionListener);
btnReset = new JButton("清空数据");
btnReset.addActionListener(myActionListener);
frame.add(fromTf);
frame.add(fromLabel);
frame.add(fromEr);
frame.add(fromBa);
frame.add(fromShi);
frame.add(fromShiLiu);
frame.add(toLabel);
frame.add(toEr);
frame.add(toBa);
frame.add(toShi);
frame.add(toShiLiu);
frame.add(toTf);
frame.add(btnReset);
frame.add(btnOK);
frame.setLayout(new FlowLayout());
frame.setSize(420, 200);
frame.setVisible(true);
frame.setResizable(false);
frame.setLocation(locationWidth, locationHeight);
frame.setAlwaysOnTop(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
class MyActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnReset) {
setNull();
fromShi.setSelected(true);
toShi.setSelected(true);
}
if (e.getSource() == btnOK) {
if ((!judge.isShi(fromTf.getText().toString()) && !fromShiLiu.isSelected())
|| (fromShiLiu.isSelected() && !judge.isShiLiu(fromTf.getText().toString()))
|| (fromEr.isSelected() && !judge.isEr(fromTf.getText().toString()))
|| (fromBa.isSelected() && !judge.isBa(fromTf.getText().toString()))) {
new ShowError(frame);//弹出错误提示对话框
setNull();
}
if (fromBa.isSelected() && toEr.isSelected()) {// 八进制——》二进制
toTf.setText("" + Integer.toBinaryString(Integer.parseInt(fromTf.getText().toString(), 8)));
}
if (fromShi.isSelected() && toEr.isSelected()) {// 十进制——》二进制
toTf.setText("" + Integer.toBinaryString(Integer.parseInt(fromTf.getText().toString())));
}
if (fromShiLiu.isSelected() && toEr.isSelected()) {// 十六进制——》二进制
toTf.setText("" + Integer.toBinaryString(Integer.parseInt(fromTf.getText().toString(), 16)));
}
if (fromEr.isSelected() && toBa.isSelected()) {// 二进制——》八进制
toTf.setText("" + Integer.toOctalString(Integer.parseInt(fromTf.getText().toString(), 2)));
}
if (fromShi.isSelected() && toBa.isSelected()) {// 十进制——》八进制
toTf.setText("" + Integer.toOctalString(Integer.parseInt(fromTf.getText())));
}
if (fromShiLiu.isSelected() && toBa.isSelected()) {// 十六进制——》八进制
toTf.setText("" + Integer.toOctalString(Integer.parseInt(fromTf.getText().toString(), 16)));
}
if (fromEr.isSelected() && toShi.isSelected()) {// 二进制——》十进制
toTf.setText("" + Integer.parseInt(fromTf.getText().toString(), 2));
}
if (fromBa.isSelected() && toShi.isSelected()) {// 八进制——》十进制
toTf.setText("" + Integer.parseInt(fromTf.getText().toString(), 8));
}
if (fromShiLiu.isSelected() && toShi.isSelected()) {// 十六进制——》十进制
toTf.setText("" + Integer.parseInt(fromTf.getText().toString(), 16));
}
if (fromEr.isSelected() && toShiLiu.isSelected()) {// 二进制——》十六进制
toTf.setText("" + Integer.toHexString(Integer.parseInt(fromTf.getText().toString(), 2)));
}
if (fromBa.isSelected() && toShiLiu.isSelected()) {// 八进制——》十六进制
toTf.setText("" + Integer.toHexString(Integer.parseInt(fromTf.getText().toString(), 8)));
}
if (fromShi.isSelected() && toShiLiu.isSelected()) {// 十进制——》十六进制
toTf.setText("" + Integer.toHexString(Integer.parseInt(fromTf.getText())));
}
if (fromShi.isSelected() && toShi.isSelected()
|| fromEr.isSelected() && toEr.isSelected()
|| fromBa.isSelected() && toBa.isSelected()
|| fromShiLiu.isSelected() && toShiLiu.isSelected()) {// 十进制——》十进制
toTf.setText(fromTf.getText().toString());
}
}
}
}
public void setNull(){
fromTf.setText("");
toTf.setText("");
}
}