2

我是 Java 新手,我发现这里很难实现与 Visual Basic 不同的 GUI。

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


public class CalculatorGui extends JFrame{
    private static final int WIDTH = 250;
    private static final int HEIGHT = 250;

    private JTextField enterTextField;

    private JButton sevenB, eightB, nineB, divideB, fourB, fiveB, sixB, multiplyB, oneB, twoB, threeB, subtractB, zeroB, dotB, equalsB, addB;

    public CalculatorGui() {
        setTitle("Calculator");
        setSize(WIDTH, HEIGHT);

        setLayout(new GridLayout(4, 4));

        enterTextField = new JTextField(30);

        sevenB = new JButton("7");
        eightB = new JButton("8");
        nineB = new JButton("9");
        divideB = new JButton("/");
        fourB = new JButton("4");
        fiveB = new JButton("5");
        sixB = new JButton("6");
        multiplyB = new JButton("*");
        oneB = new JButton("1");
        twoB = new JButton("2");
        threeB = new JButton("3");
        subtractB = new JButton("-");
        zeroB = new JButton("0");
        dotB = new JButton(".");
        equalsB = new JButton("=");
        addB = new JButton("+");

        /*JPanel dataPanel = new JPanel();
        JPanel buttonPanel = new JPanel();

        FlowLayout flowCenter = new FlowLayout(FlowLayout.RIGHT);
        GridLayout aGrid = new GridLayout(4, 4);

        buttonPanel.setLayout(flowCenter);
        dataPanel.setLayout(aGrid);


        dataPanel.add(enterTextField);
        buttonPanel.add(sevenB);
        buttonPanel.add(eightB);
        buttonPanel.add(nineB);
        buttonPanel.add(divideB);
        buttonPanel.add(fourB);
        buttonPanel.add(fiveB);
        buttonPanel.add(sixB);
        buttonPanel.add(multiplyB);
        buttonPanel.add(oneB);
        buttonPanel.add(twoB);
        buttonPanel.add(threeB);
        buttonPanel.add(subtractB);
        buttonPanel.add(zeroB);
        buttonPanel.add(dotB);
        buttonPanel.add(equalsB);
        buttonPanel.add(addB);

        add(dataPanel, BorderLayout.NORTH);
        add(buttonPanel, BorderLayout.CENTER);*/

        //add(enterTextField);
        add(sevenB);
        add(eightB);
        add(nineB);
        add(divideB);
        add(fourB);
        add(fiveB);
        add(sixB);
        add(multiplyB);
        add(oneB);
        add(twoB);
        add(threeB);
        add(subtractB);
        add(zeroB);
        add(dotB);
        add(equalsB);
        add(addB);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

    }

    public static void main(String[] args) {

        CalculatorGui calcu = new CalculatorGui();

    }

}

它运行,但我需要在这些按钮(数字)之前放置一个文本字段。当窗口最大化时,文本字段应该流动并且不会被放大(与 4x4 按钮不同)。我试过JPanel(见评论),但它输出的设计很糟糕。磷

4

4 回答 4

2

在这里,在构建 GUI(规则)时要遵循一些规则:
不要为应用程序需要子类化 AWT/Swing/X 组件

