Applet转换后Java程序空白

时间:2009-11-30 19:07:06

标签: java

我一直在研究一个简单的Java应用程序。我有它编译和运行。我做了更改,把它变成了一个Applet。但是,当我完成时,HTML的applet部分显示为空白......没有错误。

我决定恢复更改,以便我可以编译它并在我的IDE中运行它。一旦我这样做,该程序将成功编译,但不会产生我的JFrame。

任何人都可以看到为什么这不起作用......

//Import packages for GUI development.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.io.*;

public class Powell_5 extends JFrame
{
  //Left Panel of GUI.
      JButton btnSnickers = new JButton("Snickers");
      JButton btnButterFinger = new JButton("Butterfinger");
      JButton btnLays = new JButton("Lays Chips");
      JButton btnCola = new JButton("Coca-Cola");
      JButton btnDCola = new JButton("Diet Coke");
      JLabel  lblQuantity1 = new JLabel(" Quantity:");
      JLabel  lblQuantity2 = new JLabel(" Quantity:");
      JLabel  lblQuantity3 = new JLabel(" Quantity:");
      JLabel  lblQuantity4 = new JLabel(" Quantity:");
      JLabel  lblQuantity5 = new JLabel(" Quantity:");
      JTextField txtSnickers = new JTextField("0",3);
      JTextField txtButterFinger = new JTextField("0",3);
      JTextField txtLays = new JTextField("0",3);   
      JTextField txtCola = new JTextField("0",3); 
     JTextField txtDCola = new JTextField("0",3); 

     //Right Panel of GUI
      JButton btnClear = new JButton("Clear");
      JButton btnOrder = new JButton("Order");
      JLabel  lblEmployee = new JLabel("Employee Number:");
      JLabel  lblItems = new JLabel("Total Items Selected");
      JLabel  lblAmount = new JLabel("Total Amount of Order:");
      JTextField txtEmployee = new JTextField(5);
      JTextField txtItems = new JTextField("0",5);
      JTextField txtAmount = new JTextField("$0.00",8);

       //Variables for holding dollar amounts.
      int intSnickers;
      int intButterFinger;
      int intLays;
      int intCola;
      int intDCola;
      String strEmployee;
      int intTotalItems;
      double dblTotalAmount;      
      StringBuilder BuiltOrder = new StringBuilder();

         //Declare variables for main frame
         int WINDOW_WIDTH = 640;
       int WINDOW_HEIGHT = 235;

      //Declare some error string     
      String strTooMany = "The maximum number or each item that can be selected is 3.";
        //Set up to format dollar
        DecimalFormat dollar = new DecimalFormat("0.00");


