Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java小程序显示为空_Java_Applet - Fatal编程技术网

java小程序显示为空

java小程序显示为空,java,applet,Java,Applet,我已经研究这个问题好几个小时了,没有结果。在查看HTML页面或通过Eclipse运行小程序时,我似乎无法使小程序正确显示。它总是空白的。我已经找了很长一段时间了,我决定问问。 代码如下: import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.applet.*; public class Topics extends Applet{ String topicTotal = "";

我已经研究这个问题好几个小时了,没有结果。在查看HTML页面或通过Eclipse运行小程序时,我似乎无法使小程序正确显示。它总是空白的。我已经找了很长一段时间了,我决定问问。 代码如下:

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

}

要作为小程序运行,需要重写
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);
}

}

它对我在NetBeans上运行非常好,作为应用程序还是小程序运行?因为我的应用程序运行良好,但作为小程序运行时是空白的。您如何将小程序附加到HTML以及浏览器上是否启用了Java?Java已完全启用,下面是我附加它的方式:为了作为小程序运行,您需要覆盖
init()
方法。我在下面贴了一个答案。我没有得到一个正确格式化它的方法,但它可以运行。您不需要将
main
作为小程序运行。按你想要的方式格式化就行了。但您可以将程序作为小程序运行。