java GridBagConstraints解析

说明:GridBagLayout只有一个无参的构造器,要使用它就必须用setConstraints(Component comp,GridBagConstraints constraints)将它和GridBagConstraints关联起来!当GridBagLayout与无参的GridBagConstraints关联时,此时它就相当于一个GridLayout,只不过,用GridLayout布局的组件会随着窗口的变 大(小)而变 大(小)。但GridBagLayout 不会,因此,这就是我们使用GridBagConstants来设置各个约束条件的目的!


      查看API文档,我们就知道GridBagConstraints有十一个属性!自我感觉API文档里面有些东西讲得不是很好理解,就象gridx 与 gridy 这两个属性一样,有些书上说gridx表示行,gridy表示列!API文档里面更是讲了一大堆。
     现在,提供本人对这些属性的理解:

   1,   gridx: 表示组件的左边缘与网格左部之间的距离,如果学过HTML的话,那么gridx就 相当于HTML 里面的leftmargin属性。

   2,   gridy:   表示组件的上边缘与网格顶部之间的距离,相当于HTML里面的topmargin属性!
  
   3,    ipadx:   表示组件在默认的大小上,往水平方向上再加上多少像素的大小!
  
   4,   ipady:   表示组件在默认的大小上,往垂直方向上再加上多少像素的大小!

   5,     fill:   当组件没有空间大时。它可以确定是否填充空间!

   6,   anchor:   当组件没有空间大时。它可以确定在显示区域中放置组件的位置!

   7, insets:   设置组件之间彼此的间距,它有四个参数,分别是上,左,下,右,

默认值为(0,0,0,0)  

            
   8,weightx:   用来设置当窗口变大时,各组件沿水平方向跟着变大的比例,数字越大,表示组件能得到的空间越大

   9,weighty:   用来设置当窗口变大时,各组件沿垂直方向跟着变大的比例,数字越大,表示组件能得                  到的空间越大

10,gridwidth   用来设置组件所占的单位长度,默认值为1。

11,gridheight 用来设置组件所占的单位高度,默认值为1。  


   对gridwidth与gridheight做了修改后,必须记得要对fill属性做相应的修改,否则组件所显示的大小     还是原来的大小
  
===========================================================================================
      建议:运用GridBagLayout这种布局方式时,比较适合于把所有的组件围成一个四边形。所以,我们在设计这种布局方式前,可以先在纸上画一下每个组件的大概位置,然后保证每一行的长度(即每一行的gridwidth之和)都相等,且每一列的长度(即每一列的gridheight之和)也相等,这样,就能按我们要的蓝图而得到实现! 另外,有些属性是要跟另外一些属性搭配着用的,只有把相联系的属性都做了相应的修改,我们才能看到变化!比如说上面所说到的gridwidth与gridheight必须与fill属性联合起来用才能看到结果!

下面来个实际的例子:


import java.awt.*;
import javax.swing.*;