     public void Powell_5()
     {  
         //create a window
         JFrame window = new JFrame();

         //set title of JFrame
         window.setTitle("");

         //assign constants to window size
         window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

         //specify "X" button property
         window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


         //Set up Border Layout here
         window.setLayout(new BorderLayout());


         //Mnemonic for buttons
     btnSnickers.setMnemonic(KeyEvent.VK_S);
         btnLays.setMnemonic(KeyEvent.VK_L);
         btnButterFinger.setMnemonic(KeyEvent.VK_B);
         btnCola.setMnemonic(KeyEvent.VK_C);
         btnDCola.setMnemonic(KeyEvent.VK_D);
         btnClear.setMnemonic(KeyEvent.VK_E);
         btnOrder.setMnemonic(KeyEvent.VK_O);

         //Set Tooltip
         btnClear.setToolTipText("Click here to erase any pending order, and start over");
         btnOrder.setToolTipText("Click here to place your order");     

         //Make quantity, amount and items all read only
         txtItems.setEditable(false);
         txtSnickers.setEditable(false);
         txtButterFinger.setEditable(false);
         txtLays.setEditable(false);
        txtCola.setEditable(false);
        txtDCola.setEditable(false);
         txtAmount.setEditable(false);

         //Right align all numeric and currency fields.
         txtSnickers.setHorizontalAlignment(JTextField.RIGHT);
         txtLays.setHorizontalAlignment(JTextField.RIGHT);
         txtButterFinger.setHorizontalAlignment(JTextField.RIGHT);
         txtCola.setHorizontalAlignment(JTextField.RIGHT); 
         txtDCola.setHorizontalAlignment(JTextField.RIGHT); 
         txtAmount.setHorizontalAlignment(JTextField.RIGHT);
         txtItems.setHorizontalAlignment(JTextField.RIGHT); 


        //Create Products Type Panel
        JPanel Products = new JPanel();
         Products.setLayout(new GridLayout(6,3));

         Products.add(btnSnickers);
         Products.add(lblQuantity1);
         Products.add(txtSnickers); 
         Products.add(btnButterFinger);
         Products.add(lblQuantity2);
         Products.add(txtButterFinger); 
         Products.add(btnLays);
         Products.add(lblQuantity3);
         Products.add(txtLays);     
         Products.add(btnCola);
         Products.add(lblQuantity4);
         Products.add(txtCola);     
         Products.add(btnDCola);
         Products.add(lblQuantity5);
         Products.add(txtDCola);

        JPanel Details = new JPanel();
        Details.setLayout(new GridLayout(5,2));

        //add controls to panel
        Details.add(lblEmployee);
        Details.add(txtEmployee);
        Details.add(lblItems);
        Details.add(txtItems);
        Details.add(lblAmount);
        Details.add(txtAmount);
        Details.add(btnClear);
        Details.add(btnOrder);

        //Create button listeners
        btnOrder.addActionListener(new btnOrder());
        btnClear.addActionListener(new btnClearListener());
        btnSnickers.addActionListener(new btnProduct());
        btnButterFinger.addActionListener(new btnProduct());
        btnLays.addActionListener(new btnProduct());
        btnCola.addActionListener(new btnProduct());
        btnDCola.addActionListener(new btnProduct());

        //Add panels to window
       window.add(Products, BorderLayout.WEST);
        window.add(Details, BorderLayout.EAST);

       //Pack the contents and set visible to true
         pack();    
          window.setVisible(true);                              
}

    private class btnClearListener implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {

              intSnickers = 0;
              intButterFinger = 0;
               intLays = 0;
               intCola = 0;
               intDCola = 0;
               strEmployee = "";
               intTotalItems = 0;
                dblTotalAmount = 0;


            //Clear text fields
            txtSnickers.setText("0");
            txtButterFinger.setText("0");
            txtLays.setText("0");
            txtCola.setText("0");
            txtDCola.setText("0");
            txtEmployee.setText("0");
            txtDCola.setText("0");
            txtItems.setText("0");
            txtAmount.setText("$0.00");         

            }
        }

    private class btnProduct implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {    
                if (e.getSource() == btnSnickers)
                    {   
                         intSnickers++;                  
                         if(intSnickers > 3)
                            {
                              intSnickers = 3;
                              JOptionPane.showMessageDialog(null, strTooMany, "Exceeds Maximum Quantity",JOptionPane.WARNING_MESSAGE);
                              dblTotalAmount = dblTotalAmount - .75 ;
                              intTotalItems--;

                            }

                         txtSnickers.setText(String.valueOf(intSnickers));                            
                    }

                else if  (e.getSource() == btnLays)
                    {
                        intLays++;
                        if(intLays > 3)
                            {
                              intLays = 3;
                              JOptionPane.showMessageDialog(null, strTooMany, "Exceeds Maximum Quantity",JOptionPane.WARNING_MESSAGE);
                              dblTotalAmount = dblTotalAmount - .75 ;
                              intTotalItems--;

                            }

                         txtLays.setText(String.valueOf(intLays));      
                    }   
                else if  (e.getSource() == btnButterFinger)
                    {
                        intButterFinger++;
                       if(intButterFinger > 3)
                            {
                              intButterFinger = 3;
                              JOptionPane.showMessageDialog(null, strTooMany, "Exceeds Maximum Quantity",JOptionPane.WARNING_MESSAGE);
                              dblTotalAmount = dblTotalAmount - .75 ;
                              intTotalItems--;

                            }

                         txtButterFinger.setText(String.valueOf(intButterFinger));      
                    }
                else if  (e.getSource() == btnCola)
                    {
                        intCola++;
                     if(intCola > 3)
                            {
                              intCola = 3;
                              JOptionPane.showMessageDialog(null, strTooMany, "Exceeds Maximum Quantity",JOptionPane.WARNING_MESSAGE);
                              dblTotalAmount = dblTotalAmount - .75 ;
                              intTotalItems--;

                            }

                         txtCola.setText(String.valueOf(intCola));
                     }
                else if  (e.getSource() == btnDCola)
                    {
                     intDCola++;
                     if(intDCola > 3)
                            {
                              intDCola = 3;
                              JOptionPane.showMessageDialog(null, strTooMany, "Exceeds Maximum Quantity",JOptionPane.WARNING_MESSAGE);
                              dblTotalAmount = dblTotalAmount - .75 ;
                              intTotalItems--;

                            }

                         txtDCola.setText(String.valueOf(intDCola));                            }   

                intTotalItems++;
                txtItems.setText(String.valueOf(intTotalItems));

                dblTotalAmount = dblTotalAmount + .75 ;
                txtAmount.setText(String.valueOf(dollar.format(dblTotalAmount)));

            }
        }

    private class btnOrder implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {   
                strEmployee = txtEmployee.getText();

                if (strEmployee.length() == 4 && Character.isLetter(strEmployee.charAt(0)) && Character.isDigit(strEmployee.charAt(1)) && Character.isDigit(strEmployee.charAt(2)) && Character.isDigit(strEmployee.charAt(3)))
                 {
                        if (intTotalItems > 0 )
                         {

                          if (intSnickers > 0)
                             BuiltOrder.append( intSnickers + "Snickers" + "\n");

                         if (intLays > 0)
                             BuiltOrder.append("intLays + \"Lays\" + \"\\n\"");

                    JOptionPane.showMessageDialog(null, "Order for Employee " + strEmployee + ": \n" + BuiltOrder  + "\n\n Total Items: " + intTotalItems
                                                                      + "\n Total Price: $: " + dollar.format(dblTotalAmount));   
                  }                     
                else
                    {
                     JOptionPane.showMessageDialog(null, "At least 1 item must be selected.", "Invalid Order Quantity",JOptionPane.ERROR_MESSAGE);                      
                    }


                 }  

                else
                 {
                   JOptionPane.showMessageDialog(null, "The employee code must be 4 digits long.", "Invalid Employee Entry",JOptionPane.ERROR_MESSAGE);
                 }      
         }
}
    //Main method to create an instance of the class
public static void main(String[] args)
        {
            new Powell_5();

        } 

}

2 个答案:

答案 0 :(得分:1)

有些值得检查的事情:

  • 有任何例外吗?
  • 使用jstack,调试器或其他东西来查看是否存在任何死锁或无限循环。
  • 使用调试器/ printf查看您希望执行的代码是否有效。

其他建议:

  • 除非确实有必要,否则请勿延伸JFrame(或JPanel或类似)。
  • 您正在扩展JFrame,并且正在创建一个JFrame window
  • 使用Swing(java.awt.EventQueue.invokeLater)时,坚持使用AWT事件派遣线程EDT。
  • 遵守标准编码标准(包括变量命名)。
  • 将字段设为私有(并尽可能将其设为最终版)。
  • 大多数字段可能都是局部变量。

答案 1 :(得分:1)

从Powell_5()声明中删除“void”:

public Powell_5()
 {  
     //etc