简体   繁体   English

Java Applet,当我按Java Applet运行时未运行

[英]Java Applet, Not Running when I press run as Java applet

I'm fairly new to coding, I just wanted to create a simple GUI. 我对编码还很陌生,我只想创建一个简单的GUI。 The goal of my code is to design an app to look something like this: !http://curriculum.kcdistancelearning.com/courses/PROG2s-HS-A08/s/unit7/resources/images/JV_7.5.11.JPG![Example of goal of code] I have been using the window builder and I like how it looks inside of the builder. 我的代码的目的是设计一个看起来像这样的应用程序:!http://curriculum.kcdistancelearning.com/courses/PROG2s-HS-A08/s/unit7/resources/images/JV_7.5.11.JPG!代码目标示例]我一直在使用窗口构建器,我喜欢它在构建器内部的外观。 My problem is when I try to run my code as a Java application. 我的问题是当我尝试将代码作为Java应用程序运行时。 An error that says "Selection does not contain an applet" I think I may have missed something. 一个错误消息:“选择不包含小程序”,我想我可能错过了一些东西。 I'm not sure what I am missing, or what it would be. 我不确定我想念的是什么,或者会是什么样。

Here is what my code looks like currently: 这是我的代码当前的样子:

`package Create.GUI`
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.SpringLayout;
import javax.swing.ImageIcon;
import javax.swing.SwingConstants;
import javax.swing.JScrollPane;
import java.awt.TextArea;
import java.awt.Button;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Canvas;
import javax.swing.JProgressBar;
import javax.swing.JToggleButton;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JScrollBar;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPasswordField;
import java.awt.BorderLayout;
public class StringPannel1 extends JFrame 
{
    private StringPannel1 currentPannel;
    public StringPannel1() {
        getContentPane().setBackground(Color.WHITE);
        SpringLayout springLayout = new SpringLayout();
        getContentPane().setLayout(springLayout);

    JButton btnNewButton = new JButton("Button");
    springLayout.putConstraint(SpringLayout.NORTH, btnNewButton, 23, SpringLayout.NORTH, getContentPane());
    springLayout.putConstraint(SpringLayout.WEST, btnNewButton, 23, SpringLayout.WEST, getContentPane());
    springLayout.putConstraint(SpringLayout.SOUTH, btnNewButton, 88, SpringLayout.NORTH, getContentPane());
    getContentPane().add(btnNewButton);

    txtTextFeild = new JTextField();
    txtTextFeild.setBackground(new Color(248, 248, 255));
    springLayout.putConstraint(SpringLayout.NORTH, txtTextFeild, 0, SpringLayout.NORTH, btnNewButton);
    springLayout.putConstraint(SpringLayout.WEST, txtTextFeild, 27, SpringLayout.EAST, btnNewButton);
    springLayout.putConstraint(SpringLayout.SOUTH, txtTextFeild, 0, SpringLayout.SOUTH, btnNewButton);
    springLayout.putConstraint(SpringLayout.EAST, txtTextFeild, -53, SpringLayout.EAST, getContentPane());
    txtTextFeild.setText("Text Feild");
    getContentPane().add(txtTextFeild);
    txtTextFeild.setColumns(10);

    JScrollBar scrollBar = new JScrollBar();
    springLayout.putConstraint(SpringLayout.NORTH, scrollBar, 17, SpringLayout.SOUTH, btnNewButton);
    springLayout.putConstraint(SpringLayout.WEST, scrollBar, 23, SpringLayout.WEST, getContentPane());
    springLayout.putConstraint(SpringLayout.SOUTH, scrollBar, 130, SpringLayout.SOUTH, btnNewButton);
    getContentPane().add(scrollBar);

    JTextArea txtrTextArea = new JTextArea();
    txtrTextArea.setBackground(new Color(248, 248, 255));
    springLayout.putConstraint(SpringLayout.NORTH, txtrTextArea, 0, SpringLayout.NORTH, scrollBar);
    springLayout.putConstraint(SpringLayout.WEST, txtrTextArea, 7, SpringLayout.EAST, scrollBar);
    springLayout.putConstraint(SpringLayout.SOUTH, txtrTextArea, 0, SpringLayout.SOUTH, scrollBar);
    springLayout.putConstraint(SpringLayout.EAST, txtrTextArea, 173, SpringLayout.EAST, scrollBar);
    txtrTextArea.setText("Text Area");
    getContentPane().add(txtrTextArea);

    JCheckBox chckbxCheckBox = new JCheckBox("Check Box");
    chckbxCheckBox.setBackground(new Color(255, 255, 255));
    springLayout.putConstraint(SpringLayout.NORTH, chckbxCheckBox, 58, SpringLayout.SOUTH, txtTextFeild);
    springLayout.putConstraint(SpringLayout.WEST, chckbxCheckBox, 6, SpringLayout.EAST, txtrTextArea);
    getContentPane().add(chckbxCheckBox);

    JLabel lblLabel = new JLabel("Label");
    springLayout.putConstraint(SpringLayout.NORTH, lblLabel, 15, SpringLayout.SOUTH, txtrTextArea);
    springLayout.putConstraint(SpringLayout.WEST, lblLabel, 104, SpringLayout.WEST, getContentPane());
    lblLabel.setFont(new Font("Tahoma", Font.PLAIN, 13));
    getContentPane().add(lblLabel);
}
private JTextField txtTextFelid;
/**
 * @wbp.nonvisual location=-193,109
 */
private final JButton button = new JButton("New button");
private JTextField txtTextFeild;
public void StringPannel()
{
    currentPannel = new StringPannel1();

    setupFrame();
}

private void setupFrame() 
{
    this.setVisible(true);
    this.setContentPane(currentPannel);
    this.setSize(350, 350);
    this.setDefultCloseOpperation(JFrame.EXIT_ON_CLOSE);
}
 private void setDefultCloseOpperation(int exitOnClose) {
    // TODO Auto-generated method stub

}
{

{
    setupPannel1()
}
private void setupPannel() 
{
    setBorder(new LineBorder(new Color(0, 0, 255)));
    setBackground(new Color(255, 255, 255));
    SpringLayout springLayout = new SpringLayout();
    getContentPane().setLayout(springLayout);




}

private void setBorder(LineBorder lineBorder) {
    // TODO Auto-generated method stub

}
}

All help is appreciated! 感谢所有帮助!

Basically, you application extends from JFrame and not JApplet . 基本上,您的应用程序是从JFrame而不是JApplet扩展的。

There is a (significant) difference between the two... 两者之间存在(显着)差异...

I would be the first person to tell... 我将是第一个告诉...的人

  1. Always create your core UI based on JPanel and then add this/these to the container you want to use ( JFrame or JApplet ), it makes you code much more reusable and allows you to deploy the application as you see fit without have to resort to some kind of hack... 始终基于JPanel创建核心UI,然后将此/这些添加到要使用的容器( JFrameJApplet )中,它使您的代码更具可重用性,并允许您随意部署应用程序而不必诉诸于某种骇客...
  2. You should avoid using applets until you understand how the UI works. 在了解UI的工作原理之前,应避免使用applet。 Applets bring there own issues which can make your live very miserable until you understand them 小程序会带来一些自己的问题,这些问题会使您的生活变得十分痛苦,直到您了解它们为止

If you're hell bent on developing an applet, take a look at Lesson: Java Applets for more details 如果您不愿意开发小程序,请查看课程:Java小程序以获取更多详细信息。

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

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