2012-04-02 61 views
0

我目前正在爲學校開發一個程序。我們必須製作一個Applet,並選擇了JApplet。出於某種原因,我用來顯示特定字符​​串的面板不會顯示。這可能是什麼問題?請指向正確的方向。另外,我看了一些教程,一些人建議我有「開始」,「停止」和「破壞」方法,有些則沒有。這些方法中的任何一種都會影響我的JPanel爲什麼不顯示圖形?Java圖形JApplet

謝謝

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

public class Shape extends JApplet { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 

    // making the radiobuttons for the shape choices 
    JRadioButton squareButton = new JRadioButton("Square",true); 
    JRadioButton ovalButton = new JRadioButton("Oval",false); 
    JRadioButton rectangleButton = new JRadioButton("Rectangle",false); 
    JRadioButton triangleButton = new JRadioButton("Triangle",false); 

    // making radiobuttons for the color choices 
    JRadioButton redButton = new JRadioButton("Red",true); 
    JRadioButton blueButton = new JRadioButton("Blue",false); 
    JRadioButton greenButton = new JRadioButton("Green",false); 
    JRadioButton yellowButton = new JRadioButton("Yellow",false); 

    // making buttons draw and animate 
    JButton drawButton = new JButton("Draw!"); 
    JButton animateButton = new JButton("Animate!"); 

    // making JTextFields for length and width 
    JTextField lengthField = new JTextField("Enter a length",15); 
    JTextField widthField = new JTextField("Enter a width",15); 

    // making JPanel, in which the radiobuttons will go01 
    JPanel shapePanel = new JPanel(); 
    JPanel colorPanel = new JPanel(); 
    JPanel buttonPanel = new JPanel(); 
    JPanel textPanel = new JPanel(); 
    drawPanel dPanel; 

    ButtonGroup shapeGroup = new ButtonGroup(); 
    ButtonGroup colorGroup = new ButtonGroup(); 

    // variables that will dictates the shape, size and color 
    int length = 200; 
    int width = 200; 
    Color color = Color.RED; 
    String shape = "square"; 

    public void init() { 
     setLayout(new FlowLayout()); // setting layout for the applet 
     setSize(680,480); 

     // setting the layout for the shapePanel - gridlayout 2 rows, 2 cols and space of 5 
     shapePanel.setLayout(new GridLayout(2,2,5,5)); 

     // adding a border to the shapePanel to indicate to the user what it does "titledBorder" 
     shapePanel.setBorder(BorderFactory.createTitledBorder("Choose a shape")); 

     // setting layout for the color panel - gridlayout 2 rows, 2 cols and space of 5 
     colorPanel.setLayout(new GridLayout(2,2,5,5)); 

     // adding a border to the colorPanel to indicate to the user what it does "titledBorder" 
     colorPanel.setBorder(BorderFactory.createTitledBorder("Choose a color")); 

     // setting the layout for the buttonPanel - gridlayout 1 row, 2 cols and space of 5 
     buttonPanel.setLayout(new GridLayout(1,2,5,5)); 

     // adding a color border 
     buttonPanel.setBorder(BorderFactory.createLineBorder(Color.red, 2)); 

     // setting the layout of the textField - gridlayout 1 row, 2 cols and space of 5 
     textPanel.setLayout(new GridLayout(1,2,5,5)); 

     // adding some attributes for lengthField and widthField 
     lengthField.setFont(new Font("Arial",Font.PLAIN,12)); 
     lengthField.setForeground(new Color(150,150,150)); 

     widthField.setFont(new Font("Arial",Font.PLAIN,12)); 
     widthField.setForeground(new Color(150,150,150)); 

     // using shapegroup to organize the JRadioButtons 
     shapeGroup.add(squareButton); 
     shapeGroup.add(ovalButton); 
     shapeGroup.add(rectangleButton); 
     shapeGroup.add(triangleButton); 

     // using colorgroup to organize the color radiobuttons 
     colorGroup.add(redButton); 
     colorGroup.add(blueButton); 
     colorGroup.add(greenButton); 
     colorGroup.add(yellowButton); 

     // add the shape buttons to the panel so they appear in a square form 
     shapePanel.add(squareButton); 
     shapePanel.add(ovalButton); 
     shapePanel.add(rectangleButton); 
     shapePanel.add(triangleButton); 

     // adding color buttons to the color panel 
     colorPanel.add(redButton); 
     colorPanel.add(blueButton); 
     colorPanel.add(greenButton); 
     colorPanel.add(yellowButton); 

     // adding jbuttons 
     buttonPanel.add(drawButton); 
     buttonPanel.add(animateButton); 

     // adding textfields to the textPanel 
     textPanel.add(lengthField); 
     textPanel.add(widthField); 

     dPanel = new drawPanel(); 

     // adding panels to the applet 
     add(shapePanel); 
     add(colorPanel); 
     add(buttonPanel); 
     add(textPanel); 
     add(dPanel); 

     // adding focus listener to lengthField and widthField 
     lengthField.addFocusListener(new FocusListener() { 

      public void focusGained(FocusEvent e) { 
       lengthField.setText(""); 
       lengthField.setForeground(Color.black); 
      } 

      public void focusLost(FocusEvent e) {} 

     }); 

     widthField.addFocusListener(new FocusListener() { 

      public void focusGained(FocusEvent e) { 
       widthField.setText(""); 
       widthField.setForeground(Color.black); 
      } 

      public void focusLost(FocusEvent e) {} 

     }); 

     drawButton.addActionListener(new drawListener()); 

    } 

