簡體   English   中英

添加顯示時,為什么我的計算器程序會中斷?

[英]Why does my calculator program break when a display is added?

我正在編寫一個程序來模擬Java中的Windows計算器。 我將大部分圖形放下,但是當我在文本區域(顯示)中添加內容時,它破壞了格式,甚至沒有顯示內容。有趣的是,在放置文本區域之前,大多數按鈕中的僅顯示“ ...”而不是其實際值。 據推測,這是因為文本對於按鈕來說太大了,但是,添加了顯示后,這些按鈕突然變得足夠大,無法在按鈕上顯示全文,並且對於框架來說變得太寬了。

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

public class Calculator extends JFrame implements ActionListener
{
  public static Scanner in = new Scanner(System.in);

//sets up each JButton
  JButton[] button = new JButton[28];

    JTextArea display = new JTextArea(1,20);

//sets up all the text to put on the buttons
  String[] buttonString = {"MC", "MR", "MS", "M+", "M-",
                                "<-", "CE", "C", "+/-", "squ",
                                "7",  "8",  "9",  "/",  "%",
                                "4",  "5",  "6",  "*", "1/x",
                                "1",  "2",  "3",  "-", "=",
                                "0",        ".",  "+"};

//sets up the boolean values for the different operations
  boolean[] function = {false,false,false,false};
//temporary double values for each number to be for each operation
  double temp1 = 0, temp2 = 0;
//set all font to Times New Roman size 13
  Font font = new Font("Times New Roman", Font.PLAIN, 12);

  public static void main(String[] args)
  {

     //creates calculator
        Calculator calc = new Calculator();
  } 

  public Calculator()
  {
  //makes a calculator with title and size
     super("Calculator");
     setDesign();
     setSize(210,261);
     setResizable(false);
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(3,2,2,3);

    int count = 0;
    /*c.gridx = 0;
    c.gridy = 0;
   display.setEditable(false);
   display.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
   display.setPreferredSize(new Dimension(190,50));
    c.gridwidth = 5;
    display.setFont(font);
  add(display, c);
    c.gridwidth = 1;*/
    for(int i = 1; i < 5; i++)
    {
        for(int k = 0; k < 5; k++)
        {
            c.gridx = k;
            c.gridy = i;
            button[count] = new JButton(buttonString[count]);
            button[count].addActionListener(this);
            button[count].setPreferredSize(new Dimension(34,27));
            button[count].setFont(font);
            add(button[count++], c);
        }
    }
    for(int i = 0; i < 4; i++)
    {
        c.gridx = i;
        c.gridy = 5;
        button[count] = new JButton(buttonString[count]);
        button[count].addActionListener(this);
        button[count].setPreferredSize(new Dimension(34,27));
        button[count].setFont(font);
        add(button[count++], c);

    }

    c.gridx = 4;
    c.gridy = 5;
    button[count] = new JButton(buttonString[count]);
    button[count].addActionListener(this);
    button[count].setPreferredSize(new Dimension(34,59));
    button[count].setFont(font);
    c.gridheight = 2;
    add(button[count++], c);

    c.gridx = 0;
    c.gridy = 6;
    button[count] = new JButton(buttonString[count]);
    button[count].addActionListener(this);
    button[count].setPreferredSize(new Dimension(73,27));
    button[count].setFont(font);
    c.gridwidth = 2;
    c.gridheight = 1;
    add(button[count++], c);

  c.gridx = 2;
    c.gridy = 6;
    button[count] = new JButton(buttonString[count]);
    button[count].addActionListener(this);
    button[count].setPreferredSize(new Dimension(34,27));
    button[count].setFont(font);
    c.gridwidth = 1;
    add(button[count++], c);

    c.gridx = 3;
    c.gridy = 6;
    button[count] = new JButton(buttonString[count]);
    button[count].addActionListener(this);
    button[count].setPreferredSize(new Dimension(34,27));
    button[count].setFont(font);
    add(button[count++], c);

             setVisible(true);

  }



  public final void setDesign()
  {
     try
     {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

     }
        catch(Exception E){}
  }

