package chapter11_3; import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; public class Calculator extends JFrame implements ActionListener { private String[] str = { "7", "8", "9", "/", "sqrt", "4", "5", "6", "*", "%", "1", "2", "3", "-", "1/x", "0", "+/-", ".", "+", "=" }; private JTextField tf_out; private JButton jb_bk, jb_ce, jb_c; private JButton[] jb_key; private char ch = '#'; private boolean can = false; private double num1; public void creatGUI() { tf_out = new JTextField(); tf_out.setHorizontalAlignment(JTextField.RIGHT); tf_out.setColumns(18); tf_out.setEditable(false); // 设置输出不可编辑 tf_out.setText("0"); this.add(tf_out, BorderLayout.NORTH); JPanel p = new JPanel(new BorderLayout(3, 8)); JPanel p1 = new JPanel(new GridLayout(1, 3, 3, 10)); p.add(p1, "North"); jb_bk = new JButton("Backspace"); jb_bk.setForeground(Color.RED); jb_bk.addActionListener(this); jb_ce = new JButton("CE"); jb_ce.setForeground(Color.RED); jb_ce.addActionListener(this); jb_c = new JButton("C"); jb_c.setForeground(Color.RED); jb_c.addActionListener(this); p1.add(jb_bk); p1.add(jb_ce); p1.add(jb_c); JPanel p2 = new JPanel(new GridLayout(4, 5, 3, 3)); p.add(p2, BorderLayout.CENTER); jb_key = new JButton[str.length]; for (int i = 0; i < str.length; i++) { jb_key[i] = new JButton(str[i]); jb_key[i].addActionListener(this); if (i == 3 || i == 8 || i == 13 || i == 18 || i == 19) { jb_key[i].setForeground(Color.RED); } else { jb_key[i].setForeground(Color.BLUE); } p2.add(jb_key[i]); } this.add(p, BorderLayout.CENTER); this.setTitle("计算器"); this.setIconImage(new ImageIcon("image/1.jpg").getImage()); this.setBackground(Color.LIGHT_GRAY); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setBounds((1280 - 300) / 2, (768 - 200) / 2, 308, 225); this.setVisible(true); } public static void main(String[] args) { new Calculator().creatGUI(); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String act = e.getActionCommand(); // 控制键 if (e.getSource() == jb_bk) { if (tf_out.getText().length() > 1){ tf_out.setText(tf_out.getText().substring(0, tf_out.getText().length() - 1)); }else{ tf_out.setText("0"); } return; }else if (e.getSource() == jb_ce || e.getSource() == jb_c) { tf_out.setText("0"); ch = '#'; return; } // 数字键 if (act == "0") { if (!tf_out.getText().equals("0")) { tf_out.setText(tf_out.getText()); } } else if (act == "1" || act == "2" || act == "3" || act == "4" || act == "5" || act == "6" || act == "7" || act == "9") { tf_out.setText(tf_out.getText()); } // 运算符 if (act.equals("+/-")) { if (tf_out.getText().charAt(0) != '-') { tf_out.setText("-" + tf_out.getText()); } else { tf_out.setText(tf_out.getText().substring(1)); } } else if (act.equals(".")) { tf_out.setText(tf_out.getText() + act); ch = '#'; } else if (act != "1/x" && act.charAt(0) >= '0' && act.charAt(0) <= '9') { if (can) { tf_out.setText(act); can = false; } else { try { if (Double.parseDouble(tf_out.getText()) == 0) { if (tf_out.getText().equals("0.")) { tf_out.setText(tf_out.getText() + act); } else { tf_out.setText(act); } } else { tf_out.setText(tf_out.getText() + act); } } catch (NumberFormatException e1) { JOptionPane.showMessageDialog(null, "输入格式错误!", "警告!",JOptionPane.ERROR_MESSAGE); } } } else if (act.equals("+") || act.equals("-") || act.equals("*") || act.equals("/")) { if (ch != '#') { try { num1 = operation(num1, ch, Double.parseDouble(tf_out.getText())); tf_out.setText(String.valueOf(num1)); ch = act.charAt(0); can = true; return; } catch (NumberFormatException e1) { JOptionPane.showMessageDialog(null, "输入格式错误!", "警告!",JOptionPane.ERROR_MESSAGE); return; } } else { try { num1 = Double.parseDouble(tf_out.getText()); ch = act.charAt(0); can = true; return; } catch (NumberFormatException e1) { JOptionPane.showMessageDialog(null, "输入格式错误!", "警告!",JOptionPane.ERROR_MESSAGE); return; } } } else if (act.equals("sqrt")) { try { double num = (double) Math.sqrt(Double.parseDouble(tf_out.getText())); tf_out.setText(String.valueOf(num)); can = true; return; } catch (NumberFormatException e1) { JOptionPane.showMessageDialog(null, "输入格式错误!", "警告!",JOptionPane.ERROR_MESSAGE); } } else if (act.equals("1/x")) { try { double num = 1 / Double.parseDouble(tf_out.getText()); tf_out.setText(String.valueOf(num)); can = true; return; } catch (NumberFormatException e1) { JOptionPane.showMessageDialog(null, "输入格式错误!", "警告!",JOptionPane.ERROR_MESSAGE); return; } catch (ArithmeticException e1) { JOptionPane.showMessageDialog(null, "除0错误!", "警告!", JOptionPane.ERROR_MESSAGE); } } else if (act.equals("=")) { can = true; try { if (ch == '#') { return; } double num = Double.parseDouble(tf_out.getText()); num1 = operation(num1, ch, num); tf_out.setText(String.valueOf(num1)); ch = '#'; return; } catch (NumberFormatException e1) { JOptionPane.showMessageDialog(null, "输入格式错误!", "警告!",JOptionPane.ERROR_MESSAGE); return; } } else if (act.equals("%")) { double num = Double.valueOf(tf_out.getText()).doubleValue(); tf_out.setText(String.valueOf(num1)); double sum = (num1 * num) / 100; tf_out.setText(String.valueOf(sum)); return; } } // 运算 public double operation(double a, char c, double b) { double sum; switch (c) { case '+': sum = a + b; break; case '-': sum = a - b; break; case '*': sum = a * b; break; case '/': if (b == 0) { JOptionPane.showMessageDialog(null, "除0错误!", "警告!",JOptionPane.ERROR_MESSAGE); return 0; } sum = a / b; break; default: return 0; } return sum; } }
2022-06-16 15:24:16 7KB JAVA 计算器
1
java反编译源码京东核心 JD-Core 是用 JAVA 编写的 JAVA 反编译器。 Java Decompiler 项目主页: 京东核心源代码: JCenter Maven 存储库: 描述 JD-Core 是一个独立的 JAVA 库,包含“Java Decompiler 项目”的 JAVA 反编译器。 它支持 Java 1.1.8 到 Java 12.0,包括 Lambda 表达式、方法引用和默认方法。 JD-Core 是 JD-GUI 的引擎。 如何构建JD-Core? > git clone https://github.com/java-decompiler/jd-core.git > cd jd-core > ./gradlew build 生成“build/libs/jd-core-xyzjar” 如何使用京东核心? 实施界面, 实施界面, 并调用方法“反编译(加载器,打印机,internalTypeName);” 例子 实现加载器接口: Loader loader = new Loader () { @Override public byte [] load ( St
2022-06-15 18:41:01 1.13MB 系统开源
1
java编写的文件管理系统,通过文件路径索引到文件目录以及文件项从而实现对整个磁盘上的文件管理
2022-06-14 09:54:22 20KB java 文件管理
1
自己编写的类似于JFilechooser的java代码
1
java课后题中第九章的题目,没写很好,望大家多多指教,不胜感激!
2022-06-13 15:05:34 2KB 红屋顶,绿树,加小路
1
Java编写的HTML浏览器
2022-06-06 18:01:29 10KB html java 前端 开发语言
Java编写的多人聊天+用户在线即时聊天系统源代码
2022-06-06 18:01:28 59KB Java编写的多人聊天+用户在线
Java编写的网页版魔方游戏
2022-06-06 18:01:27 9KB java 游戏 开发语言
Java编写的显示器显示模式检测程序
2022-06-06 18:01:27 45KB java 源码软件 开发语言
Java HTTP / HTTPS代理服务器 代理服务器 代理服务器是位于客户端和客户端希望从中检索文件的远程服务器之间的服务器。 来自客户端的所有流量都被发送到代理服务器,并且代理服务器代表客户端向远程服务器发出请求。 代理服务器接收到所需文件后,便会将其转发到客户端。 这可能是有益的,因为它允许代理服务器管理员对其网络上的计算机可以执行的操作进行某些控制。 例如,某些网站可能被代理服务器阻止,这意味着客户端将无法访问它们。 这也是有益的,因为代理服务器可以缓存经常访问的页面。 这意味着当客户端(或其他客户端)对已缓存的任何文件进行后续请求时,代理可以立即向其发出文件,而不必从远程服务器请求
2022-06-05 20:07:15 21KB java ssl http blacklist
1