    // when the person presses paint, this will be executed to paint the specific shape, color with the width and length 
    class drawListener implements ActionListener { 
     public void actionPerformed(ActionEvent e) { 

      int mylength = 5; 
      int mywidth = 5; 

      try { 
       mylength = Integer.parseInt(lengthField.getText());; 
       mywidth = Integer.parseInt(widthField.getText());; 
      }catch(Exception ex) { 
       JOptionPane.showMessageDialog(null,""+ex,"",JOptionPane.ERROR_MESSAGE); 
      } 

      if((mylength > 200 || mylength < 5)) { 
       JOptionPane.showMessageDialog(null, "Please make sure the number is above 5 and less than 200", 
         "Invalid length message", JOptionPane.ERROR_MESSAGE); 
      }else if((mywidth > 200 || mywidth < 5)) { 
       JOptionPane.showMessageDialog(null, "Please make sure the number is above 5 and less than 200", 
         "Invalid width message", JOptionPane.ERROR_MESSAGE); 
      }else { 
       length = mylength; 
       width = mywidth; 

       // checking which color button is selected 
       if(redButton.isSelected()) { 
        color = Color.RED; 
       }else if(blueButton.isSelected()) { 
        color = Color.BLUE; 
       }else if(greenButton.isSelected()) { 
        color = Color.GREEN; 
       }else if(yellowButton.isSelected()) { 
        color = Color.YELLOW; 
       } 

       // checking which shape has been selected 
       if(rectangleButton.isSelected()) { 
        shape = "rectangle"; 
       }else if(triangleButton.isSelected()) { 
        shape = "triangle"; 
       }else if(ovalButton.isSelected()) { 
        shape = "oval"; 
       }else if(squareButton.isSelected()) { 
        shape = "square"; 
       } 

       //System.out.printf("%3d %3d %s %s \n",length,width,shape,color); 

      } 

     } 
    } 

    // This will be used to do the painting 
    class drawPanel extends JPanel { 
     private static final long serialVersionUID = 1L; 

     //Paint Method 
     public void paintComponent(Graphics g){ 
      super.paintComponent(g); 
      Graphics2D g2 = (Graphics2D) g; 
      g2.setColor(Color.black); 

      g2.drawString("My awesome string", 200, 200); 
     } 

    } 

} 

回答

3

一個JPanel沒有組件已爲0x0的默認大小。試試這個來源:

// <applet code=Shape width=640 height=480></applet> 
import java.awt.*; 

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

public class Shape extends JApplet { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 

