首页 > 代码库 > gui小计算器的程序写法
gui小计算器的程序写法
import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JButton; import java.awt.Color; import javax.swing.JTextArea; import javax.swing.GroupLayout; import javax.swing.GroupLayout.Alignment; import javax.swing.LayoutStyle.ComponentPlacement; import javax.swing.JCheckBox; import javax.swing.JTextField; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.SwingConstants; import java.awt.Font; public class gui1 extends JFrame { private JPanel contentPane; private JTextField textField; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { gui1 frame = new gui1(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public void f1(){ //获取文本框的内容 textField.getText(); //对字符串进行解析(获取你有用的信息)//1+1= //System.out.println(textField.getText()); //1+1= //截取+号之前的数据,+号之后=号之前的数据 int aa1 =(textField.getText()).indexOf("+"); int aa2 =(textField.getText()).indexOf("-"); int aa3 =(textField.getText()).indexOf("*"); int aa4 =(textField.getText()).indexOf("/"); //判断加减乘除 if(-1!=aa1){ int a=(textField.getText()).indexOf("+"); //int b=(textField.getText()).indexOf("+1"); int d=(textField.getText()).indexOf("="); String str1=new String(); String str2=new String(); str1=(textField.getText()).substring(0,a); str2=(textField.getText()).substring(a+1,d); //截取到了之后,结果是个字符串,要进行转换,转换成int int e1=Integer.parseInt(str1); int e2=Integer.parseInt(str2); //执行操作,计算(用方法实现),返回值int gui1 ss=new gui1(); ss.f(e1,e2); textField.setText(""+ss.f(e1,e2)); }else if(-1!=aa2){ int a=(textField.getText()).indexOf("-"); //int b=(textField.getText()).indexOf("+1"); int d=(textField.getText()).indexOf("="); String str1=new String(); String str2=new String(); str1=(textField.getText()).substring(0,a); str2=(textField.getText()).substring(a+1,d); //截取到了之后,结果是个字符串,要进行转换,转换成int int e1=Integer.parseInt(str1); int e2=Integer.parseInt(str2); //执行操作,计算(用方法实现),返回值int gui1 ss=new gui1(); ss.f4(e1,e2); textField.setText(""+ss.f4(e1,e2)); }else if(-1!=aa3){ int a=(textField.getText()).indexOf("*"); //int b=(textField.getText()).indexOf("+1"); int d=(textField.getText()).indexOf("="); String str1=new String(); String str2=new String(); str1=(textField.getText()).substring(0,a); str2=(textField.getText()).substring(a+1,d); //截取到了之后,结果是个字符串,要进行转换,转换成int int e1=Integer.parseInt(str1); int e2=Integer.parseInt(str2); //执行操作,计算(用方法实现),返回值int gui1 ss=new gui1(); ss.f2(e1,e2); textField.setText(""+ss.f2(e1,e2)); }else if(-1!=aa4){ int a=(textField.getText()).indexOf("/"); //int b=(textField.getText()).indexOf("+1"); int d=(textField.getText()).indexOf("="); String str1=new String(); String str2=new String(); str1=(textField.getText()).substring(0,a); str2=(textField.getText()).substring(a+1,d); //截取到了之后,结果是个字符串,要进行转换,转换成int int e1=Integer.parseInt(str1); int e2=Integer.parseInt(str2); //执行操作,计算(用方法实现),返回值int gui1 ss=new gui1(); ss.f3(e1,e2); textField.setText(""+ss.f3(e1,e2)); } //输出结果,settext(string t),字符串,int转成成string,作为入参{String = 1+“1”} //String a1=(String)"a2"; //输出结果:把之前的=号公式,要放在前面 } public int f(int w,int r){ int p=w+r; return p; } public int f4(int w,int r){ int p=w-r; return p; } public int f2(int w,int r){ int p=w*r; return p; } public int f3(int w,int r){ int p=w/r; return p; } public gui1() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 322, 412); contentPane = new JPanel(); contentPane.setBackground(Color.LIGHT_GRAY); contentPane.setForeground(Color.GREEN); contentPane.setBorder(new EmptyBorder(5, 0, 5, 5)); setContentPane(contentPane); JButton btnNewButton_8 = new JButton("m-"); btnNewButton_8.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { } }); btnNewButton_8.setBackground(Color.ORANGE); JButton btnNewButton_6 = new JButton("+"); JButton btnNewButton_18 = new JButton("*"); JButton btnNewButton_20 = new JButton("\u221A "); JButton btnNewButton_22 = new JButton("%"); JButton btnNewButton_21 = new JButton("1/x"); textField = new JTextField(); textField.setColumns(10); JButton btnNewButton = new JButton("MC"); btnNewButton.setBackground(Color.LIGHT_GRAY); JButton btnMr = new JButton("MR"); JButton btnMs = new JButton("MS"); JButton btnM = new JButton("M+"); JButton btnM_1 = new JButton("M-"); JButton button_4 = new JButton("<--"); button_4.setFont(new Font("宋体", Font.PLAIN, 10)); button_4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { } }); JButton button_5 = new JButton("7"); button_5.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText()+"7"); } }); JButton button_6 = new JButton("4"); button_6.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText()+"4"); } }); JButton button_7 = new JButton("1"); button_7.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { textField.setText(textField.getText()+"1"); } }); JButton button_8 = new JButton("0"); button_8.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText()+"0"); String inputValue = JOptionPane.showInputDialog("请输入密码:"); } }); JButton btnCe = new JButton("CE"); JButton button_10 = new JButton("8"); button_10.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText()+"8"); } }); JButton button_11 = new JButton("5"); button_11.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText()+"5"); } }); JButton button_12 = new JButton("2"); button_12.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { textField.setText(textField.getText()+"2"); } }); JButton btnC = new JButton("C"); btnC.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(""); } }); JButton button_14 = new JButton("\u00B1"); JButton button_15 = new JButton("\u221A"); button_15.setFont(new Font("宋体", Font.PLAIN, 11)); button_15.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { textField.setText(textField.getText()+"√"); } }); JButton button_16 = new JButton("9"); button_16.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText()+"9"); } }); JButton button_17 = new JButton("/"); button_17.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText()+"/"); } }); JButton button_18 = new JButton("%"); JButton button_19 = new JButton("6"); button_19.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText()+"6"); } }); JButton button_20 = new JButton("*"); button_20.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText()+"*"); } }); JButton btnx = new JButton("1/x"); btnx.setFont(new Font("微软雅黑", Font.PLAIN, 10)); btnx.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { } }); JButton button_22 = new JButton("3"); button_22.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText()+"3"); } }); JButton button_23 = new JButton("-"); button_23.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText()+"-"); } }); JButton button_24 = new JButton("."); button_24.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText()+"."); } }); JButton button_25 = new JButton("+"); button_25.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText()+"+"); //JOptionPane.showConfirmDialog(null, // "请选择", "titie", JOptionPane.YES_NO_OPTION); } }); JButton button_26 = new JButton("="); button_26.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //等于、、结果输出 textField.setText(textField.getText()+"="); f1(); } }); GroupLayout gl_contentPane = new GroupLayout(contentPane); gl_contentPane.setHorizontalGroup( gl_contentPane.createParallelGroup(Alignment.LEADING) .addGroup(gl_contentPane.createSequentialGroup() .addGap(19) .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) .addGroup(gl_contentPane.createSequentialGroup() .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false) .addGroup(gl_contentPane.createSequentialGroup() .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) .addComponent(button_6, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE) .addComponent(button_7, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(ComponentPlacement.RELATED) .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) .addComponent(button_11, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE) .addComponent(button_12, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE))) .addComponent(button_8, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addPreferredGap(ComponentPlacement.RELATED) .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) .addGroup(gl_contentPane.createSequentialGroup() .addComponent(button_19, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(button_20, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(btnx, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE)) .addGroup(gl_contentPane.createSequentialGroup() .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) .addGroup(gl_contentPane.createSequentialGroup() .addComponent(button_22, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(button_23, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE)) .addGroup(gl_contentPane.createSequentialGroup() .addComponent(button_24, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(button_25, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE))) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(button_26, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE))) .addPreferredGap(ComponentPlacement.RELATED) .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING) .addGroup(gl_contentPane.createSequentialGroup() .addGap(108) .addComponent(btnNewButton_6, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.UNRELATED) .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) .addComponent(btnNewButton_21, GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE) .addComponent(btnNewButton_22, GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE) .addComponent(btnNewButton_20, GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)) .addGap(102) .addComponent(btnNewButton_18)) .addGroup(gl_contentPane.createSequentialGroup() .addGap(141) .addComponent(btnNewButton_8) .addGap(223)))) .addGroup(gl_contentPane.createSequentialGroup() .addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(btnMr, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(btnMs, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(btnM, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(btnM_1, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE)) .addComponent(textField, GroupLayout.PREFERRED_SIZE, 272, GroupLayout.PREFERRED_SIZE) .addGroup(gl_contentPane.createSequentialGroup() .addComponent(button_4, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(btnCe, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(btnC, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(button_14, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(button_15, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE)) .addGroup(gl_contentPane.createSequentialGroup() .addComponent(button_5, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(button_10, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(button_16, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(button_17, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(button_18, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE))) .addContainerGap()) ); gl_contentPane.setVerticalGroup( gl_contentPane.createParallelGroup(Alignment.TRAILING) .addGroup(gl_contentPane.createSequentialGroup() .addContainerGap() .addComponent(textField, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.UNRELATED) .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) .addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE) .addComponent(btnMr, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE) .addComponent(btnMs, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE) .addComponent(btnM, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE) .addComponent(btnM_1, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(ComponentPlacement.RELATED) .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) .addComponent(button_4, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE) .addComponent(btnCe, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE) .addComponent(btnC, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE) .addComponent(button_14, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE) .addComponent(button_15, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(ComponentPlacement.RELATED) .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) .addGroup(gl_contentPane.createSequentialGroup() .addGap(41) .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING) .addGroup(gl_contentPane.createSequentialGroup() .addComponent(btnNewButton_20) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(btnNewButton_22) .addPreferredGap(ComponentPlacement.RELATED)) .addComponent(btnNewButton_8, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)) .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE) .addComponent(btnNewButton_18) .addComponent(btnNewButton_21)) .addGap(35) .addComponent(btnNewButton_6)) .addGroup(gl_contentPane.createSequentialGroup() .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) .addGroup(gl_contentPane.createSequentialGroup() .addComponent(button_5, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(button_6, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE)) .addGroup(gl_contentPane.createSequentialGroup() .addComponent(button_10, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(button_11, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE)) .addGroup(gl_contentPane.createSequentialGroup() .addComponent(button_16, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(button_19, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE)) .addGroup(gl_contentPane.createSequentialGroup() .addComponent(button_17, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(button_20, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE))) .addPreferredGap(ComponentPlacement.RELATED) .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) .addComponent(button_12, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE) .addComponent(button_7, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE) .addComponent(button_22, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE) .addComponent(button_23, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(ComponentPlacement.RELATED) .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) .addComponent(button_25, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE) .addComponent(button_24, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE) .addComponent(button_8, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE))) .addGroup(gl_contentPane.createSequentialGroup() .addComponent(button_18, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(btnx, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(button_26, GroupLayout.PREFERRED_SIZE, 84, GroupLayout.PREFERRED_SIZE))) .addGap(31)) ); contentPane.setLayout(gl_contentPane); } }
gui小计算器的程序写法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。