import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class CalculatorGui {

    private static final int WIDTH = 250;
    private static final int HEIGHT = 250;
    private JTextField enterTextField;
    private JButton sevenB, eightB, nineB, divideB, fourB, fiveB, sixB, multiplyB, oneB, twoB, threeB, subtractB, zeroB, dotB, equalsB, addB;

    public CalculatorGui() {
        JFrame frame = new JFrame("Calculator");
        frame.setTitle("Calculator");
        frame.setSize(WIDTH, HEIGHT);

        JPanel buttonPanel = new JPanel(new GridLayout(4, 4));        
        enterTextField = new JTextField();       

        frame.add(enterTextField, BorderLayout.NORTH);

        sevenB = new JButton("7");
        eightB = new JButton("8");
        nineB = new JButton("9");
        divideB = new JButton("/");
        fourB = new JButton("4");
        fiveB = new JButton("5");
        sixB = new JButton("6");
        multiplyB = new JButton("*");
        oneB = new JButton("1");
        twoB = new JButton("2");
        threeB = new JButton("3");
        subtractB = new JButton("-");
        zeroB = new JButton("0");
        dotB = new JButton(".");
        equalsB = new JButton("=");
        addB = new JButton("+");

        buttonPanel.add(sevenB);
        buttonPanel.add(eightB);
        buttonPanel.add(nineB);
        buttonPanel.add(divideB);
        buttonPanel.add(fourB);
        buttonPanel.add(fiveB);
        buttonPanel.add(sixB);
        buttonPanel.add(multiplyB);
        buttonPanel.add(oneB);
        buttonPanel.add(twoB);
        buttonPanel.add(threeB);
        buttonPanel.add(subtractB);
        buttonPanel.add(zeroB);
        buttonPanel.add(dotB);
        buttonPanel.add(equalsB);
        buttonPanel.add(addB);

        frame.add(buttonPanel, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        CalculatorGui calcu = new CalculatorGui();
    }
}

我建议您在经过良好的手动练习后使用WindowsBuilder Prolayoutscomponents

于 2012-08-18T06:43:18.497 回答
0

我是新手,但我有一个使用 AWT 设计计算器的代码。它只是一个示例。

    导入 java.awt.*;
    公共类计算器扩展框架
    {
    公共计算器(字符串 s)
    {
        String buttonString = "789/456*123-0.=+";
        for (int i = 0, x=0,y=50; i

              Button b = new Button(buttonString.substring(i, i + 1));
              b.setBounds(x, y, 50, 50);
              add(b);
              if((i+1)%4==0)
              {
                  x=0;
                  y+=50;
              }
              else
              {
                  x=x+50;
              }
    }
    TextField txtField=new TextField("0");
    txtField.setBounds(0,0, 200, 30);
    add(txtField);
    setSize(200,255);
    setVisible(true);
}
public static void main(String[] args) 
{
    new Calculator("Calculator");
}}
于 2012-11-29T18:04:43.337 回答
0

如果您来自 Visual Basic,您将养成拖放的习惯,那么当您也可以在 Java 中这样做时,为什么还要离开它。

使用现在从 Google 免费提供的 WindowsBuilder Pro,并使用 Group Layout 来设计您的应用程序。组布局是由 NetBeans 团队早在 2005 年开发的。

于 2012-08-18T06:30:10.013 回答
0

自定义计算器

GUI是框架定制...

public class Calculator implements ActionListener
{

final static Point point = new Point();
JFrame frame;
JButton one,two,three,four,five,six,seven,eight,nine,zone,cancel,clear,cancel1,sub,add,equal,dot,mult,div,adsub,baket,up;
JPanel top,top1,top2,down,down1,down2,history;
JLabel name,images;
//JTextField text1,text2;
ImageIcon up1,up2;
JTextPane text1;
JScrollPane pane;
ScriptEngine engine;
Border em;
boolean endwith1;
int numbcount=1,opercount=1,numbcount1=1;
boolean operaffer=true;
int lensold;
boolean baketsub=true;
boolean dotcheck=false;
boolean baketcheck=true;
int baketcount=0;
int baketcount1=1;
boolean check;
boolean check1;
SimpleAttributeSet attrib;

public Calculator()
{
    frame = new JFrame();

    engine = new ScriptEngineManager().getEngineByExtension("js");

    name = new JLabel(" Calculator");
    name.setForeground(Color.WHITE);
    name.setFont(new FontUIResource("Franklin Gothic Medium", Font.PLAIN, 18));

    one = new JButton("1");
    one.addActionListener(this);
    one.setFocusPainted(false);
    one.setBackground(Color.WHITE);
    one.setForeground(Color.BLACK);
    one.setBorder(BorderFactory.createLineBorder(new Color(240, 240, 240),1));
    one.setFont(new FontUIResource("OCR-B 10 BT", Font.PLAIN, 25));

    two = new JButton("2");
    two.addActionListener(this);
    two.setFocusPainted(false);
    two.setBackground(Color.WHITE);
    two.setForeground(Color.BLACK);
    two.setBorder(BorderFactory.createLineBorder(new Color(240, 240, 240),1));
    two.setFont(new Font("OCR-B 10 BT", Font.PLAIN, 25));

    three = new JButton("3");
    three.addActionListener(this);
    three.setFocusPainted(false);
    three.setBackground(Color.WHITE);
    three.setForeground(Color.BLACK);
    three.setBorder(BorderFactory.createLineBorder(new Color(240, 240, 240),1));
    three.setFont(new Font("OCR-B 10 BT", Font.PLAIN, 25));

    four = new JButton("4");
    four.addActionListener(this);
    four.setFocusPainted(false);
    four.setBackground(Color.WHITE);
    four.setForeground(Color.BLACK);
    four.setBorder(BorderFactory.createLineBorder(new Color(240, 240, 240),1));
    four.setFont(new Font("OCR-B 10 BT", Font.PLAIN, 25));

    five = new JButton("5");
    five.addActionListener(this);
    five.setFocusPainted(false);
    five.setBackground(Color.WHITE);
    five.setForeground(Color.BLACK);
    five.setBorder(BorderFactory.createLineBorder(new Color(240, 240, 240),1));
    five.setFont(new Font("OCR-B 10 BT", Font.PLAIN, 25));

    six = new JButton("6");
    six.addActionListener(this);
    six.setFocusPainted(false);
    six.setBackground(Color.WHITE);
    six.setForeground(Color.BLACK);
    six.setBorder(BorderFactory.createLineBorder(new Color(240, 240, 240),1));
    six.setFont(new Font("OCR-B 10 BT", Font.PLAIN, 25));

    seven = new JButton("7");
    seven.addActionListener(this);
    seven.setFocusPainted(false);
    seven.setBackground(Color.WHITE);
    seven.setForeground(Color.BLACK);
    seven.setBorder(BorderFactory.createLineBorder(new Color(240, 240, 240),1));
    seven.setFont(new Font("OCR-B 10 BT", Font.PLAIN, 25));

    eight = new JButton("8");
    eight.addActionListener(this);
    eight.setFocusPainted(false);
    eight.setBackground(Color.WHITE);
    eight.setForeground(Color.BLACK);
    eight.setBorder(BorderFactory.createLineBorder(new Color(240, 240, 240),1));
    eight.setFont(new Font("OCR-B 10 BT", Font.PLAIN, 25));

    nine = new JButton("9");
    nine.addActionListener(this);
    nine.setFocusPainted(false);
    nine.setBackground(Color.WHITE);
    nine.setForeground(Color.BLACK);
    nine.setBorder(BorderFactory.createLineBorder(new Color(240, 240, 240),1));
    nine.setFont(new Font("OCR-B 10 BT", Font.PLAIN, 25));

    zone = new JButton("0");
    zone.addActionListener(this);
    zone.setFocusPainted(false);
    zone.setBackground(Color.WHITE);
    zone.setForeground(Color.BLACK);
    zone.setBorder(BorderFactory.createLineBorder(new Color(240, 240, 240),1));
    zone.setFont(new Font("OCR-B 10 BT", Font.PLAIN, 25));

    cancel = new JButton("x");
    cancel.addActionListener(this);
    cancel.setFocusPainted(false);
    cancel.setBackground(new Color(48, 174, 184));
    cancel.setForeground(Color.WHITE);
    cancel.setBorder(new EmptyBorder(6,6,6,6));
    cancel.setFont(new Font("OCR-A BT", Font.PLAIN, 25));

    cancel1 = new JButton("X");
    cancel1.addActionListener(this);
    cancel1.setFocusPainted(false);
    cancel1.setBackground(new Color(250, 250, 250));
    cancel1.setForeground(new Color(255, 0, 0));
    cancel1.setBorder(BorderFactory.createLineBorder(new Color(240, 240, 240),1));
    cancel1.setFont(new Font("OCR-A BT", Font.BOLD, 25));

    clear = new JButton("C");
    clear.addActionListener(this);
    clear.setFocusPainted(false);
    clear.setBackground(new Color(250, 250, 250));
    clear.setForeground(new Color(49, 166, 216));
    clear.setBorder(BorderFactory.createLineBorder(new Color(240, 240, 240),1));
    clear.setFont(new Font("OCR-B 10 BT", Font.BOLD,35));

    sub = new JButton("-");
    sub.addActionListener(this);
    sub.setFocusPainted(false);
    sub.setBackground(new Color(250, 250, 250));
    sub.setForeground(new Color(49, 166, 216));
    sub.setBorder(BorderFactory.createLineBorder(new Color(240, 240, 240),1));
    sub.setFont(new Font("OCR-B 10 BT", Font.PLAIN, 35));

    add = new JButton("+");
    add.addActionListener(this);
    add.setFocusPainted(false);
    add.setBackground(new Color(250, 250, 250));
    add.setForeground(new Color(49, 166, 216));
    add.setBorder(BorderFactory.createLineBorder(new Color(240, 240, 240),1));
    add.setFont(new Font("OCR-B 10 BT", Font.PLAIN, 35));

    mult = new JButton("*");
    mult.addActionListener(this);
    mult.setFocusPainted(false);
    mult.setBackground(new Color(250, 250, 250));
    mult.setForeground(new Color(49, 166, 216));
    mult.setBorder(BorderFactory.createLineBorder(new Color(240, 240, 240),1));
    mult.setFont(new Font("OCR-B 10 BT", Font.PLAIN, 35));

    div = new JButton("/");
    div.addActionListener(this);
    div.setFocusPainted(false);
    div.setBackground(new Color(250, 250, 250));
    div.setForeground(new Color(49, 166, 216));
    div.setBorder(BorderFactory.createLineBorder(new Color(240, 240, 240),1));
    div.setFont(new Font("OCR-B 10 BT", Font.PLAIN, 30));

    equal = new JButton("=");
    equal.addActionListener(this);
    equal.setFocusPainted(false);
    equal.setBackground(new Color(250, 250, 250));
    equal.setForeground(new Color(0, 153, 0));
    equal.setBorder(BorderFactory.createLineBorder(new Color(240, 240, 240),1));
    equal.setFont(new Font("OCR-B 10 BT", Font.BOLD, 35));

    dot = new JButton(".");
    dot.addActionListener(this);
    dot.setFocusPainted(false);
    dot.setBackground(Color.WHITE);
    dot.setForeground(Color.BLACK);
    dot.setBorder(BorderFactory.createLineBorder(new Color(240, 240, 240),1));
    dot.setFont(new Font("OCR-B 10 BT", Font.PLAIN, 25));

    adsub = new JButton("+/-");
    adsub.addActionListener(this);
    adsub.setFocusPainted(false);
    adsub.setBackground(Color.WHITE);
    adsub.setForeground(Color.BLACK);
    adsub.setBorder(BorderFactory.createLineBorder(new Color(240, 240, 240),1));
    adsub.setFont(new Font("OCR-B 10 BT", Font.PLAIN, 25));

    baket = new JButton("()");
    baket.addActionListener(this);
    baket.setFocusPainted(false);
    baket.setBackground(new Color(250, 250, 250));
    baket.setForeground(new Color(49, 166, 216));
    baket.setBorder(BorderFactory.createLineBorder(new Color(240, 240, 240),1));
    baket.setFont(new Font("OCR-B 10 BT", Font.PLAIN, 25));

    //text1 = new JTextField();
    //text1.setColumns(10);
    //text1.setFont(new Font("OCR-B 10 BT",Font.PLAIN, 35));
    //text1.setBorder(new EmptyBorder(4, 7, 4, 7));
    //text1.setHorizontalAlignment(SwingConstants.TRAILING);
    //text1.setCaretColor(new Color(49, 166, 216));



    attrib  = new SimpleAttributeSet();
    StyleConstants.setAlignment(attrib, StyleConstants.ALIGN_RIGHT);
    StyleConstants.setFontSize(attrib, 30);
    text1 = new JTextPane();
    text1.setEditorKit(new WrapEditorKit());
    text1.setParagraphAttributes(attrib, true);
    text1.setBorder(new EmptyBorder(4, 7, 4, 7));
    text1.setCaretColor(new Color(49, 166, 216));
    text1.setPreferredSize(new Dimension(270, 130));
    JScrollPane scrollpane = new JScrollPane(text1);
    scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    scrollpane.getVerticalScrollBar().setPreferredSize(new Dimension(5, 5));
    scrollpane.getVerticalScrollBar().setUI(new ScrollLookandFeel());
    em = BorderFactory.createEmptyBorder(0, 0, 0, 0);
    scrollpane.setBorder(em);
    UIManager.put("ScrollBarUI", "My.package.ScrollLookandFeel");


    //text2 = new JTextField();z
    //text2.setFont(new Font("OCR-B 10 BT",Font.PLAIN, 35));
    // text2.setBorder(new EmptyBorder(4, 7, 4, 7));
    // text2.setHorizontalAlignment(SwingConstants.RIGHT);
    // text2.setForeground(new Color(0, 153, 0));
    // text2.setCaretColor(new Color(49, 166, 216));

    up2 = new ImageIcon(CreateImage("/project/1467651440_calculator.png").getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH));
    images = new JLabel(up2);
    images.setBorder(BorderFactory.createLineBorder(new Color(48, 174, 184),4));

    top = new JPanel();
    top1 = new JPanel();
    top1.setSize(200,400);
    top1.setBackground(new Color(48, 174, 184));
    top1.setLayout(new BorderLayout());
    top1.add(images,BorderLayout.WEST);
    top1.add(name,BorderLayout.CENTER);
    top1.add(cancel,BorderLayout.EAST);

    top2 = new JPanel();
    top2.setLayout(new BorderLayout());
    top2.add(scrollpane,BorderLayout.NORTH);
   //top2.add(text2,BorderLayout.CENTER);

    top.setLayout(new BorderLayout());
    top.add(top1,BorderLayout.NORTH);
    top.add(top2,BorderLayout.CENTER);

    down = new JPanel();
    down.setLayout(new BorderLayout());

    up1 = new ImageIcon(CreateImage("/project/1467658414_arrow-down-01_1.png").getImage().getScaledInstance(15, 15, Image.SCALE_SMOOTH));

    up = new JButton();
    up.setIcon(up1);
    up.setFocusPainted(false);
    up.setBackground(new Color(240, 240, 240));
    up.setForeground(Color.BLACK);
    up.setBorder(BorderFactory.createLineBorder(new Color(187, 197, 201),0));
    up.setFont(new Font("OCR-B 10 BT", Font.PLAIN, 15));

    down1 = new JPanel();
    down1.setLayout(new BorderLayout());
    down1.add(up);

    down2 = new JPanel();
    down2.setBackground(new Color(240, 240, 240));
    down2.setLayout(new GridLayout(5, 4));
    down2.add(clear);
    down2.add(div);
    down2.add(mult);
    down2.add(cancel1);
    down2.add(seven);
    down2.add(eight);
    down2.add(nine);
    down2.add(sub);
    down2.add(four);
    down2.add(five);
    down2.add(six);
    down2.add(add);
    down2.add(one);
    down2.add(two);
    down2.add(three);
    down2.add(baket);
    down2.add(zone);
    down2.add(dot);
    down2.add(adsub);
    down2.add(equal);

    down.add(down1,BorderLayout.NORTH);
    down.add(down2,BorderLayout.CENTER);

    frame.add(top,BorderLayout.NORTH);
    frame.add(down,BorderLayout.CENTER);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setSize(270,450);
    frame.setUndecorated(true);
    frame.setIconImage(CreateImage("/project/1467651440_calculator.png").getImage().getScaledInstance(60, 60, Image.SCALE_SMOOTH));
    frame.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e)
        {
            point.x = e.getX();
            point.y = e.getY();
        }
    });

    frame.addMouseMotionListener(new MouseMotionAdapter() {
        @Override
        public void mouseDragged(MouseEvent e)
        {
            Point p = frame.getLocation();
            frame.setLocation(p.x+e.getX()-point.x,p.y+e.getY()-point.y);
        }

    });
    frame.setVisible(true);
}

