java applet显示为空白

时间:2013-11-24 16:18:19

标签: java applet

我已经在这个问题上工作了几个小时,没有结果。在查看HTML页面或通过Eclipse运行applet时,我似乎无法正确显示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();
                }
            });
       }

}

1 个答案:

答案 0 :(得分: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);
}

}