java吧 关注:1,233,208贴子:12,698,935
  • 6回复贴,共1

第一个贴,发一个计算器的程序代码,界面没有用grid,用了group

只看楼主收藏回复

所以,界面有点乱。不过,我感觉还好了。待会把程序实体发上来,链接见下面的楼


IP属地:北京1楼2015-03-12 14:02回复
    The following is the code part:
    package pri.Cal.views;
    import java.awt.Color;
    import java.awt.EventQueue;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import javax.script.ScriptEngine;
    import javax.script.ScriptEngineManager;
    import javax.script.ScriptException;
    import javax.swing.ButtonGroup;
    import javax.swing.GroupLayout;
    import javax.swing.GroupLayout.Alignment;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.LayoutStyle.ComponentPlacement;
    import javax.swing.LookAndFeel;
    import javax.swing.SwingConstants;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    import javax.swing.border.EmptyBorder;


    IP属地:北京2楼2015-03-12 14:06
    收起回复
      public class CalViews extends JFrame {
      private JPanel contentPane;
      private JTextField textField_2;
      private JTextField textField;
      private String t1=null;
      private String t3;
      private double n1=0;
      private JMenuBar menuBar;
      private JMenu menu;
      private JButton button_1;
      private JButton button_3;
      private JButton button_2;
      private JButton button_4;
      private JButton button_5;
      private JButton button_6;
      private JButton button_7;
      private JButton button_8;
      private JButton button_9;
      private JButton button_10;
      private JButton button_11;
      private JButton button_12;
      private JButton btnC;
      private JButton button_17;
      private JButton btnSin;
      private JButton button_18;
      private String strbg="";
      private JButton btnPow;
      private JButton button_19;
      private JButton btnLg;
      private JButton btnNewButton;
      private JButton button_20;
      private JButton button_21;
      private JButton button_22;
      /**
      * Launch the application.
      */
      public static void main(String[] args) {
      try {
      UIManager.setLookAndFeel(NimbusLookAndFeel());
      } catch (UnsupportedLookAndFeelException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
      }
      EventQueue.invokeLater(new Runnable() {
      public void run() {
      try {
      CalViews frame = new CalViews();
      frame.setVisible(true);
      } catch (Exception e) {
      e.printStackTrace();
      }
      }
      });
      }
      private static LookAndFeel NimbusLookAndFeel() {
      // TODO Auto-generated method stub
      return null;
      }
      /**
      * Create the frame.
      */
      public CalViews() {
      setForeground(new Color(204, 255, 255));
      setBackground(new Color(204, 255, 255));
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setBounds(100, 100, 478, 333);
      menuBar = new JMenuBar();
      menuBar.setBackground(new Color(204, 255, 255));
      menuBar.setForeground(Color.BLUE);
      setJMenuBar(menuBar);
      menu = new JMenu("帮助");
      menu.setBackground(new Color(204, 255, 255));
      menuBar.add(menu);
      button_1 = new JButton("关于");
      button_1.setBackground(new Color(204, 255, 255));
      button_1.addMouseListener(new MouseAdapter() {
      @Override
      public void mouseClicked(MouseEvent e) {
      JOptionPane.showMessageDialog(CalViews.this, "此软件由浮冰制作,版权所有哦!(*^__^*) 嘻嘻……", "计算器", JOptionPane.INFORMATION_MESSAGE, null);
      }
      });
      menu.add(button_1);
      contentPane = new JPanel();
      contentPane.setBackground(new Color(204, 255, 255));
      contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
      setContentPane(contentPane);
      ButtonGroup group=new ButtonGroup();
      textField = new JTextField();
      textField.setBackground(new Color(255, 153, 255));
      textField.setFont(new Font("Yu Gothic Light", Font.BOLD | Font.ITALIC, 18));
      textField.setEditable(false);
      textField.setColumns(10);
      JButton button = new JButton("计算");
      button.setBackground(new Color(204, 255, 255));
      button.addMouseListener(new MouseAdapter() {
      @Override
      public void mouseClicked(MouseEvent e) {
      ScriptEngine se = new ScriptEngineManager().getEngineByName("JavaScript");
      //t1=textField.getText();
      t1=strbg;
      try {
      Object object=se.eval(t1);
      t1=object.toString();
      } catch (ScriptException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
      }
      n1=Double.parseDouble(t1);
      n1=Math.pow(10,14)*n1;
      n1=Math.round(n1);
      n1=n1/Math.pow(10,14);
      if(n1 % 1.0==0){
      Long l=(long) n1;
      t3=String.valueOf(l);
      textField_2.setText(t3);
      }else{
      t3=String.valueOf(n1);
      textField_2.setText(t3);
      }
      }
      });
      //textField_1.addKeyListener(new KeyAdapter(){
      //
      //@Override
      //public void keyPressed(KeyEvent e) {
      //// TODO Auto-generated method stub
      //textField_1.setText(textField_1.getText().replaceAll("[^-/qwer]", ""));
      //};
      //});
      textField_2 = new JTextField();
      textField_2.setBackground(new Color(255, 153, 255));
      textField_2.setFont(new Font("Yu Gothic Light", Font.BOLD | Font.ITALIC, 18));
      textField_2.setEditable(false);
      textField_2.setHorizontalAlignment(SwingConstants.LEFT);
      textField_2.setColumns(10);
      button_2 = new JButton("1");
      button_2.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
      }
      });
      button_2.setBackground(new Color(204, 255, 255));
      button_2.addMouseListener(new MouseAdapter() {
      @Override
      public void mouseClicked(MouseEvent e) {
      textField.setText(textField.getText()+1);
      strbg=strbg+"1";
      }
      });
      button_3 = new JButton("2");
      button_3.setBackground(new Color(204, 255, 255));
      button_3.addMouseListener(new MouseAdapter() {
      @Override
      public void mouseClicked(MouseEvent e) {
      textField.setText(textField.getText()+2);
      strbg=strbg+"2";
      }
      });
      button_4 = new JButton("3");
      button_4.setBackground(new Color(204, 255, 255));
      button_4.addMouseListener(new MouseAdapter() {
      @Override
      public void mouseClicked(MouseEvent e) {
      textField.setText(textField.getText()+3);
      strbg=strbg+"3";
      }
      });
      button_5 = new JButton("4");
      button_5.setBackground(new Color(204, 255, 255));
      button_5.addMouseListener(new MouseAdapter() {
      @Override
      public void mouseClicked(MouseEvent e) {
      textField.setText(textField.getText()+4);
      strbg=strbg+"4";
      }
      });
      button_6 = new JButton("5");
      button_6.setBackground(new Color(204, 255, 255));
      button_6.addMouseListener(new MouseAdapter() {
      @Override
      public void mouseClicked(MouseEvent e) {
      textField.setText(textField.getText()+5);
      strbg=strbg+"5";
      }
      });
      button_7 = new JButton("6");
      button_7.setBackground(new Color(204, 255, 255));
      button_7.addMouseListener(new MouseAdapter() {
      @Override
      public void mouseClicked(MouseEvent e) {
      textField.setText(textField.getText()+6);
      strbg=strbg+"6";
      }
      });
      button_8 = new JButton("7");
      button_8.setBackground(new Color(204, 255, 255));
      button_8.addMouseListener(new MouseAdapter() {
      @Override
      public void mouseClicked(MouseEvent e) {
      textField.setText(textField.getText()+7);
      strbg=strbg+"7";
      }
      });
      button_9 = new JButton("8");
      button_9.setBackground(new Color(204, 255, 255));
      button_9.addMouseListener(new MouseAdapter() {
      @Override
      public void mouseClicked(MouseEvent e) {
      textField.setText(textField.getText()+8);
      strbg=strbg+"8";
      }
      });
      button_10 = new JButton("9");
      button_10.setBackground(new Color(204, 255, 255));
      button_10.addMouseListener(new MouseAdapter() {
      @Override
      public void mouseClicked(MouseEvent e) {
      textField.setText(textField.getText()+9);
      strbg=strbg+"9";
      }
      });
      button_11 = new JButton("0");
      button_11.setBackground(new Color(204, 255, 255));
      button_11.addMouseListener(new MouseAdapter() {
      @Override
      public void mouseClicked(MouseEvent e) {
      textField.setText(textField.getText()+0);
      strbg=strbg+"0";
      }
      });
      button_12 = new JButton(".");
      button_12.setBackground(new Color(204, 255, 255));
      button_12.addMouseListener(new MouseAdapter() {
      @Override
      public void mouseClicked(MouseEvent e) {
      textField.setText(textField.getText() + ".");
      strbg=strbg+".";
      }
      });


      IP属地:北京3楼2015-03-12 14:06
      收起回复
        JButton button_13 = new JButton("+");
        button_13.setBackground(new Color(204, 255, 255));
        button_13.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
        textField.setText(textField.getText() + "+");
        strbg=strbg+"+";
        }
        });
        JButton button_14 = new JButton("-");
        button_14.setBackground(new Color(204, 255, 255));
        button_14.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
        textField.setText(textField.getText() + "-");
        strbg=strbg+"-";
        }
        });
        JButton button_15 = new JButton("*");
        button_15.setBackground(new Color(204, 255, 255));
        button_15.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
        textField.setText(textField.getText() + "*");
        strbg=strbg+"*";
        }
        });
        JButton button_16 = new JButton("/");
        button_16.setBackground(new Color(204, 255, 255));
        button_16.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
        textField.setText(textField.getText() + "/");
        strbg=strbg+"/";
        }
        });
        btnC = new JButton("C");
        btnC.setBackground(new Color(204, 255, 255));
        btnC.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
        textField.setText("");
        strbg="";
        }
        });
        button_17 = new JButton("(");
        button_17.setBackground(new Color(204, 255, 255));
        button_17.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
        textField.setText(textField.getText() + "(");
        strbg=strbg+"(";
        }
        });
        btnSin = new JButton("sin");
        btnSin.setBackground(new Color(204, 255, 255));
        btnSin.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
        textField.setText(textField.getText() + "sin");
        strbg=strbg+"Math.sin";
        }
        });
        button_18 = new JButton(")");
        button_18.setBackground(new Color(204, 255, 255));
        button_18.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
        textField.setText(textField.getText() + ")");
        strbg=strbg+")";
        }
        });
        JButton btnCos = new JButton("cos");
        btnCos.setBackground(new Color(204, 255, 255));
        btnCos.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
        textField.setText(textField.getText() + "cos");
        strbg=strbg+"Math.cos";
        }
        });
        JButton btnTan = new JButton("tan");
        btnTan.setBackground(new Color(204, 255, 255));
        btnTan.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
        textField.setText(textField.getText() + "tan");
        strbg=strbg+"Math.tan";
        }
        });
        JButton btnLn = new JButton("Ln");
        btnLn.setBackground(new Color(204, 255, 255));
        btnLn.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
        textField.setText(textField.getText() + "Ln");
        strbg=strbg+"Math.log";
        }
        });
        btnPow = new JButton("pow");
        btnPow.setBackground(new Color(204, 255, 255));
        btnPow.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
        textField.setText(textField.getText() + "pow");
        strbg=strbg+"Math.pow";
        }
        });
        button_19 = new JButton(",");
        button_19.setBackground(new Color(204, 255, 255));
        button_19.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
        textField.setText(textField.getText() + ",");
        strbg=strbg+",";
        }
        });
        btnLg = new JButton("Lg");
        btnLg.setBackground(new Color(204, 255, 255));
        btnLg.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
        textField.setText(textField.getText() + "Lg");
        strbg=strbg+"Math.pow(Math.log(10),-1)*Math.log";
        }
        });
        btnNewButton = new JButton("[");
        btnNewButton.setBackground(new Color(204, 255, 255));
        btnNewButton.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
        textField.setText(textField.getText() + "[");
        strbg=strbg+"(";
        }
        });


        IP属地:北京4楼2015-03-12 14:07
        回复
          button_20 = new JButton("]");
          button_20.setBackground(new Color(204, 255, 255));
          button_20.addMouseListener(new MouseAdapter() {
          @Override
          public void mouseClicked(MouseEvent e) {
          textField.setText(textField.getText() + "]");
          strbg=strbg+")";
          }
          });
          button_21 = new JButton("{");
          button_21.setBackground(new Color(204, 255, 255));
          button_21.addMouseListener(new MouseAdapter() {
          @Override
          public void mouseClicked(MouseEvent e) {
          textField.setText(textField.getText() + "{");
          strbg=strbg+"(";
          }
          });
          button_22 = new JButton("}");
          button_22.setBackground(new Color(204, 255, 255));
          button_22.addMouseListener(new MouseAdapter() {
          @Override
          public void mouseClicked(MouseEvent e) {
          textField.setText(textField.getText() + "}");
          strbg=strbg+")";
          }
          });
          JButton button_23 = new JButton("←");
          button_23.setBackground(new Color(204, 255, 255));
          button_23.addMouseListener(new MouseAdapter() {
          @Override
          public void mouseClicked(MouseEvent e) {
          textField.setText(textField.getText().substring(0, textField.getText().length()-1));
          }
          });
          GroupLayout gl_contentPane = new GroupLayout(contentPane);
          gl_contentPane.setHorizontalGroup(
          gl_contentPane.createParallelGroup(Alignment.LEADING)
          .addGroup(gl_contentPane.createSequentialGroup()
          .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
          .addGroup(gl_contentPane.createSequentialGroup()
          .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
          .addComponent(button_8)
          .addComponent(button_12))
          .addPreferredGap(ComponentPlacement.RELATED)
          .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
          .addGroup(gl_contentPane.createSequentialGroup()
          .addComponent(button_9)
          .addPreferredGap(ComponentPlacement.RELATED)
          .addComponent(button_10))
          .addGroup(gl_contentPane.createSequentialGroup()
          .addComponent(button_11)
          .addPreferredGap(ComponentPlacement.RELATED)
          .addComponent(button_19)))
          .addPreferredGap(ComponentPlacement.RELATED)
          .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
          .addGroup(gl_contentPane.createSequentialGroup()
          .addComponent(button_16)
          .addPreferredGap(ComponentPlacement.RELATED)
          .addComponent(btnTan)
          .addPreferredGap(ComponentPlacement.RELATED)
          .addComponent(btnLg)
          .addPreferredGap(ComponentPlacement.UNRELATED)
          .addComponent(button_22))
          .addGroup(gl_contentPane.createSequentialGroup()
          .addComponent(button_15)
          .addPreferredGap(ComponentPlacement.RELATED)
          .addComponent(btnCos)
          .addPreferredGap(ComponentPlacement.RELATED)
          .addComponent(btnLn)
          .addPreferredGap(ComponentPlacement.UNRELATED)
          .addComponent(button_21))))
          .addComponent(textField, GroupLayout.DEFAULT_SIZE, 442, Short.MAX_VALUE)
          .addGroup(gl_contentPane.createSequentialGroup()
          .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
          .addGroup(gl_contentPane.createSequentialGroup()
          .addComponent(button_2)
          .addPreferredGap(ComponentPlacement.RELATED)
          .addComponent(button_3)
          .addPreferredGap(ComponentPlacement.RELATED)
          .addComponent(button_4)
          .addPreferredGap(ComponentPlacement.RELATED)
          .addComponent(button_13)
          .addPreferredGap(ComponentPlacement.RELATED)
          .addComponent(button_17, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE))
          .addGroup(gl_contentPane.createSequentialGroup()
          .addComponent(button_5)
          .addPreferredGap(ComponentPlacement.RELATED)
          .addComponent(button_6)
          .addPreferredGap(ComponentPlacement.RELATED)
          .addComponent(button_7)
          .addPreferredGap(ComponentPlacement.RELATED)
          .addComponent(button_14)
          .addPreferredGap(ComponentPlacement.RELATED)
          .addComponent(btnSin)))
          .addPreferredGap(ComponentPlacement.RELATED)
          .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
          .addGroup(gl_contentPane.createSequentialGroup()
          .addComponent(button_18)
          .addGap(18)
          .addComponent(btnNewButton))
          .addGroup(gl_contentPane.createSequentialGroup()
          .addComponent(btnPow)
          .addPreferredGap(ComponentPlacement.RELATED)
          .addComponent(button_20))))
          .addGroup(gl_contentPane.createSequentialGroup()
          .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
          .addComponent(button, GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)
          .addComponent(btnC, GroupLayout.DEFAULT_SIZE, 55, Short.MAX_VALUE))
          .addPreferredGap(ComponentPlacement.UNRELATED)
          .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
          .addComponent(button_23)
          .addComponent(textField_2, GroupLayout.DEFAULT_SIZE, 377, Short.MAX_VALUE))))
          .addContainerGap())
          );
          gl_contentPane.setVerticalGroup(
          gl_contentPane.createParallelGroup(Alignment.LEADING)
          .addGroup(gl_contentPane.createSequentialGroup()
          .addGap(6)
          .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
          .addPreferredGap(ComponentPlacement.RELATED)
          .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
          .addGroup(gl_contentPane.createSequentialGroup()
          .addGap(92)
          .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
          .addComponent(button_11)
          .addComponent(button_12)
          .addComponent(button_16)
          .addComponent(btnTan)
          .addComponent(button_19)
          .addComponent(btnLg)
          .addComponent(button_22)))
          .addGroup(gl_contentPane.createSequentialGroup()
          .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
          .addComponent(button_2)
          .addComponent(button_3)
          .addComponent(button_4)
          .addComponent(button_13)
          .addComponent(button_17)
          .addComponent(button_18)
          .addComponent(btnNewButton))
          .addPreferredGap(ComponentPlacement.RELATED)
          .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
          .addComponent(button_5)
          .addComponent(button_6)
          .addComponent(button_7)
          .addComponent(button_14)
          .addComponent(btnSin)
          .addComponent(btnPow)
          .addComponent(button_20))
          .addGap(5)
          .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
          .addComponent(button_8)
          .addComponent(button_9)
          .addComponent(button_10)
          .addComponent(button_15)
          .addComponent(btnCos)
          .addComponent(btnLn)
          .addComponent(button_21))))
          .addGap(24)
          .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING, false)
          .addComponent(button, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
          .addComponent(textField_2))
          .addPreferredGap(ComponentPlacement.RELATED)
          .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
          .addComponent(btnC)
          .addComponent(button_23))
          .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
          );
          contentPane.setLayout(gl_contentPane);
          }
          }


          IP属地:北京5楼2015-03-12 14:09
          回复