private ImageIcon CreateImage(String path)
{
    return new ImageIcon(java.awt.Toolkit.getDefaultToolkit().getClass().getResource(path));
}

@Override
public void actionPerformed(ActionEvent e) {

    switch(e.getActionCommand())
    {
        case "1":
            numberAction("1");
            break;
        case "2":
            numberAction("2");
            break;
        case "3":
            numberAction("3");
            break;
        case "4":
            numberAction("4");
            break;
        case "5":
            numberAction("5");
            break;
        case "6":
            numberAction("6");
            break;    
        case "7":
            numberAction("7");
            break;    
        case "8":
            numberAction("8");
            break;    
        case "9":
            numberAction("9");
            break;
        case "0":
            numberAction("0");
            break;
        case ".":
            dots(".");
            break;
        case "+":
            digitalAction("+");
            break;
        case "-":
            digitalAction("-");
            break; 
        case "*":
            digitalAction("*");
            break; 
        case "/":
            digitalAction("/");
            break;     
        case "=":
              total();
            break;    
        case "C":
              clear(); 
            break;
        case "X":
              cancel1(); 
            break;
        case "()":
            Baket();
            break;
        case "+/-":
            baketsub();
            break;
    }

    if(e.getSource()==cancel)
    {        
        frame.setVisible(false);
        frame.dispose();
    }
}

