简体   繁体   English

Java小程序显示为空白

[英]The java applet appears blank

I've been working at this problem for hours now, with no results. 我已经在这个问题上工作了几个小时,没有任何结果。 I cannot seem to get the applet to display properly when viewing the HTML page OR when running the applet through Eclipse. 在查看HTML页面或通过Eclipse运行applet时,我似乎无法正确显示applet。 It is always blank. 它总是空白。 I've been searching for a long time now and decided just to ask. 我已经搜寻了很长时间,所以决定问一下。 Here's the code: 这是代码:

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();
                }
            });
       }

}

In order to run as an applet, you need to override the init method. 为了作为小程序运行,您需要重写init方法。 You don't need a main method. 您不需要main方法。 Just add all you components inside the init method 只需将所有组件添加到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);
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM