Java 进制转换工具源码
源码
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.function.BinaryOperator;
public class test2 extends JPanel {
static final int WIDTH=200;
static final int HEIGHT=200;
JTextField text1;
JTextField text2;
JTextField text3;
JTextField text4;
JTextField text5;
public void add(Component c,GridBagConstraints constrains,int x,int y,int w,int h){
//此方法用来添加控件到容器中
constrains.gridx=x;
constrains.gridy=y;
constrains.gridwidth=w;
constrains.gridheight=h;
add(c,constrains);
}
public test2(){
JFrame frame = new JFrame();//创建顶层容器
frame.setTitle("进制转换工具");//顶层容器名称
frame.setSize(WIDTH,HEIGHT);//设置顶层容器大小
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭功能
frame.setResizable(false);//设置窗口大小固定
Toolkit kit=Toolkit.getDefaultToolkit();//设置顶层容器框架为居中
Dimension screenSize=kit.getScreenSize();
int width=screenSize.width;
int height=screenSize.height;
int x=(width-WIDTH)/2;
int y=(height-HEIGHT)/2;
frame.setLocation(x,y);
frame.setContentPane(this);
JRadioButton jr1 = new JRadioButton("二进制");
JRadioButton jr2 = new JRadioButton("八进制");
JRadioButton jr3 = new JRadioButton("十进制");
JRadioButton jr4 = new JRadioButton("十六进制");
ButtonGroup bg = new ButtonGroup();
bg.add(jr1);
bg.add(jr2);
bg.add(jr3);
bg.add(jr4);
JLabel label1=new JLabel("输入待转换数值:");
JLabel label2=new JLabel("二进制");
JLabel label3=new JLabel("八进制");
JLabel label4=new JLabel("十进制");
JLabel label5=new JLabel("十六进制");
JLabel label6=new JLabel("输入数值的进制");
JLabel label7=new JLabel("转换结果");
text1=new JTextField(10);
text2=new JTextField(10);
text3=new JTextField(10);
text4=new JTextField(10);
text5=new JTextField(10);
JButton compute=new JButton("开始转换");
setLayout(new GridBagLayout()); //设置布局方式
GridBagConstraints constrains = new GridBagConstraints(); //创建布局约束对象
constrains.fill = GridBagConstraints.NONE; //不填充,保持原状
constrains.anchor = GridBagConstraints.WEST; //组件大于分配空间时向左侧缩小
constrains.weightx = 100;
constrains.weighty = 100;
add(label6,constrains,2,0,7,1);
add(label7,constrains,18,0,4,1);
add(jr1,constrains,2,2,4,1);
add(jr2,constrains,2,4,4,1);
add(jr3,constrains,2,6,4,1);
add(jr4,constrains,2,8,4,1);
add(label1,constrains,2,10,7,1);
add(text1,constrains,2,12,10,1);
add(label2,constrains,8,2,3,1);
add(text2,constrains,12,2,10,1);
add(label3,constrains,8,4,3,1);
add(text3,constrains,12,4,10,1);
add(label4,constrains,8,6,3,1);
add(text4,constrains,12,6,10,1);
add(label5,constrains,8,8,3,1);
add(text5,constrains,12,8,10,1);
add(compute,constrains,12,10,13,9);
frame.setVisible(true);//设置顶层容器可见
compute.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int radix = 0;
String d = text1.getText();
if(jr1.isSelected()){
radix = 2;
}else if(jr2.isSelected()){
radix = 8;
}else if(jr3.isSelected()){
radix = 10;
}else if(jr4.isSelected()){
radix = 16;
}
int d1 = Integer.parseInt(d,radix);
text2.setText("" + Integer.toBinaryString(d1));
text3.setText("" + Integer.toOctalString(d1));
text4.setText("" + d1);
text5.setText("" + Integer.toHexString(d1));
}
});
}
public static void main(String[] args){
new test2();
}
}
运行
![进制转换工具](https://i.loli.net/2020/12/20/GVRtS1BYApFUD3w.png)