简体   繁体   中英

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. 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. My problem is when I try to run my code as a Java application. 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 .

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...
  2. You should avoid using applets until you understand how the UI works. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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