private void clear() 
{
   numbcount=1;
   opercount=1;
   numbcount1=1;
   baketcount=0;
   baketcount1=1;
   baketcheck=true;
   baketsub=true;
   operaffer=true;
   text1.setText("");
}

private void digitalAction(String operation) 
{
    if(!text1.getText().equals(""))
    {    
        String txt1 = text1.getText();
        endwith1 = txt1.endsWith(operation);
        System.out.println("text "+endwith1);

        int len = txt1.length();
        String subs = txt1.substring(len-1);
        System.out.println(subs);

        if(endwith1==true)
        {

        }else
        {
            if(!subs.equals("+") && !subs.equals("-") && !subs.equals("/") && !subs.equals("*"))
            {    
                if(opercount<=5)
                {   
                    text1.setText(text1.getText()+operation);
                    opercount++;
                }

                String txt10  = text1.getText();
                boolean add1 = txt10.endsWith("+");
                boolean sub1 = txt10.endsWith("-");
                boolean mult1 = txt10.endsWith("*");
                boolean div1 = txt10.endsWith("/");

                if(true==add1 ||true==sub1 ||true==mult1 || true==div1 )
                {
                    operaffer=false;
                    numbcount1=1;
                }

            }else
            {
                String hide=text1.getText();
                text1.setText("");
                for(int J=0;J<hide.length()-1;J++)
                {    
                    text1.setText(text1.getText()+hide.charAt(J));
                }

                text1.setText(text1.getText()+operation);
            }    
        }
    }else
    {
        text1.setText("");
    }  
    baketcheck=true;
}

private void numberAction(String buttonNumber)
{    
    if(true==operaffer)
    {    
        String txt2 = text1.getText();
        boolean endwith2 = txt2.endsWith(")");

        if(true!=endwith2)
        {    
            if(!text1.getText().equals("0"))
            {  
                if(numbcount<=5)
                {   
                    text1.setText(text1.getText()+buttonNumber);
                    numbcount++;
                }
                dotcheck=true;
                baketsub=false;

                String txt3 = text1.getText();
                lensold = txt3.length();
                System.out.println("len"+lensold);
                check = txt3.contains(".");
                System.out.println(check); 
            }else
            {
                String hide=text1.getText();
                text1.setText("");
                for(int J=0;J<hide.length()-1;J++)
                {    
                    text1.setText(text1.getText()+hide.charAt(J));
                }
                text1.setText(text1.getText()+buttonNumber);
            }
        }
    }else
    {
        String txt4 = text1.getText();
        int len = txt4.length();
        String ss = txt4.substring(len-2);
        System.out.println("ss "+ss);

        if(ss.equals("+0") || ss.equals("-0") || ss.equals("*0") || ss.equals("/0"))
        {
            String hide=text1.getText();
            text1.setText("");
            for(int J=0;J<hide.length()-1;J++)
            {    
                text1.setText(text1.getText()+hide.charAt(J));
            }
            text1.setText(text1.getText()+buttonNumber);
        }
        else
        {
            String txt5 = text1.getText();
            boolean endwith3 = txt5.endsWith(")");

            if(true!=endwith3)
            { 
                if(numbcount1<=5)
                {   
                    text1.setText(text1.getText()+buttonNumber);
                    numbcount1++;
                }
                dotcheck=true;
                baketsub=false;

                String txt6 = text1.getText();
                int le = txt6.length();
                int subt = le-numbcount1;
                String subs1 = txt6.substring(subt);
                System.out.println("tt"+subt);
                System.out.println("tt"+subs1);
                check1 = subs1.contains(".");
                System.out.println("tt"+check1);    
                check=false;
            }
        }
    }
    baketcheck=false;  

    String txt = text1.getText();
    int ff = txt.length();
    System.out.print("ffss"+ff);
    if(ff>=14)
    {
    StyleConstants.setFontSize(attrib, 25);
    text1.setParagraphAttributes(attrib, true);
        if(ff>=25)
        {
            StyleConstants.setFontSize(attrib, 20);
            text1.setParagraphAttributes(attrib, true);
        }
    }
}

