为什么JApplet显示空白灰屏?

时间:2014-11-20 20:32:05

标签: java swing layout graphics

所以我一直试图弄清楚为什么我的JApplet正在启动并且没有出现任何错误,但没有任何错误显示出来。我有一个调用setup_layout()的init()方法,但我想我可能做错了布局。有人帮忙吗?

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;


public class Assignment8 extends JApplet implements ActionListener
{
    public static final int WIDTH = 500;
    public static final int HEIGHT = 500;
    public static final int NUMBER_OF_DIGITS = 30;

    private JTextArea operand, result;
    private double answer = 0.0;
    private JButton sevenB, eightB, nineB, divideB, fourB, fiveB, sixB, multiplyB, oneB, twoB, threeB, subtractB, 
    zeroB, dotB, addB, resetB, clearB, sqrtB, absB, expB;

   /** public static void main(String[] args)
    {
        Assignment8 aCalculator = new Assignment8( );
        aCalculator.setVisible(true);
    }**/

    public void init( )
    {
        setup_layout();
    }

    private void setup_layout() {
        setSize(WIDTH, HEIGHT);

        JPanel MainPanel = new JPanel();
        JPanel buttonPanel = new JPanel();
        JPanel calcPanel = new JPanel();
        JPanel textPanel = new JPanel ();

        buttonPanel.setLayout(new GridLayout(3, 4));
        calcPanel.setLayout(new GridLayout(3, 3));
        textPanel.setLayout(new FlowLayout());
        MainPanel.setLayout(new BorderLayout());
        MainPanel.setBackground(Color.red);

        operand = new JTextArea();   
        result = new JTextArea();   
        operand.setEditable(false);
        result.setEditable(false);
        textPanel.add(operand);
        textPanel.add(result);

        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(".");
        addB = new JButton("+");
        resetB = new JButton("Reset");
        clearB = new JButton("Clear");
        sqrtB = new JButton("Sqrt");
        absB = new JButton("Abs");
        expB = new JButton("Exp");

        sevenB.addActionListener(this);
        eightB.addActionListener(this);
        nineB.addActionListener(this);
        divideB.addActionListener(this);
        fourB.addActionListener(this);
        fiveB.addActionListener(this);
        sixB.addActionListener(this);
        multiplyB.addActionListener(this);
        oneB.addActionListener(this);
        twoB.addActionListener(this);
        threeB.addActionListener(this);
        subtractB.addActionListener(this);
        zeroB.addActionListener(this);
        dotB.addActionListener(this);
        addB.addActionListener(this);
        resetB.addActionListener(this);
        clearB.addActionListener(this);
        absB.addActionListener(this);
        expB.addActionListener(this);
        sqrtB.addActionListener(this);

        buttonPanel.add(sevenB);
        buttonPanel.add(eightB);
        buttonPanel.add(nineB);         
        buttonPanel.add(fourB);
        buttonPanel.add(fiveB);
        buttonPanel.add(sixB);         
        buttonPanel.add(oneB);
        buttonPanel.add(twoB);
        buttonPanel.add(threeB);
        buttonPanel.add(zeroB);
        buttonPanel.add(dotB);

        calcPanel.add(subtractB);
        calcPanel.add(multiplyB);
        calcPanel.add(divideB);
        calcPanel.add(sqrtB);
        calcPanel.add(absB);
        calcPanel.add(expB);
        calcPanel.add(addB);
        calcPanel.add(resetB);
        calcPanel.add(clearB);

        MainPanel.add(buttonPanel);
        MainPanel.add(calcPanel);
        MainPanel.add(textPanel);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
        try
        {
            assumingCorrectNumberFormats(e);
        }
        catch (NumberFormatException e2)
        {
            result.setText("Error: Reenter Number.");
        }
        catch (DivideByZeroException e1) {
            result.setText("Error: CANNOT Divide By Zero. Reenter Number.");
        }
    }