    // making the radiobuttons for the shape choices 
    JRadioButton squareButton = new JRadioButton("Square",true); 
    JRadioButton ovalButton = new JRadioButton("Oval",false); 
    JRadioButton rectangleButton = new JRadioButton("Rectangle",false); 
    JRadioButton triangleButton = new JRadioButton("Triangle",false); 

    // making radiobuttons for the color choices 
    JRadioButton redButton = new JRadioButton("Red",true); 
    JRadioButton blueButton = new JRadioButton("Blue",false); 
    JRadioButton greenButton = new JRadioButton("Green",false); 
    JRadioButton yellowButton = new JRadioButton("Yellow",false); 

    // making buttons draw and animate 
    JButton drawButton = new JButton("Draw!"); 
    JButton animateButton = new JButton("Animate!"); 

    // making JTextFields for length and width 
    JTextField lengthField = new JTextField("Enter a length",15); 
    JTextField widthField = new JTextField("Enter a width",15); 

    // making JPanel, in which the radiobuttons will go01 
    JPanel shapePanel = new JPanel(); 
    JPanel colorPanel = new JPanel(); 
    JPanel buttonPanel = new JPanel(); 
    JPanel textPanel = new JPanel(); 
    drawPanel dPanel; 

    ButtonGroup shapeGroup = new ButtonGroup(); 
    ButtonGroup colorGroup = new ButtonGroup(); 

    // variables that will dictates the shape, size and color 
    int length = 200; 
    int width = 200; 
    Color color = Color.RED; 
    String shape = "square"; 

    public void init() { 
     setLayout(new FlowLayout()); // setting layout for the applet 
     // This is set by HTML! 
     //setSize(680,480); 

     // setting the layout for the shapePanel - gridlayout 2 rows, 2 cols and space of 5 
     shapePanel.setLayout(new GridLayout(2,2,5,5)); 

     // adding a border to the shapePanel to indicate to the user what it does "titledBorder" 
     shapePanel.setBorder(BorderFactory.createTitledBorder("Choose a shape")); 

     // setting layout for the color panel - gridlayout 2 rows, 2 cols and space of 5 
     colorPanel.setLayout(new GridLayout(2,2,5,5)); 

     // adding a border to the colorPanel to indicate to the user what it does "titledBorder" 
     colorPanel.setBorder(BorderFactory.createTitledBorder("Choose a color")); 

     // setting the layout for the buttonPanel - gridlayout 1 row, 2 cols and space of 5 
     buttonPanel.setLayout(new GridLayout(1,2,5,5)); 

     // adding a color border 
     buttonPanel.setBorder(BorderFactory.createLineBorder(Color.red, 2)); 

     // setting the layout of the textField - gridlayout 1 row, 2 cols and space of 5 
     textPanel.setLayout(new GridLayout(1,2,5,5)); 

     // adding some attributes for lengthField and widthField 
     lengthField.setFont(new Font("Arial",Font.PLAIN,12)); 
     lengthField.setForeground(new Color(150,150,150)); 

     widthField.setFont(new Font("Arial",Font.PLAIN,12)); 
     widthField.setForeground(new Color(150,150,150)); 

     // using shapegroup to organize the JRadioButtons 
     shapeGroup.add(squareButton); 
     shapeGroup.add(ovalButton); 
     shapeGroup.add(rectangleButton); 
     shapeGroup.add(triangleButton); 

     // using colorgroup to organize the color radiobuttons 
     colorGroup.add(redButton); 
     colorGroup.add(blueButton); 
     colorGroup.add(greenButton); 
     colorGroup.add(yellowButton); 

     // add the shape buttons to the panel so they appear in a square form 
     shapePanel.add(squareButton); 
     shapePanel.add(ovalButton); 
     shapePanel.add(rectangleButton); 
     shapePanel.add(triangleButton); 

     // adding color buttons to the color panel 
     colorPanel.add(redButton); 
     colorPanel.add(blueButton); 
     colorPanel.add(greenButton); 
     colorPanel.add(yellowButton); 

     // adding jbuttons 
     buttonPanel.add(drawButton); 
     buttonPanel.add(animateButton); 

     // adding textfields to the textPanel 
     textPanel.add(lengthField); 
     textPanel.add(widthField); 

     dPanel = new drawPanel(); 
     dPanel.setPreferredSize(new Dimension(500,300)); 

     // adding panels to the applet 
     add(shapePanel); 
     add(colorPanel); 
     add(buttonPanel); 
     add(textPanel); 
     add(dPanel); 

     // adding focus listener to lengthField and widthField 
     lengthField.addFocusListener(new FocusListener() { 

      public void focusGained(FocusEvent e) { 
       lengthField.setText(""); 
       lengthField.setForeground(Color.black); 
      } 

      public void focusLost(FocusEvent e) {} 

     }); 

     widthField.addFocusListener(new FocusListener() { 

      public void focusGained(FocusEvent e) { 
       widthField.setText(""); 
       widthField.setForeground(Color.black); 
      } 

      public void focusLost(FocusEvent e) {} 

     }); 

     drawButton.addActionListener(new drawListener()); 

    } 

    // when the person presses paint, this will be executed to paint the specific shape, color with the width and length 
    class drawListener implements ActionListener { 
     public void actionPerformed(ActionEvent e) { 

      int mylength = 5; 
      int mywidth = 5; 

      try { 
       mylength = Integer.parseInt(lengthField.getText());; 
       mywidth = Integer.parseInt(widthField.getText());; 
      }catch(Exception ex) { 
       JOptionPane.showMessageDialog(null,""+ex,"",JOptionPane.ERROR_MESSAGE); 
      } 

      if((mylength > 200 || mylength < 5)) { 
       JOptionPane.showMessageDialog(null, "Please make sure the number is above 5 and less than 200", 
         "Invalid length message", JOptionPane.ERROR_MESSAGE); 
      }else if((mywidth > 200 || mywidth < 5)) { 
       JOptionPane.showMessageDialog(null, "Please make sure the number is above 5 and less than 200", 
         "Invalid width message", JOptionPane.ERROR_MESSAGE); 
      }else { 
       length = mylength; 
       width = mywidth; 

       // checking which color button is selected 
       if(redButton.isSelected()) { 
        color = Color.RED; 
       }else if(blueButton.isSelected()) { 
        color = Color.BLUE; 
       }else if(greenButton.isSelected()) { 
        color = Color.GREEN; 
       }else if(yellowButton.isSelected()) { 
        color = Color.YELLOW; 
       } 

       // checking which shape has been selected 
       if(rectangleButton.isSelected()) { 
        shape = "rectangle"; 
       }else if(triangleButton.isSelected()) { 
        shape = "triangle"; 
       }else if(ovalButton.isSelected()) { 
        shape = "oval"; 
       }else if(squareButton.isSelected()) { 
        shape = "square"; 
       } 

       //System.out.printf("%3d %3d %s %s \n",length,width,shape,color); 

      } 

     } 
    } 

    // This will be used to do the painting 
    class drawPanel extends JPanel { 
     private static final long serialVersionUID = 1L; 

     //Paint Method 
     public void paintComponent(Graphics g){ 
      super.paintComponent(g); 
      Graphics2D g2 = (Graphics2D) g; 
      g2.setColor(Color.black); 

      g2.drawString("My awesome string", 200, 200); 
     } 

    } 

} 
+0

你先生,是救命的人。非常感謝你。我想知道是否可以請刪除此代碼,因爲這是針對一個項目的,我不想讓其他人複製它。再一次感謝你。 – 2012-04-02 22:38:59

+2

*「我想知道您是否可以刪除此代碼,」*我可以,但不會。這不是SO的工作原理。如果您需要私人解決方案,請聘請私人導師。 – 2012-04-02 22:41:20

+2

@ user681159:所以你說實際上你借用這段代碼是可以的,但是對於其他人來說這樣做不好嗎?那是什麼?你比其他誰可以從中受益更好或更重要嗎?或者是你想爲你的任務提交這段代碼的原因,就像你寫了它一樣。無論哪種方式,這太臭了。 – 2012-04-02 22:46:10