public class Test { 
   public static void main(String[] args) { 
       JFrame f = new JFrame("Test");
      
       JButton b1 = new JButton("Button1");
       JButton b2 = new JButton("Button2");
       JButton b3 = new JButton("Button3");
       JButton b4 = new JButton("Button4");
       JButton b5 = new JButton("Button5");
       JButton b6 = new JButton("Button6");
       JButton b7 = new JButton("Button7");
      
       GridBagConstraints c = new GridBagConstraints();
       GridBagLayout g = new GridBagLayout();
      
       f.setLayout(g);
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
      
       f.add(b1);   //由于属性都有默认值,前面的按默认值来加入!
       f.add(b2);
       f.add(b3);

       c.gridy = 1;
       c.gridwidth = 3 ;   //修改了gridwidth值
       c.fill = GridBagConstraints.BOTH; //所以这里要做相应的修改才能按gridwidth的值来显示
       g.setConstraints(b4,c);
       f.add(b4);
      
       c = new GridBagConstraints();
       c.gridy = 2;
       g.setConstraints(b5,c);
       f.add(b5);
      
       c = new GridBagConstraints();
       c.gridy = 3;
       g.setConstraints(b6,c);
       f.add(b6);
      
      
       c = new GridBagConstraints();
       c.gridx = 1;
       c.gridy = 2;
       c.gridwidth = 2;
       c.gridheight = 2;
       c.fill = GridBagConstraints.BOTH;   //同上面的注释
       g.setConstraints(b7,c);
       f.add(b7);
      
       f.pack();
       f.setVisible(true);
   }

}


  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import java.util.Scanner; import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.imageio.*; public class Jsq{ public static void main(String [] as){ new JsqFrame(); } } class JsqFrame extends JFrame implements KeyListener{ private String s000 = "image/000.png"; private String s001 = "image/001.png"; private String s002 = "image/002.png"; private String s003 = "image/003.png"; private String s004 = "image/004.png"; private String s005 = "image/005.png"; private String s006 = "image/006.png"; private String s007 = "image/007.png"; private String s008 = "image/008.png"; private String s009 = "image/009.png"; private String sdian = "image/dian.png"; private String sjia = "image/jia.png"; private String sjian = "image/jian.png"; private String scheng = "image/cheng.png"; private String schu = "image/chu.png"; private String spf = "image/pf.png"; private String skf = "image/kf.png"; private String szkh = "image/zkh.png"; private String sykh = "image/ykh.png"; private String sht = "image/ht.png"; private String sqc = "image/qc.png"; private String sdy = "image/dy.png"; private String sbz = "image/help.png"; private boolean dian = true; private boolean zkh = true; private boolean qc = false; private boolean dengyu = true; private boolean i_=false; private int p = 0; private StackTally pt = new StackTally(); private StackNum pn = new StackNum(); private JButton blaf0 = new JButton(new ImageIcon(getClass().getResource(s000))); private JButton blaf1 = new JButton(new ImageIcon(getClass().getResource(s001))); private JButton blaf2 = new JButton(new ImageIcon(getClass().getResource(s002))); private JButton blaf3 = new JButton(new ImageIcon(getClass().getResource(s003))); private JButton blaf4 = new JButton(new ImageIcon(getClass().getResource(s004))); private JButton blaf5 = new JButton(new ImageIcon(getClass().getResource(s005))); private JButton blaf6 = new JButton(new ImageIcon(getClass().getResource(s006))); private JButton blaf7 = new JButton(new ImageIcon(getClass().getResource(s007))); private JButton blaf8 = new JButton(new ImageIcon(getClass().getResource(s008))); private JButton blaf9 = new JButton(new ImageIcon(getClass().getResource(s009))); private JButton blafzkh = new JButton(new ImageIcon(getClass().getResource(szkh))); private JButton blafykh = new JButton(new ImageIcon(getClass().getResource(sykh))); private JButton blafdian = new JButton(new ImageIcon(getClass().getResource(sdian))); private JButton blafjia = new JButton(new ImageIcon(getClass().getResource(sjia))); private JButton blafjian = new JButton(new ImageIcon(getClass().getResource(sjian))); private JButton blafcheng = new JButton(new ImageIcon(getClass().getResource(scheng))); private JButton blafchu = new JButton(new ImageIcon(getClass().getResource(schu))); private JButton blafpf = new JButton(new ImageIcon(getClass().getResource(spf))); private JButton blafkf = new JButton(new ImageIcon(getClass().getResource(skf))); private JButton blafht = new JButton(new ImageIcon(getClass().getResource(sht))); private JButton blafqc = new JButton(new ImageIcon(getClass().getResource(sqc))); private JButton blafdy = new JButton(new ImageIcon(getClass().getResource(sdy))); private JButton blafbz = new JButton(new ImageIcon(getClass().getResource(sbz))); private JTextField txt = new JTextField("0"); private JTextArea txta = new JTextArea(4,10); public JsqFrame(){ JPanel p1 = new JPanel(); JPanel p2 = new JPanel(); JPanel p3 = new JPanel(); JScrollPane scr = new JScrollPane(txta); p1.setBorder(BorderFactory.createLoweredBevelBorder()); p1.setLayout(new GridBagLayout()); blaf0.setBackground(new Color(225,225,225)); blaf0.setPreferredSize(new Dimension(45,26)); blaf1.setPreferredSize(new Dimension(40,26)); blaf2.setPreferredSize(new Dimension(40,26)); blaf3.setPreferredSize(new Dimension(40,26)); blaf4.setPreferredSize(new Dimension(40,26)); blaf5.setPreferredSize(new Dimension(40,26)); blaf6.setPreferredSize(new Dimension(40,26)); blaf7.setPreferredSize(new Dimension(40,26)); blaf8.setPreferredSize(new Dimension(40,26)); blaf9.setPreferredSize(new Dimension(40,26)); blafdian.setPreferredSize(new Dimension(40,26)); blafzkh.setPreferredSize(new Dimension(40,26)); blafykh.setPreferredSize(new Dimension(40,26)); blafjia.setPreferredSize(new Dimension(40,26)); blafjian.setPreferredSize(new Dimension(40,26)); blafcheng.setPreferredSize(new Dimension(40,26)); blafchu.setPreferredSize(new Dimension(40,26)); blafkf.setPreferredSize(new Dimension(40,26)); blafpf.setPreferredSize(new Dimension(40,26)); blafqc.setPreferredSize(new Dimension(40,26)); blafht.setPreferredSize(new Dimension(40,26)); blafdy.setBackground(new Color(225,225,225)); blafdy.setPreferredSize(new Dimension(40,50)); blafbz.setBackground(new Color(225,225,225)); blafbz.setPreferredSize(new Dimension(40,26)); // blaf1.registerKeyboardAction() LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,0,0,1,1,blafbz); LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,1,0,1,1,blafzkh); LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,2,0,1,1,blafykh); LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,3,0,1,1,blafht); LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,4,0,1,1,blafqc); LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,0,1,1,1,blaf7); LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,1,1,1,1,blaf8); LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,2,1,1,1,blaf9); LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,3,1,1,1,blafpf); LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,4,1,1,1,blafkf); LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,0,2,1,1,blaf4); LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,1,2,1,1,blaf5); LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,2,2,1,1,blaf6); LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,3,2,1,1,blafcheng); LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,4,2,1,1,blafchu); LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,0,3,1,1,blaf1); LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,1,3,1,1,blaf2); LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,2,3,1,1,blaf3); LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,3,3,1,1,blafjian); LayoutUtil.add(p1,GridBagConstraints.BOTH,GridBagConstraints.CENTER,0,0,4,3,1,2,blafdy); LayoutUtil.add(p1,GridBagConstraints.HORIZONTAL,GridBagConstraints.CENTER,0,0,0,4,2,1,blaf0); LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,2,4,1,1,blafdian); LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,3,4,1,1,blafjia); getContentPane().add(p1,BorderLayout.SOUTH); p2.setBorder(BorderFactory.createLoweredBevelBorder()); p2.setLayout(new GridBagLayout()); txt.setHorizontalAlignment(JTextField.RIGHT); txt.setEditable(false); txt.setFont(new Font("font",Font.ITALIC,14)); txta.setFont(new Font("font",Font.ITALIC,14)); txta.setEditable(false); txta.setLineWrap(true); LayoutUtil.add(p2,GridBagConstraints.HORIZONTAL,GridBagConstraints.SOUTH,1,1,0,3,1,1,txt); LayoutUtil.add(p2,GridBagConstraints.HORIZONTAL,GridBagConstraints.SOUTHWEST,1,1,0,0,1,2,scr); getContentPane().add(p2,BorderLayout.CENTER); scr.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); MouseListener al = new LookAndMouseListener(); // KeyListener kl = new LookAndKeyListener(); blaf0.addMouseListener(al); blaf1.addMouseListener(al); blaf2.addMouseListener(al); blaf3.addMouseListener(al); blaf4.addMouseListener(al); blaf5.addMouseListener(al); blaf6.addMouseListener(al); blaf7.addMouseListener(al); blaf8.addMouseListener(al); blaf9.addMouseListener(al); blafzkh.addMouseListener(al); blafykh.addMouseListener(al); blafdian.addMouseListener(al); blafjia.addMouseListener(al); blafjian.addMouseListener(al); blafcheng.addMouseListener(al); blafchu.addMouseListener(al); blafpf.addMouseListener(al); blafkf.addMouseListener(al); blafht.addMouseListener(al); blafqc.addMouseListener(al); blafdy.addMouseListener(al); blafbz.addMouseListener(al); // blaf0.addKeyListener(kl); p1.setBackground(new Color(225,225,225)); try{ Image image = ImageIO.read(getClass().getResource("image/tubiao.png")); this.setIconImage(image); }catch(Exception e){ } this.setBounds(400,200,230,290); this.setTitle("计算器"); this.setResizable(false); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.show(); } class LookAndMouseListener implements MouseListener{ public void mouseEntered(MouseEvent e){ Object sjy = e.getSource(); if(sjy==blaf0){ s000="image/100.png"; blaf0.setIcon(new ImageIcon(getClass().getResource(s000))); } if(sjy==blaf1){ s001="image/101.png"; blaf1.setIcon(new ImageIcon(getClass().getResource(s001))); } if(sjy==blaf2){ s002="image/102.png"; blaf2.setIcon(new ImageIcon(getClass().getResource(s002))); } if(sjy==blaf3){ s003="image/103.png"; blaf3.setIcon(new ImageIcon(getClass().getResource(s003))); } if(sjy==blaf4){ s004="image/104.png"; blaf4.setIcon(new ImageIcon(getClass().getResource(s004))); } if(sjy==blaf5){ s005="image/105.png"; blaf5.setIcon(new ImageIcon(getClass().getResource(s005))); } if(sjy==blaf6){ s006="image/106.png"; blaf6.setIcon(new ImageIcon(getClass().getResource(s006))); } if(sjy==blaf7){ s007="image/107.png"; blaf7.setIcon(new ImageIcon(getClass().getResource(s007))); } if(sjy==blaf8){ s008="image/108.png"; blaf8.setIcon(new ImageIcon(getClass().getResource(s008))); } if(sjy==blaf9){ s009="image/109.png"; blaf9.setIcon(new ImageIcon(getClass().getResource(s009))); } if(sjy==blafdian){ sdian="image/1dian.png"; blafdian.setIcon(new ImageIcon(getClass().getResource(sdian))); } if(sjy==blafzkh){ szkh="image/1zkh.png"; blafzkh.setIcon(new ImageIcon(getClass().getResource(szkh))); } if(sjy==blafykh){ sykh="image/1ykh.png"; blafykh.setIcon(new ImageIcon(getClass().getResource(sykh))); } if(sjy==blafjia){ sjia="image/1jia.png"; blafjia.setIcon(new ImageIcon(getClass().getResource(sjia))); } if(sjy==blafjian){ sjian="image/1jian.png"; blafjian.setIcon(new ImageIcon(getClass().getResource(sjian))); } if(sjy==blafcheng){ scheng="image/1cheng.png"; blafcheng.setIcon(new ImageIcon(getClass().getResource(scheng))); } if(sjy==blafchu){ schu="image/1chu.png"; blafchu.setIcon(new ImageIcon(getClass().getResource(schu))); } if(sjy==blafpf){ spf="image/1pf.png"; blafpf.setIcon(new ImageIcon(getClass().getResource(spf))); } if(sjy==blafkf){ skf="image/1kf.png"; blafkf.setIcon(new ImageIcon(getClass().getResource(skf))); } if(sjy==blafht){ sht="image/1ht.png"; blafht.setIcon(new ImageIcon(getClass().getResource(sht))); } if(sjy==blafqc){ sqc="image/1qc.png"; blafqc.setIcon(new ImageIcon(getClass().getResource(sqc))); } if(sjy==blafdy){ sdy="image/1dy.png"; blafdy.setIcon(new ImageIcon(getClass().getResource(sdy))); } if(sjy==blafbz){ sbz="image/1help.png"; blafbz.setIcon(new ImageIcon(getClass().getResource(sbz))); } } public void mouseExited(MouseEvent e){ Object sjy = e.getSource(); if(sjy == blaf0){ s000="image/000.png"; blaf0.setIcon(new ImageIcon(getClass().getResource(s000))); } if(sjy == blaf1){ s001="image/001.png"; blaf1.setIcon(new ImageIcon(getClass().getResource(s001))); } if(sjy == blaf2){ s002="image/002.png"; blaf2.setIcon(new ImageIcon(getClass().getResource(s002))); } if(sjy == blaf3){ s003="image/003.png"; blaf3.setIcon(new ImageIcon(getClass().getResource(s003))); } if(sjy == blaf4){ s004="image/004.png"; blaf4.setIcon(new ImageIcon(getClass().getResource(s004))); } if(sjy == blaf5){ s005="image/005.png"; blaf5.setIcon(new ImageIcon(getClass().getResource(s005))); } if(sjy == blaf6){ s006="image/006.png"; blaf6.setIcon(new ImageIcon(getClass().getResource(s006))); } if(sjy == blaf7){ s007="image/007.png"; blaf7.setIcon(new ImageIcon(getClass().getResource(s007))); } if(sjy == blaf8){ s008="image/008.png"; blaf8.setIcon(new ImageIcon(getClass().getResource(s008))); } if(sjy == blaf9){ s009="image/009.png"; blaf9.setIcon(new ImageIcon(getClass().getResource(s009))); } if(sjy == blafzkh){ szkh="image/zkh.png"; blafzkh.setIcon(new ImageIcon(getClass().getResource(szkh))); } if(sjy == blafykh){ sykh="image/ykh.png"; blafykh.setIcon(new ImageIcon(getClass().getResource(sykh))); } if(sjy == blafjia){ sjia="image/jia.png"; blafjia.setIcon(new ImageIcon(getClass().getResource(sjia))); } if(sjy == blafdian){ sdian="image/dian.png"; blafdian.setIcon(new ImageIcon(getClass().getResource(sdian))); } if(sjy == blafjian){ sjian="image/jian.png"; blafjian.setIcon(new ImageIcon(getClass().getResource(sjian))); } if(sjy == blafcheng){ scheng="image/cheng.png"; blafcheng.setIcon(new ImageIcon(getClass().getResource(scheng))); } if(sjy == blafchu){ schu="image/chu.png"; blafchu.setIcon(new ImageIcon(getClass().getResource(schu))); } if(sjy == blafpf){ spf="image/pf.png"; blafpf.setIcon(new ImageIcon(getClass().getResource(spf))); } if(sjy == blafkf){ skf="image/kf.png"; blafkf.setIcon(new ImageIcon(getClass().getResource(skf))); } if(sjy == blafqc){ sqc="image/qc.png"; blafqc.setIcon(new ImageIcon(getClass().getResource(sqc))); } if(sjy == blafht){ sht="image/ht.png"; blafht.setIcon(new ImageIcon(getClass().getResource(sht))); } if(sjy == blafdy){ sdy="image/dy.png"; blafdy.setIcon(new ImageIcon(getClass().getResource(sdy))); } if(sjy==blafbz){ sbz="image/help.png"; blafbz.setIcon(new ImageIcon(getClass().getResource(sbz))); } } public void mouseReleased(MouseEvent e){ Object sjy = e.getSource(); if(sjy == blafbz){ JOptionPane.showMessageDialog(null,"本计算器暂不支持负号输入\n请合理使用(0-1)来代替\nS字符代表根号 格式为:\n开方次数S被开方数\n可精确到小数点后8位\n使用计算器时如遇bug请敬请见谅\n并希望能与本人联系","帮助",JOptionPane.PLAIN_MESSAGE); } if(sjy == blaf0) shuru("0"); if(sjy == blaf1) shuru("1"); if(sjy == blaf2) shuru("2"); if(sjy == blaf3) shuru("3"); if(sjy == blaf4) shuru("4"); if(sjy == blaf5) shuru("5"); if(sjy == blaf6) shuru("6"); if(sjy == blaf7) shuru("7"); if(sjy == blaf8) shuru("8"); if(sjy == blaf9) shuru("9"); if(sjy == blafzkh) shuru("("); if(sjy == blafykh) shuru(")"); if(sjy == blafjia) shuru("+"); if(sjy == blafdian) shuru("."); if(sjy == blafjian) shuru("-"); if(sjy == blafcheng) shuru("*"); if(sjy == blafchu) shuru("/"); if(sjy == blafpf) shuru("^"); if(sjy == blafkf) shuru("S"); if(sjy == blafqc){ dian = true; dengyu = true; zkh = true; txt.setText("0"); if(qc){ txta.append("---------------------------------------\n"); qc = false; } } if(sjy == blafht) txt.setText(houtui()); if(sjy == blafdy&&khpp;()&&wz;()){ qc = true; txt.setText(shuchu()); } else if(sjy == blafdy) JOptionPane.showMessageDialog(null,"括号未能匹配\n或输入格式错误","警告",JOptionPane.WARNING_MESSAGE); } public void mousePressed(MouseEvent e){ } public void mouseClicked(MouseEvent e){ } } public void keyPressed(KeyEvent e){ int key = e.getKeyCode(); } public void keyReleased(KeyEvent e){ } public void keyTyped(KeyEvent e){ } class StackTally{ private int n = 100; private char tally[] = new char[n]; private int top; public StackTally(){ tally[0]='#'; top=0; } public void TallyPush(char c){ tally[++top]=c; } public char TallyPop(){ return tally[top--]; } public char TallyPopK(){ return tally[top]; } } class StackNum{ private int n = 100; private double num[] = new double[n]; private int top; public StackNum(){ top=-1; } public void NumPush(double f){ top++; num[top]=f; } public double NumPop(){ return num[top--]; } } void shuru(String c){ String s; if(dengyu||c.equals("+")||c.equals("-")||c.equals("*")||c.equals("/")||c.equals("^")||c.equals("S")){ s = txt.getText().trim(); dengyu = true; } else{ zkh = true; dengyu = true; s = "0"; } if(s.endsWith("+")||s.endsWith("-")||s.endsWith("*")||s.endsWith("/")||s.endsWith("^")||s.endsWith("S")||s.endsWith("(")){ if(!(c.equals("+")||c.equals("-")||c.equals("*")||c.equals("/")||c.equals("^")||c.equals("S")||c.equals(".")||(s.endsWith("(")&&c.equals(")")))) s=s.concat(c); dian = true; txt.setText(chu0(s)); } else{ if(!c.equals("(")||zkh){ if(!(s.endsWith(")")&&!(c.equals("+")||c.equals("-")||c.equals("*")||c.equals("/")||c.equals("^")||c.equals("S")||c.equals(".")||s.endsWith("(")||c.equals(")")))&&(dian||!c.equals("."))&&!(s.endsWith("+")||s.endsWith("-")||s.endsWith("*")||s.endsWith("/")||s.endsWith("^")||s.endsWith("S")||s.endsWith("("))) s=s.concat(c); zkh = false; } if(c.equals(".")) dian = false; txt.setText(chu0(s)); } } String shuchu(){ String s = txt.getText().concat("#"); txta.append(s.replace("#","=")); pn.NumPush(NumFen(s.toCharArray())); String s1 =""+YunSuan(s.toCharArray()); dengyu = false; p = 0; i_ = false; dian = true; if(s1.endsWith(".0")) s1=s1.replace(".0",""); txta.append(s1+'\n'); return s1; } double NumFen(char c[]){ double n = 0; double m = 1; while(c[p]>='0'&&c[p]<='9'){ n=n*10+(c[p]-'0'); p++; } if(c[p]=='.'){ p++; while(c[p]>='0'&&c[p]<='9'){ m*=0.1; n=n+(m*(c[p]-'0')); p++; } } return n; } double YunSuan(char c[]){ while(!(c[p]=='#'&&pt;.TallyPopK()=='#')){ if(BiJiao(pt.TallyPopK(),c[p])==1){ pt.TallyPush(c[p]); p++; if(c[p]>='0'&&c[p]<='9') pn.NumPush(NumFen(c)); } else pn.NumPush(JiSua()); } return pn.NumPop(); } int BiJiao(char m,char n){ int z1,z2; i_ = true; z1=Zhuan(m); i_ = false; z2=Zhuan(n); if(z1<z2) return 1; else return 0; } int Zhuan(char ch){ if(ch=='('&&!i_) return 10; if(ch=='('&&i_) return 0; switch(ch){ case ')': return 0; case '#': return -1; case '+': case '-': return 1; case '*': case '/': return 2; case '^': case 'S': return 3; case 'E': return 9; default: return -10;//无可能这种情况 } } double JiSua() { char ch; double a,b,z = 1; int i; ch = pt.TallyPop(); if(ch!='(') { b = pn.NumPop(); a = pn.NumPop(); switch(ch) { case '+': z=a+b; break; case '-': z=a-b; break; case '*': z=a*b; break; case '/': if(b==0)JOptionPane.showMessageDialog(null,"出现除数为0\n计算出错\n请按清除C键重新输入","错误",JOptionPane.WARNING_MESSAGE);else z=a/b; break; case '^': if(b==0)z=1;else for(i=0;i<b;i++)z*=a;break; case 'S': if(b<0||a<2)JOptionPane.showMessageDialog(null,"出现被开发数小于0\n或开发次数小于2\n计算出错\n请按清除C键重新输入","错误",JOptionPane.WARNING_MESSAGE);else z=(float)java.lang.StrictMath.pow(b,1.0/(int)a);break; case 'E': for(i=0;i<b;i++)z*=10;z*=a;break; } } else { p++; z = pn.NumPop(); } return z; } String houtui(){ String s = txt.getText().trim(); if(s.endsWith(".")) dian = true; int j = s.length(); char c[] = new char[j]; c = s.toCharArray(); s = ""; for(int i=0;i<j-1;i++) s+=c[i]; if(s.equals("")) s = "0"; if(s.equals("0")){ dian = true; dengyu = true; zkh = true; } return s; } boolean khpp(){ String s = txt.getText().trim(); int j = s.length(); int n =0; char c[] = new char[j]; c = s.toCharArray(); for(int i=0;i<j;i++){ if(c[i]=='(') n++; if(c[i]==')') n--; if(n<0) break; } if(n==0) return true; else return false; } boolean wz(){ String s = txt.getText().trim(); if(!(s.endsWith("+")||s.endsWith("-")||s.endsWith("*")||s.endsWith("/")||s.endsWith("^")||s.endsWith("S"))) return true; else return false; } String chu0(String s){ int i; int n; n = s.length(); char c[] = new char[n]; c = s.toCharArray(); s=""; if(1<n&&c[0]=='0'&&(('0'<=c[1]&&c[1]<='9')||c[1]=='(')) i=1; else i=0; for(;i<n;i++) s+=c[i]; return s; } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值