    //Throws NumberFormatException.
    public void assumingCorrectNumberFormats(ActionEvent e) throws DivideByZeroException
    {
        String actionCommand = e.getActionCommand( );

        if (actionCommand.equals("1"))
        {
            int num = 1;
            operand.append(Integer.toString(num));
        }
        else if (actionCommand.equals("2"))
        {
            int num = 2;
            operand.append(Integer.toString(num));
        }
        else if (actionCommand.equals("3"))
        {
            int num = 3;
            operand.append(Integer.toString(num));
        }
        else if (actionCommand.equals("4"))
        {
            int num = 4;
            operand.append(Integer.toString(num));
        }
        else if (actionCommand.equals("5"))
        {
            int num = 5;
            operand.append(Integer.toString(num));
        }
        else if (actionCommand.equals("6"))
        {
            int num = 6;
            operand.append(Integer.toString(num));
        }
        else if (actionCommand.equals("7"))
        {
            int num = 7;
            operand.append(Integer.toString(num));
        }
        else if (actionCommand.equals("8"))
        {
            int num = 8;
            operand.append(Integer.toString(num));
        }
        else if (actionCommand.equals("9"))
        {
            int num = 9;
            operand.append(Integer.toString(num));
        }
        else if (actionCommand.equals("0"))
            {
                int num = 0;
                String text = operand.getText();
                //double check = stringToDouble(text);
                if(text.isEmpty()||text.contains(".")) {
                    operand.append(Integer.toString(num));
                }
            }
        else if (actionCommand.equals("."))
        {
            String text = operand.getText();
            if(!text.contains(".")) {
                operand.append(".");
            }
        }

        else if (actionCommand.equals("+"))
        {
            answer = answer + stringToDouble(operand.getText( ));
            result.setText(Double.toString(answer));
            operand.setText("");
        }
        else if (actionCommand.equals("-"))
        {
            answer = answer - stringToDouble(operand.getText( ));
            result.setText(Double.toString(answer));
            operand.setText("");

        }
        else if (actionCommand.equals("*"))
        {
            answer = answer * stringToDouble(operand.getText( ));
            result.setText(Double.toString(answer));
            operand.setText("");

        }
        else if (actionCommand.equals("/"))
        {
            double check = stringToDouble(operand.getText());
            if(check >= -1.0e-10 && check <= 1.0e-10 ) {
                throw new DivideByZeroException();
            }
            else {
                answer = answer / stringToDouble(operand.getText( ));
                result.setText(Double.toString(answer));
                operand.setText("");
            }

        }
        else if (actionCommand.equals("Sqrt"))
        {
            double check = stringToDouble(result.getText());
            if(check < 0 ) {
                throw new NumberFormatException();
            }
            else {
                answer = Math.sqrt(stringToDouble(result.getText( )));
                result.setText(Double.toString(answer));
                operand.setText("");
            }

        }
        else if (actionCommand.equals("Abs"))
        {
            answer = Math.abs(stringToDouble(result.getText( )));
            result.setText(Double.toString(answer));
            operand.setText("");

        }
        else if (actionCommand.equals("Exp"))
        {
            answer = Math.pow(stringToDouble(result.getText( )),stringToDouble(operand.getText( )));
            result.setText(Double.toString(answer));
            operand.setText("");
        }
        else if (actionCommand.equals("Reset"))
        {
            answer = 0.0;
            result.setText("");
            operand.setText("");
        }
        else if (actionCommand.equals("Clear"))
        {
            operand.setText("");
        }
        else
            result.setText("Unexpected error.");
     }


    //Throws NumberFormatException.
    private static double stringToDouble(String stringObject)
    {
        return Double.parseDouble(stringObject.trim( ));
    }

   //Divide by Zero Exception Class 
   public class DivideByZeroException extends Exception {
        DivideByZeroException() {
        }
   }

}

1 个答案:

答案 0 :(得分:2)

您永远不会将MainPanel添加到applet本身。

即,

add(MainPanel);

此外,由于MainPanel使用BorderLayout,您需要添加一个BorderLayout.XXX常量的子面板。即,

改变这个:

  MainPanel.add(buttonPanel);
  MainPanel.add(calcPanel);
  MainPanel.add(textPanel);

为:

  MainPanel.add(buttonPanel, BorderLayout.CENTER);
  MainPanel.add(calcPanel, BorderLayout.PAGE_END); // wherever you want to add this
  MainPanel.add(textPanel, BorderLayout.PAGE_START);

请注意,使用数组或列表可以大大简化您的代码。

或者可以通过其他方式简化它,例如:

public void assumingCorrectNumberFormats(ActionEvent e)
     throws DivideByZeroException {
  String actionCommand = e.getActionCommand();
  String numbers = "1234567890";

  if (numbers.contains(actionCommand)) {
     operand.append(actionCommand);

     // if you need to work with it as a number
     int num = Integer.parseInt(actionCommand);
  }

我自己,我使用不同的ActionListener来实现不同的功能,例如一个ActionListener用于数字和.输入按钮,另一个用于操作按钮。