  public void actionPerformed(ActionEvent ae)
  {
     if(ae.getSource() == button[10])
        display.append("7");
     if(ae.getSource() == button[11])
        display.append("8");
     if(ae.getSource() == button[12])
        display.append("9");
     if(ae.getSource() == button[15])
        display.append("4");
     if(ae.getSource() == button[16])
        display.append("5");
     if(ae.getSource() == button[17])
        display.append("6");
     if(ae.getSource() == button[20])
        display.append("1");
     if(ae.getSource() == button[21])
        display.append("2");
     if(ae.getSource() == button[22])
        display.append("3");
     if(ae.getSource() == button[7])
        clear();
     if(ae.getSource() == button[8])
        switchSign();
     if(ae.getSource() == button[9])
        sqrt();
     if(ae.getSource() == button[10])
        display.append("7");
     if(ae.getSource() == button[13])
     {
        temp1 = Double.parseDouble(display.getText());
        function[1] = true;
        display.setText("");
     }
     if(ae.getSource() == button[18])
     {
        temp1 = Double.parseDouble(display.getText());
        function[0] = true;
        display.setText("");
     }
     if(ae.getSource() == button[23])
     {
        temp1 = Double.parseDouble(display.getText());
        function[3] = true;
        display.setText("");
     }
     if(ae.getSource() == button[27])
     {
        temp1 = Double.parseDouble(display.getText());
        function[2] = true;
        display.setText("");
     }
     if(ae.getSource() == button[24])
        displayResult();
     if(ae.getSource() == button[25])
        display.append("0");
     if(ae.getSource() == button[6])
        display.setText("");
  }

    public void clear()
    {
        try
        {
            display.setText("0");
            for(int i = 0; i < 4; i++)
                function[i] = false;
            temp1 = 0;
            temp2 = 0;
        }
        catch(NullPointerException e){}
    }

    public void clearEntry()
    {
        try
        {
            display.setText("0");
        }catch(NullPointerException e){}
    }

    public void oneoverx()
    {
        try
        {
            if(display.getText() != "0")
                display.setText("" + Double.parseDouble(display.getText()));
        }
        catch(NumberFormatException e){}
    }
    public void sqrt()
    {
        try
        {
            double value = Math.sqrt(Double.parseDouble(display.getText()));
            display.setText("" + value);
        }
        catch(NumberFormatException e){}
    }

    public void switchSign()
    {
        try
        {
            double value = Double.parseDouble(display.getText());

            if(value != 0)
        {
                value = value * -1;
               display.setText(Double.toString(value));
        }
        else{}
        }
        catch(NumberFormatException e){}
    }

    public void displayResult()
    {
        double result = 0; //variable for result
        temp2 = Double.parseDouble(display.getText()); //second temp number from display
        try
        {
            if(Double.toString(temp1).contains("-"))
            {
                String tempString = Double.toString(temp1);
                String temp = Double.toString(temp1).substring(tempString.indexOf("-"));
                temp1 = Double.parseDouble(temp) * -1;
            }   
            if(Double.toString(temp2).contains("-"))
            {
                String tempString = Double.toString(temp2);
                String temp = Double.toString(temp2).substring(tempString.indexOf("-"));
                temp2 = Double.parseDouble(temp) * -1;
            }   
        }
        catch(ArrayIndexOutOfBoundsException e){}
        try
        {
            if(function[0])
                result = temp1 * temp2;
            else if(function[1])
                result = temp1 / temp2;
            else if(function[2])
                result = temp1 + temp2;
            else if(function[3])
                result = temp1 - temp2;
            display.setText(Double.toString(result));
            for(int i = 0; i < 4; i++)
                function[i] = false;

        }
        catch(NumberFormatException e){}

    }

} 

這是我的代碼,我不完全確定這是怎么回事。 當前,該顯示已被注釋掉,如果您想在塊中找到它,則這里是代碼

c.gridx = 0;
c.gridy = 0;
  display.setEditable(false);
  display.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
  display.setPreferredSize(new Dimension(190,50));
    c.gridwidth = 5;
    display.setFont(font);
  add(display, c);
    c.gridwidth = 1;

我也不確定我是否將所有這些內容正確地放入,以前我從未在這里提出過任何問題。 謝謝!

編輯:這是我要模仿的計算器,但外觀和感覺更“金屬”。 我已經有了外觀,因此看起來像我希望在計算器上顯示的樣子,但是我想保持尺寸精確。 計算器

只需從代碼中刪除所有setPreferredSize()setSize() ,然后在添加所有組件之后最后調用pack()

Window#pack()聲明什么?

使此窗口的大小適合其子組件的首選大小和布局。 如果任意一個尺寸小於上一次調用setMinimumSize方法指定的最小尺寸,則窗口的寬度和高度將自動放大。

如果窗口和/或其所有者仍無法顯示,則在計算首選大小之前,將它們都顯示。 計算窗口大小后,將對其進行驗證。

再做一個更改:

GridBagConstraints c = new GridBagConstraints();
c.fill=GridBagConstraints.BOTH;                 // ADD THIS LINE

閱讀有關GridBagConstraints.fill屬性的更多信息。

快照:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM