Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我的计算器程序在添加显示器时会中断?_Java_Jframe_Calculator_Jtextarea - Fatal编程技术网

Java 为什么我的计算器程序在添加显示器时会中断?

Java 为什么我的计算器程序在添加显示器时会中断?,java,jframe,calculator,jtextarea,Java,Jframe,Calculator,Jtextarea,我正在编写一个用java模拟windows计算器的程序。我把大部分图形都放下来了,但当我在文本区域(显示器)中添加时,它会破坏格式,甚至不会在其中显示。 有趣的是,在输入文本区域之前,大多数按钮只显示“…”而不是它们的实际值。据推测,这是因为文本对于按钮来说太大了,但是,随着显示器的加入,这些按钮突然变得足够大,可以在按钮上显示全文,并且对于框架来说太宽了 import java.util.*; import javax.swing.*; import java.awt.*; import ja

我正在编写一个用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){}

    }

} 
我也不确定我是否把这些都写对了,我以前从来没有在这里问过问题。谢谢

编辑:这是我试图模仿的计算器,但它的外观和感觉更“金属”。我已经有了它的外观和感觉,所以它看起来就像我希望它在我的计算器上,但我想保持尺寸的准确性。

只需从代码中删除所有
setPreferredSize()
setSize()
,并在添加所有组件后调用
pack()

什么州? 使此窗口的大小适合其子组件的首选大小和布局。如果任意一个尺寸小于上一次调用setMinimumSize方法指定的最小尺寸,则生成的窗口宽度和高度将自动放大

如果窗口和/或其所有者尚不可显示,则在计算首选大小之前,这两个窗口都可显示。计算窗口大小后,将验证窗口

再做一个改变:

GridBagConstraints c = new GridBagConstraints();
c.fill=GridBagConstraints.BOTH;                 // ADD THIS LINE
阅读更多关于财产的信息

快照:


切勿使用
设置首选尺寸(新尺寸(190,50))如果让布局管理器控制组件的位置和大小,只需保留if即可。@Braj我尝试使用setSize,但这使得按钮在整个时间内都太大了。setPreferredSize()有什么问题?请阅读并找到其他解决方案。你根本不需要设置大小。如果我这样做,它会使整个面板变大,我想让它保持在我设置的尺寸范围内。有没有办法做到这一点?这就是为什么按钮中会出现省略号,因为你将宽度设置为低于最小首选宽度。它是,但理想情况下,我想制作一个几乎完全相同尺寸的WindowsVista计算器克隆。如果我的问题是按钮太小,我可以截图并在按钮上使用图标,而不是正常的数字,因此椭圆不是一个太大的问题。我只想有正确的格式和文本区域更大。基本上,我想保留setPreferredSize(维度)中的维度。我拿出了setPreferredSize()作为显示器,但把它们放在按钮上,我喜欢它的外观。我把一张我想要的图片放在上面
GridBagConstraints c = new GridBagConstraints();
c.fill=GridBagConstraints.BOTH;                 // ADD THIS LINE