2012-04-02 10 views
0

私は現在、学校向けのプログラムに取り組んでいます。私たちはアプレットを作る必要があり、私はJAppletを選択しました。何らかの理由で、特定の文字列を表示するために使用しているパネルが表示されません。ここで何が問題になるでしょうか?正しい方向に私を指差してください。また、私はいくつかのチュートリアルを見て、私は "開始"、 "停止"と "destory"メソッドを持っていると示唆したものもあれば、そうでないものもありました。 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

A 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

あなたは人生保護者です。どうもありがとうございました。これはプロジェクト用で、誰かにコピーさせたくないので、このコードを削除してもいいかどうか疑問に思っていました。もう一度ありがとうございます。 –

+2

*「このコードを削除してもらえますか?」と思っていましたが、できません。それはSOの仕組みではありません。プライベートソリューションが必要な場合は、プライベートチューターを雇う。 –

+2

@ user681159:あなたはこのコードを借りても問題ないと言っていますが、他の人が同じコードを実行することはできません。それは何ですか?あなたはこれで恩恵を受けることができる他の人よりも優れているのですか?または、あなたが書いたかのように、あなたの割り当てのためにこのコードを提出したいという理由があります。いずれにせよ、これは悪臭を放つ。 –