2009-11-30 139 views
-1

我一直在研究一個簡單的Java應用程序。我編譯並運行了它。我做了修改,將它變成一個Applet。但是,當我完成時,HTML的小程序部分變爲空白......沒有錯誤。Java程序在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(); 

     } 

} 

回答

1

從Powell_5刪除 「空缺」()聲明:

public Powell_5() 
{ 
    //etc 
+0

謝謝...這是它 – tpow 2009-12-01 04:01:04

1

有些事情值得一試:

  • 任何異常?
  • 使用jstack,一個調試器或其他東西來查看是否有死鎖或無限循環。
  • 使用調試器/ printf來查看您希望執行的代碼是否執行。

其他建議:

  • 請勿延長JFrame(或JPanel或類似的),除非確有必要。
  • 你有一個JFrame你正在擴展,並且你正在創建一個不同的JFramewindow)。
  • 在使用擺動時(java.awt.EventQueue.invokeLater),堅持使用AWT事件調度線程EDT。
  • 遵守標準編碼標準(包括變量命名)。
  • 將字段設爲私有(並在可能的情況下爲最終)。
  • 大部分字段可能會被作爲本地變量。