private void dots(String dot) 
{   
    String txt7 = text1.getText();
    boolean endwith4 = txt7.endsWith(dot);

    if(true == dotcheck)
    {
        if(check==false && check1==false)
        {
            if(false==endwith4)
            {
                text1.setText(text1.getText()+dot);
            }
        }
    } 
} 

public  void total()
{
    try
    {    
        Object result = engine.eval(text1.getText());

        if(result==null)
        {
            text1.setText("Wrong null");
        }else
        {
            text1.setText("\n ="+result.toString()); 
        }
    }
    catch(ScriptException es)
    {
        text1.setText("Wrong format");
    }
} 

private void cancel1() {

    if(!text1.getText().equals(""))   
    {
        String txt8  = text1.getText();
        boolean endwith5 = txt8.endsWith("(");
        if(true==endwith5)
        {
            String hide=text1.getText();
            text1.setText("");
            for(int J=0;J<hide.length()-1;J++)
            {
                text1.setText(text1.getText()+hide.charAt(J));
            }
            baketcount--; 
            baketcount1=1;

            baketsub=true;
        }else
        {
            String txt9 = text1.getText();
            int length = txt9.length();
            String subs2 = txt9.substring(length-1);
            System.out.println(subs2);
            if(subs2.equals("+") || subs2.equals("-") ||subs2.equals("/")  || subs2.equals("-") )
            {
                String hide=text1.getText();
                text1.setText("");
                for(int J=0;J<hide.length()-1;J++)
                {
                    text1.setText(text1.getText()+hide.charAt(J));
                }
                opercount--;
            }else
            {
                if(length<=lensold)
                {    
                    String hide=text1.getText();
                    text1.setText("");
                    for(int J=0;J<hide.length()-1;J++)
                    {
                        text1.setText(text1.getText()+hide.charAt(J));
                    }
                    numbcount--; 
                    operaffer=true;
                    baketsub=true;
                    baketcheck=true;
                }else
                {
                    String hide=text1.getText();
                    text1.setText("");
                    for(int J=0;J<hide.length()-1;J++)
                    {
                        text1.setText(text1.getText()+hide.charAt(J));
                    }
                    numbcount1--;
                }    
            }
        }

        String txt = text1.getText();
        int ff = txt.length();
        System.out.print("ffss"+ff);
        if(ff<=14)
        {
            StyleConstants.setFontSize(attrib, 30);
            text1.setParagraphAttributes(attrib, true);
        }
    }      
}

private void Baket() 
{
    if(baketcheck==true)
    {
        text1.setText(text1.getText()+"(");
        baketcount++;
    }else
    {
        if(baketcount1<=baketcount)
        {    
            text1.setText(text1.getText()+")");
            baketcount1++;
        }else
        {
            text1.setText(text1.getText()+"*");
            baketcheck=true;
            operaffer=false;
            numbcount1=1;
            opercount++;
        }
    }    
    System.out.print(baketcount);
}

private void baketsub() 
{
    if(text1.getText().equals(""))
    {
        if(baketsub==true)
        {
        text1.setText(text1.getText()+"(-");
        }
        baketcheck=false;
        baketcount++;
        opercount++;
    }
}

private static class ScrollLookandFeel  extends BasicScrollBarUI {

     private ScrollLookandFeel() {

    }

    @Override
    protected void configureScrollBarColors() {

        thumbColor = Color.LIGHT_GRAY;
        thumbDarkShadowColor = Color.LIGHT_GRAY;
        thumbHighlightColor = Color.LIGHT_GRAY;
        thumbLightShadowColor = Color.lightGray;

        trackColor = new Color(245, 245, 245);
        trackHighlightColor = new Color(245, 245, 245);
    }

    @Override
    protected JButton createDecreaseButton(int orientation) {
        JButton button = new BasicArrowButton(orientation);
        button.setBackground(new Color(245, 245, 245));
        button.setForeground(new Color(245, 245, 245));
        button.setBorder(new EmptyBorder(10, 10, 10, 10));
        return button;
    }

    @Override
    protected JButton createIncreaseButton(int orientation) {
        JButton button = new BasicArrowButton(orientation);
        button.setBackground(new Color(245, 245, 245));
        button.setForeground(new Color(245, 245, 245));
        button.setBorder(new EmptyBorder(10, 10, 10, 10));
        return button;
    }
}

class WrapEditorKit extends StyledEditorKit
{
   final ViewFactory defaults = new WrapColumnFactory();

    @Override
    public ViewFactory getViewFactory()
    {
        return defaults;
    }
}

class WrapColumnFactory implements ViewFactory
{
    @Override
    public View create(Element elem) {
       String ki = elem.getName();
       if(ki!=null)
       {
           switch (ki) {
               case AbstractDocument.ContentElementName:
                   return new WrapLabelView(elem);
               case AbstractDocument.ParagraphElementName:
                   return new ParagraphView(elem);
               case AbstractDocument.SectionElementName:
                   return new BoxView(elem, View.Y_AXIS);
               case StyleConstants.ComponentElementName:
                   return new ComponentView(elem);
               case StyleConstants.IconElementName:
                   return new IconView(elem);
           }
       }
        return new LabelView(elem);
    }  
}

class WrapLabelView extends LabelView
{
    public WrapLabelView(Element elem) 
    {
       super(elem);
    }

    @Override
    public float getMinimumSpan(int axis)
    {
        switch (axis)
        {
            case View.X_AXIS:
                return 0;
            case View.Y_AXIS:
                return super.getMinimumSpan(axis);
            default:
                throw new IllegalArgumentException("Invalid axis"+axis);
        }
    }
}

public static void main(String []args)
{
    Calculator c = new Calculator();
}  
}
于 2016-07-18T12:52:27.220 回答