Applet没有运行java

时间:2016-05-05 17:28:04

标签: java applet

我想知道为什么我的applet没有运行。我使用netbeans,程序工作正常,没有错误,但我似乎无法让applet屏幕出现,以测试我的程序的所有功能。我想知道是否有人可以指出什么我在启动applet时做错了。我还没有通过applet做很多事情,所以我很好奇为什么它不能为我而努力。我真的不想发布我的程序的完整代码,但我不知道我的代码中是否有错误,或者我只是没有做正确的事情实际上启动applet或其他东西。

运行我的程序时没有出现任何错误,该applet本身并不会出现并运行。否则我在运行我的程序时没有从neatbeans那里得到任何错误。

import java.awt.event.*;
import javax.swing.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.FlowLayout;



public class Program7 extends JApplet implements ActionListener, ItemListener
{

//static varaibles
private static int GuiHeight = 90;
private static int Square = 50;

//gui varaibles
private JRadioButton drawSquare;
private JRadioButton mess;

private JTextField messageFeild;

private JCheckBox color;

private JButton button;

private JComboBox location;

//modable global varaibles
private int width, height, drawX, drawY;
private Color drawColor;
private String draw;

@Override
public void init()
{
    //creating the panels
    JPanel drawChoice;

    JPanel drawSet;

    //setting group onject to link certain buttons together
    ButtonGroup ObjGroup;

    //setting layout to make things look nice
    setLayout(new FlowLayout());

    //ibtializing other varaibles
    width = 0;
    height = 0;
    drawX = 0;
    drawY= 0;
    drawColor = Color.BLACK;
    draw = "";

    //intialzing the gui interface
    drawSquare = new JRadioButton("Draw a Square!");
    mess = new JRadioButton("Write a Message!");
    location = new JComboBox(new String[]{"", "Random Pick", "Top Left"});
    color = new JCheckBox("Draw in Color!");
    button = new JButton("Draw it!");


    //linking the buttons together
    ObjGroup = new ButtonGroup();
    ObjGroup.add(drawSquare);
    ObjGroup.add(mess);


    //adding event handlers to the buttons
    location.addItem(this);
    button.addActionListener(this);
    color.addItemListener(this);
    drawSquare.addActionListener(this);
    mess.addActionListener(this);

    messageFeild = new JTextField(20);


    //adding to the first paels
    drawChoice = new JPanel();
    drawChoice.add(drawSquare);
    drawChoice.add(mess);
    drawChoice.add(messageFeild);
    //adding to the applet
    add(drawChoice);

    //adding to the second panel
    drawSet = new JPanel();
    drawSet.add(new JLabel("Select where to draw!"));
    drawSet.add(location);
    drawSet.add(color);
    drawSet.add(button);
    //adding to the applet
    add(drawSet);

}
@Override
public void paint(Graphics g)
{
    //calling super constructor
    super.paint(g);

    //getting width
    width = getWidth();
    height = getHeight();

    //setting graphics color
    g.setColor(drawColor);
    if (location.getSelectedItem() != "")
    {
        //ifstatement checking for inputs
        if(draw == "Square"||draw == "square"||draw == "SQUARE")
        {
            //creating rectange
            g.fillRect(drawX, drawY, width, height);
        }
        if(draw == "Message"|| draw == "MESSAGE"||draw == "message")
        {
            //writing message
            g.drawString(messageFeild.getText(), drawX, drawY);
        }
    }
}
    @Override
    public void actionPerformed(ActionEvent e)
    {
        //repainting
        repaint();
    }
    @Override
    public void itemStateChanged(ItemEvent e)
    {
        //if statement checking for the various changes being made by the user in the pograms interface

        //if statement check checking to see if there's a square or to write a message
        if (e.getSource() == drawSquare && e.getStateChange() == ItemEvent.SELECTED)
        {
            draw = "Square";
            messageFeild.setEnabled(false);
        }
        if (e.getSource() == mess && e.getStateChange() == ItemEvent.SELECTED)
        {
            draw = "Message";
            messageFeild.setEnabled(true);
        }
        //if statement set checking for locational input
        if (e.getSource() == location)
        {
            if(location.getSelectedItem() == "Random Pick")
            {
                drawX=(int)(Math.random()*(width+1));
                drawY=(int)(GuiHeight+ Math.random()*(height-GuiHeight+1));
            }
            if(location.getSelectedItem() == "Top Left")
            {
                drawX=0;
                drawY=GuiHeight;
            }

        }
        //if statement to check for color change
        if(e.getSource()==color && e.getStateChange()==ItemEvent.SELECTED)
        {
            drawColor = Color.GREEN;
        }
        if(e.getSource()==color && e.getStateChange()==ItemEvent.DESELECTED)
        {
            drawColor = Color.BLACK;
        }


    }



public static void main(String[] args) {

}

}

0 个答案:

没有答案