2013-11-24 82 views
0

我一直在这个问题上工作了几个小时,没有任何结果。在查看HTML页面或通过Eclipse运行applet时,我似乎无法使applet正确显示。它始终是空白的。我一直在寻找很长一段时间,并决定要问。 下面的代码:Java applet显示为空白

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

public class Topics extends Applet{ 
    String topicTotal = ""; 
    JPanel topicPanel; 
    JLabel title, username, topic, paragraph, topicsTitle; 
    JTextField nameField, topicField; 
    JButton submitButton; 
    JTextArea paragraphArea; 

    public String getTopicTotal() { 
     return topicTotal; 
    } 


    public JPanel createContentPane(){ 
     final JPanel topicGUI = new JPanel(); 
     topicGUI.setLayout(null); 

     //posting panel 
     final JPanel postPanel = new JPanel(); 
     postPanel.setLayout(null); 
     postPanel.setLocation(0, 0); 
     postPanel.setSize(500, 270); 
     topicGUI.add(postPanel); 

     setVisible(true); 

     // JLabels 

     JLabel title = new JLabel("Make A Post"); 
     title.setLocation(170, 3); 
     title.setSize(150,25); 
     title.setFont(new Font("Serif", Font.PLAIN, 25)); 
     title.setHorizontalAlignment(0); 
     postPanel.add(title); 

     JLabel username = new JLabel("Username: "); 
     username.setLocation(20, 30); 
     username.setSize(70,15); 
     username.setHorizontalAlignment(0); 
     postPanel.add(username); 

     JLabel topic = new JLabel("Topic: "); 
     topic.setLocation(20, 50); 
     topic.setSize(40,15); 
     topic.setHorizontalAlignment(0); 
     postPanel.add(topic); 

     JLabel paragraph = new JLabel("Paragraph: "); 
     paragraph.setLocation(20, 70); 
     paragraph.setSize(70,15); 
     paragraph.setHorizontalAlignment(0); 
     postPanel.add(paragraph); 

     // JTextFields 

     nameField = new JTextField(8); 
     nameField.setLocation(90, 30); 
     nameField.setSize(150, 18); 
     postPanel.add(nameField); 

     topicField = new JTextField(8); 
     topicField.setLocation(60, 50); 
     topicField.setSize(180, 18); 
     postPanel.add(topicField); 

     // JTextAreas 

     paragraphArea = new JTextArea(8, 5); 
     paragraphArea.setLocation(20, 85); 
     paragraphArea.setSize(450, 100); 
     paragraphArea.setLineWrap(true); 
     paragraphArea.setEditable(true); 
     JScrollPane scrollParagraph = new JScrollPane (paragraphArea); 
     scrollParagraph.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
     postPanel.add(paragraphArea); 

     // JButton 

     JButton submitButton = new JButton("SUBMIT"); 
     submitButton.setLocation(250, 30); 
     submitButton.setSize(100, 30); 
     submitButton.addActionListener(new ActionListener(){ 
      @Override 
      public void actionPerformed(ActionEvent e) { 

       topicTotal = topicTotal + "/n" + nameField + "/n" + topicTotal + "/n" + paragraphArea + "/n"; 
     } 
     }); 
     postPanel.add(submitButton); 

     setVisible(true); 

      return topicGUI; 
     } 

     private static void createAndShowGUI() { 


      JFrame frame = new JFrame(""); 

      Topics display = new Topics(); 
      frame.setContentPane(display.createContentPane()); 
      frame.setSize(500, 270); 
      frame.setVisible(true); 

     } 

     public static void main(String[] args) { 
      SwingUtilities.invokeLater(new Runnable() { 
       public void run() { 
        createAndShowGUI(); 
       } 
      }); 
     } 

} 
+0

它为我的NetBeans –

+0

运行完全正常内部的所有组件,你是运行它作为一个应用程序或小程序?因为我的应用程序运行良好,但作为applet运行时空白。 – Dimfish

+0

您如何将applet附加到您的HTML中,并且您是否在浏览器上启用了Java? –

回答

0

为了作为一个applet运行,你需要重写init方法。您不需要main方法。只需添加init方法

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

public class Topics extends Applet { 
String topicTotal = ""; 
JPanel topicPanel; 
JLabel title, username, topic, paragraph, topicsTitle; 
JTextField nameField, topicField; 
JButton submitButton; 
JTextArea paragraphArea; 

public String getTopicTotal() { 
    return topicTotal; 
} 

public void init() { 
    final JPanel topicGUI = new JPanel(); 
    topicGUI.setLayout(null); 

    // posting panel 
    final JPanel postPanel = new JPanel(); 
    postPanel.setLayout(null); 
    postPanel.setLocation(0, 0); 
    postPanel.setSize(500, 270); 
    add(postPanel); 

    setVisible(true); 

    // JLabels 

    JLabel title = new JLabel("Make A Post"); 
    title.setLocation(170, 3); 
    title.setSize(150, 25); 
    title.setFont(new Font("Serif", Font.PLAIN, 25)); 
    title.setHorizontalAlignment(0); 
    add(title); 

    JLabel username = new JLabel("Username: "); 
    username.setLocation(20, 30); 
    username.setSize(70, 15); 
    username.setHorizontalAlignment(0); 
    add(username); 

    JLabel topic = new JLabel("Topic: "); 
    topic.setLocation(20, 50); 
    topic.setSize(40, 15); 
    topic.setHorizontalAlignment(0); 
    add(topic); 

    JLabel paragraph = new JLabel("Paragraph: "); 
    paragraph.setLocation(20, 70); 
    paragraph.setSize(70, 15); 
    paragraph.setHorizontalAlignment(0); 
    add(paragraph); 

    // JTextFields 

    nameField = new JTextField(8); 
    nameField.setLocation(90, 30); 
    nameField.setSize(150, 18); 
    add(nameField); 

    topicField = new JTextField(8); 
    topicField.setLocation(60, 50); 
    topicField.setSize(180, 18); 
    add(topicField); 

    // JTextAreas 

    paragraphArea = new JTextArea(8, 5); 
    paragraphArea.setLocation(20, 85); 
    paragraphArea.setSize(450, 100); 
    paragraphArea.setLineWrap(true); 
    paragraphArea.setEditable(true); 
    JScrollPane scrollParagraph = new JScrollPane(paragraphArea); 
    scrollParagraph 
      .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
    add(paragraphArea); 

    // JButton 

    JButton submitButton = new JButton("SUBMIT"); 
    submitButton.setLocation(250, 30); 
    submitButton.setSize(100, 30); 
    submitButton.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 

      topicTotal = topicTotal + "/n" + nameField + "/n" + topicTotal 
        + "/n" + paragraphArea + "/n"; 
     } 
    }); 
    add(submitButton); 
} 

} 
+0

非常感谢,完美的作品! – Dimfish