JAVA 简单进制转换器
乱搞的..#
JAVA作业写了个进制转换器...写出来的东西还蛮怪的
/*
* Author:graykido
* Coding:GBK
* */
package Caculater;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundImage;
import javax.rmi.ssl.SslRMIClientSocketFactory;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class caculater extends JFrame {
ImageIcon icon;
JLabel label;
//输入文本框
JTextField text;
//数字按钮
JPanel numPanl;
JButton numButton[];
//输入和输出进制的框
JPanel buttonPanel;
//输出进制单选框
JLabel outbuttonlabel;
JPanel outbuttonPanel;
ButtonGroup obtg;
//输入进制单选框
JLabel inbuttonlabel;
JPanel inbuttonPanel;
ButtonGroup ibtg;
//将文本框和进制框放在一个jpanel内
JPanel thPanel;
//当前输入进制
int IHex;
caculater(String title) {
super(title);
//默认输入是十进制
this.IHex = 10;
Container ctpn = getContentPane();
//设置布局
GridLayout layout = new GridLayout(2, 2);
this.setLayout(layout);
//设置文本框
text = new JTextField();
text.setBorder(BorderFactory.createRaisedBevelBorder());
text.setFont(new Font("宋体", Font.BOLD, 25));
//设置标题图标
this.icon = new ImageIcon("src\\Caculater\\go.png");
icon.setDescription("gogogo");
Image img = this.icon.getImage();
this.setIconImage(img);
this.setLocation(100, 200);
this.setSize(1000, 600);
//设置菜单
JMenuBar jmb = new JMenuBar();
JMenu edit = new JMenu("编辑");
JMenuItem edit1 = new JMenuItem("施工中");
edit.add(edit1);
jmb.add(edit);
JMenu view = new JMenu("查看");
JMenuItem view1 = new JMenuItem("施工中");
view.add(view1);
jmb.add(view);
JMenu help = new JMenu("帮助");
JMenuItem help1 = new JMenuItem("施工中");
help.add(help1);
jmb.add(help);
this.setJMenuBar(jmb);
//图标
ImageIcon icon1 = new ImageIcon("src/Caculater/left.gif");
ImageIcon icon2 = new ImageIcon("src/Caculater/right.gif");
buttonPanel = new JPanel();
//输出进制单选框
outbuttonPanel = new JPanel();
obtg = new ButtonGroup();
JLabel outL = new JLabel("输出进制", icon1, JLabel.CENTER);
outbuttonPanel.add(outL);
outaddRadioButton("二进制", 2);
outaddRadioButton("八进制", 8);
outaddRadioButton("十进制", 10);
outaddRadioButton("十六进制", 16);
//输入进制单选框
inbuttonPanel = new JPanel();
ibtg = new ButtonGroup();
JLabel inL = new JLabel("输入进制", icon2, JLabel.CENTER);
inbuttonPanel.add(inL);
inaddRadioButton("二进制", 2);
inaddRadioButton("八进制", 8);
inaddRadioButton("十进制", 10);
//数字按钮
this.numPanl = new JPanel();
this.numButton = new JButton[20];
for (int i = 0; i < 10; i++) {
numButton[i] = addJButton(i);
numPanl.add(numButton[i]);
}
//清空文本框按钮
JButton clr = new JButton("clr");
clr.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
text.setText("");
}
}); //对于数字按钮以及清空按钮排版
numPanl.add(clr);
numPanl.setLayout(new GridLayout(4, 4));
buttonPanel.setLayout(new GridLayout(2, 1));
buttonPanel.add(outbuttonPanel);
buttonPanel.add(inbuttonPanel);
thPanel = new JPanel(new GridLayout(1, 2));
thPanel.add(text);
thPanel.add(buttonPanel);
// ctpn.add(text);
ctpn.add(thPanel);
// ctpn.add(outbuttonPanel);
// ctpn.add(inbuttonPanel);
ctpn.add(numPanl);
//关闭进程
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public JButton addJButton(int num) {
JButton button = new JButton("" + num);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
try {
Integer.parseInt(text.getText());
} catch (Exception e) {
text.setText("");
}
text.setText(text.getText() + num);
validate();
}
});
return button;
}
public void outaddRadioButton(String title, int Hex) {
JRadioButton button = new JRadioButton(title);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
//触发进制转换
try {
long num = Long.parseLong(text.getText());
//先将框内内容转为十进制
long temp = num;
long mut = 1;
long res = 0;
if (IHex != 10) {
while (temp != 0) {
res += (temp % 10 * mut);
mut *= IHex;
temp /= 10;
}
} else {
res = num;
}
num = res;
String ans = "";
while (num != 0) {
// ans += String.valueOf(num % Hex < 10 ? num % Hex : num % Hex - 10 + 'A');
ans += (char) (num % Hex < 10 ? num % Hex + '0' : num % Hex - 10 + 'A');
num /= Hex;
}
text.setText(new StringBuffer(ans).reverse().toString());
} catch (NumberFormatException e) {
text.setText("请输入数字或检查输入数字范围是否过大");
} catch (Exception e) {
text.setText("" + e);
}
}
});
obtg.add(button);
outbuttonPanel.add(button);
}
public void inaddRadioButton(String title, int Hex) {
JRadioButton button = new JRadioButton(title);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
IHex = Hex;
for (int i = 0; i < 10; i++) {
numButton[i].setEnabled(true);
}
for (int i = Hex; i < 10; i++) {
numButton[i].setEnabled(false);
}
}
});
inbuttonPanel.add(button);
ibtg.add(button);
}
public static void main(String[] args) {
caculater fr = new caculater("计算器");
}
}
一个小小的update#
结果因为把IDEA突然弄崩了鼓捣了一晚上导致心态炸裂
/*
* Author:graykido
* Coding:GBK
* */
package Caculater;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundImage;
import javax.rmi.ssl.SslRMIClientSocketFactory;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.plaf.synth.SynthEditorPaneUI;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class caculater extends JFrame {
ImageIcon icon;
JLabel label;
//输入文本框
JTextField text;
//数字按钮
JPanel numPanl;
JButton numButton[];
//进制和數字框
JPanel buttonPanel;
//进制
JLabel inbuttonlabel;
JPanel inbuttonPanel;
ButtonGroup ibtg;
//当前进制
int IHex;
caculater(String title) {
super(title);
//默认输入是十进制
this.IHex = 10;
Container ctpn = getContentPane();
//设置布局
this.setLayout(new BorderLayout());
//设置文本框
text = new JTextField();
text.setBorder(BorderFactory.createRaisedBevelBorder());
text.setFont(new Font("宋体", Font.BOLD, 25));
ctpn.add("North", text);
//设置标题图标
this.icon = new ImageIcon("src\\Caculater\\go.png");
icon.setDescription("gogogo");
Image img = this.icon.getImage();
this.setIconImage(img);
this.setLocation(100, 200);
this.setSize(400, 500);
//设置菜单
JMenuBar jmb = new JMenuBar();
JMenu edit = new JMenu("编辑");
JMenuItem edit1 = new JMenuItem("施工中");
edit.add(edit1);
jmb.add(edit);
JMenu view = new JMenu("查看");
JMenuItem view1 = new JMenuItem("施工中");
view.add(view1);
jmb.add(view);
JMenu help = new JMenu("帮助");
JMenuItem help1 = new JMenuItem("About Me");
help1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
JOptionPane.showMessageDialog(null, "graykido");
}
});
help.add(help1);
jmb.add(help);
this.setJMenuBar(jmb);
//图标
ImageIcon icon1 = new ImageIcon("src/Caculater/left.gif");
ImageIcon icon2 = new ImageIcon("src/Caculater/right.gif");
buttonPanel = new JPanel(new BorderLayout());
//进制单选框
inbuttonPanel = new JPanel();
ibtg = new ButtonGroup();
JLabel inL = new JLabel("进制", icon2, JLabel.CENTER);
inbuttonPanel.add(inL);
inaddRadioButton("二进制", 2);
inaddRadioButton("八进制", 8);
inaddRadioButton("十进制", 10);
inaddRadioButton("十六进制", 16);
buttonPanel.add("North", inbuttonPanel);
//输入进制单选框
this.numPanl = new JPanel(new GridLayout(5, 5));
this.numButton = new JButton[20];
for (int i = 0; i < 16; i++) {
numButton[i] = addJButton(i);
numPanl.add(numButton[i]);
}
//清空文本框按钮
JButton clr = new JButton("clr");
clr.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
text.setText("");
}
});
JButton numadd = new JButton("+");
numadd.setEnabled(false);
JButton numsub = new JButton("-");
numsub.setEnabled(false);
JButton nummul = new JButton("*");
nummul.setEnabled(false);
JButton numdiv = new JButton("/");
numdiv.setEnabled(false);
JButton numeql = new JButton("=");
numeql.setEnabled(false);
numPanl.add(clr);
numPanl.add(numdiv);
numPanl.add(nummul);
numPanl.add(numsub);
numPanl.add(numadd);
numPanl.add(numeql);
buttonPanel.setPreferredSize(new Dimension(200, 200));
buttonPanel.add("Center", this.numPanl);
ctpn.add("Center", buttonPanel);
//关闭进程
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public JButton addJButton(int num) {
JButton button;
if (num >= 10) {
button = new JButton("" + (char) ((byte) 'A' + num - 10));
} else {
button = new JButton("" + num);
}
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
text.setText(text.getText() + button.getText());
validate();
}
});
return button;
}
public void inaddRadioButton(String title, int Hex) {
JRadioButton button = new JRadioButton(title);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
try {
String Txt = text.getText();
long temp = 0;
long mut = 1;
long res = 0;
for (int i = Txt.length() - 1; i >= 0; i--) {
if (Character.isUpperCase(Txt.charAt(i))) {
temp += ((byte) Txt.charAt(i) - (byte) 'A' + 10) * mut;
} else {
temp += ((byte) (Txt.charAt(i)) - (byte) ('0')) * mut;
}
mut *= IHex;
}
res = temp;
String ans = "";
while (res != 0) {
// ans += String.valueOf(res % Hex < 10 ? res % Hex : res % Hex - 10 + 'A');
ans += (char) (res % Hex < 10 ? res % Hex + '0' : res % Hex - 10 + 'A');
res /= Hex;
}
text.setText(new StringBuffer(ans).reverse().toString());
IHex = Hex;
for (int i = 0; i < 16; i++) {
numButton[i].setEnabled(true);
}
for (int i = Hex; i < 16; i++) {
numButton[i].setEnabled(false);
}
} catch (NumberFormatException e) {
text.setText("请输入数字或检查输入数字范围是否过大");
} catch (Exception e) {
text.setText("" + e);
}
}
});
inbuttonPanel.add(button);
ibtg.add(button);
}
public static void main(String[] args) {
caculater fr = new caculater("计算器");
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET开发智能桌面机器人:用.NET IoT库编写驱动控制两个屏幕
· 用纯.NET开发并制作一个智能桌面机器人:从.NET IoT入门开始
· 一个超经典 WinForm,WPF 卡死问题的终极反思
· ASP.NET Core - 日志记录系统(二)
· .NET 依赖注入中的 Captive Dependency
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(二):用.NET IoT库
· 几个自学项目的通病,别因为它们浪费了时间!
· 在外漂泊的这几年总结和感悟,展望未来
· .NET 数据拷贝方案选择
· .net工作流elsa-书签