无法使JApplet工作。每次我运行程序时,都会出现错误

时间:2019-05-04 03:12:35

标签: java nullpointerexception japplet

每次运行代码时,都会在代码顶部列出此错误。

我的程序运行时颜色没有变化,但自从我增加了更改数字颜色的功能以来,我一直无法使它正常工作。任何帮助表示赞赏。对于我的Java类,该程序是一个问题。

java.lang.NullPointerException
    at ColorChange.init(ColorChange.java:29)
    at sun.applet.AppletPanel.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)" 

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;


public class ColorChange extends JApplet implements ItemListener, 
ActionListener
{


private JRadioButton blackRb, blueRb, orangeRb, greenRb, yellowRb, redRb, 
pinkRb, grayRb;
private Color currentColor=Color.magenta;   
private ButtonGroup colorSelect;    
private JTextField numberTF;
private JLabel label;
private JButton okB;
private int num=0;

public void init() {

Container c=getContentPane();
c.setLayout(null);
setSize(500,300);

label=new JLabel("Enter number");
label.setLocation(200,40);
label.setSize(100,30);

numberTF=new JTextField(1);
numberTF.setLocation(300,40);

okB.setLocation(400,40);
okB.setSize(50,30);
okB.addActionListener(this);

c.add(label);
c.add(numberTF);
c.add(okB);

blackRb=new JRadioButton("Black");
blueRb=new JRadioButton("Blue");
orangeRb=new JRadioButton("Orange");
greenRb=new JRadioButton("Green");
yellowRb=new JRadioButton("Yellow");
redRb=new JRadioButton("Red");
pinkRb=new JRadioButton("Pink");
grayRb=new JRadioButton("Grey");

blackRb.setSize(100,30);
blueRb.setSize(100,30);
orangeRb.setSize(100,30);
greenRb.setSize(100,30);
yellowRb.setSize(100,30);
redRb.setSize(100,30);
pinkRb.setSize(100,30);
grayRb.setSize(100,30);

blackRb.setLocation(200,70);
blueRb.setLocation(200,100);
orangeRb.setLocation(200,130);
greenRb.setLocation(200,160);
yellowRb.setLocation(400,70);
redRb.setLocation(400,100);
pinkRb.setLocation(400,130);
grayRb.setLocation(400,160);

blackRb.addActionListener(this);
blueRb.addActionListener(this);
orangeRb.addActionListener(this);
greenRb.addActionListener(this);
yellowRb.addActionListener(this);
redRb.addActionListener(this);
pinkRb.addActionListener(this);
grayRb.addActionListener(this);

c.add(blackRb);
c.add(blueRb);
c.add(orangeRb);
c.add(greenRb);
c.add(yellowRb);
c.add(redRb);
c.add(pinkRb);
c.add(grayRb);

colorSelect=new ButtonGroup();

colorSelect.add(blackRb);
colorSelect.add(blueRb);
colorSelect.add(orangeRb);
colorSelect.add(greenRb);
colorSelect.add(yellowRb);
colorSelect.add(redRb);
colorSelect.add(pinkRb);
colorSelect.add(grayRb);

}


public void paint(Graphics g)
{
super.paint(g);
g.setColor(currentColor);
switch (num)
{

case 0:
    g.fillRect(50,25,125,25);
    g.fillRect(50,50,25,175);
    g.fillRect(50,200,125,25);
    g.fillRect(150,25,25,175);
    break;

case 1:
    g.fillRect(75,25,75,25);
    g.fillRect(100,50,50,125);
    g.fillRect(50,175,150,25);
    break;

case 2:
    g.fillRect(50,25,125,25);
    g.fillRect(150,50,25,50);
    g.fillRect(50,100,125,25);
    g.fillRect(50,175,125,25);
    g.fillRect(50,125,25,50);
    break;

case 3:
    g.fillRect(150,50,25,175);
    g.fillRect(50,50,100,25);
    g.fillRect(50,125,100,25);
    g.fillRect(50,200,100,25);
    break;

case 4:
    g.fillRect(50,25,25,75);
    g.fillRect(50,100,100,25);
    g.fillRect(150,25,25,175);
    break;

case 5:
    g.fillRect(50,25,125,25);
    g.fillRect(50,50,25,50);
    g.fillRect(50,100,125,25);
    g.fillRect(50,175,125,25);
    g.fillRect(150,125,25,50);
    break;

case 6:
    g.fillRect(50,25,125,25);
    g.fillRect(50,50,25,50);
    g.fillRect(50,100,125,25);
    g.fillRect(50,175,125,25);
    g.fillRect(150,125,25,50);
    g.fillRect(50,125,25,50);
    break;

case 7:
    g.fillRect(50,25,125,25);
    g.fillRect(150,50,25,150);
    break;

case 8:
    g.fillRect(50,25,125,25);
    g.fillRect(50,50,25,50);
    g.fillRect(50,100,125,25);
    g.fillRect(50,175,125,25);
    g.fillRect(150,125,25,50);
    g.fillRect(50,125,25,50);
    g.fillRect(150,50,25,50);
    break;

case 9:
default:
    g.fillRect(50,25,125,25);
    g.fillRect(50,50,25,50);
    g.fillRect(50,100,125,25);
    g.fillRect(50,175,125,25);
    g.fillRect(150,125,25,50);
    g.fillRect(150,50,25,50);
    break;
 }
}

public void itemStateChanged(ItemEvent e) 
{
if(e.getSource()==blackRb)
    currentColor=Color.black;
else if(e.getSource()==blueRb)
    currentColor=Color.blue;
else if(e.getSource()==orangeRb)
    currentColor=Color.orange;
else if(e.getSource()==greenRb)
    currentColor=Color.green;
else if(e.getSource()==yellowRb)
    currentColor=Color.yellow;
else if(e.getSource()==redRb)
    currentColor=Color.red;
else if(e.getSource()==pinkRb)
    currentColor=Color.pink;
else if(e.getSource()==grayRb)
    currentColor=Color.gray;
repaint();
}

public void actionPerformed(ActionEvent e) {
    if(e.getActionCommand().equals("Ok"))
            num=Integer.parseInt(numberTF.getText());
    repaint();

}

}

2 个答案:

答案 0 :(得分:1)

您的okB jbutton变量永远不会初始化。

答案 1 :(得分:-1)

这是您修改后的工作代码,找出丢失的内容。

//<applet width="500" height="300" code="ColorChange"></applet>

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;


public class ColorChange extends JApplet implements ItemListener, ActionListener {

    private JRadioButton blackRb, blueRb, orangeRb, greenRb, yellowRb, redRb, pinkRb, grayRb;
    private Color currentColor=Color.magenta;   
    private ButtonGroup colorSelect;    
    private JTextField numberTF;
    private JLabel label;
    private JButton okB;
    private int num=0;

    public void init() {

        Container c=getContentPane();
        c.setLayout(null);
        setSize(500,300);

        label=new JLabel("Enter number");
        label.setLocation(200,40);
        label.setSize(100,30);

        numberTF=new JTextField(5);
        numberTF.setLocation(300,40);
        numberTF.setSize(50,30);

        okB = new JButton("Ok");
        okB.setLocation(400,40);
        okB.setSize(80,30);
        okB.addActionListener(this);

        c.add(label);
        c.add(numberTF);
        c.add(okB);

        blackRb=new JRadioButton("Black");
        blueRb=new JRadioButton("Blue");
        orangeRb=new JRadioButton("Orange");
        greenRb=new JRadioButton("Green");
        yellowRb=new JRadioButton("Yellow");
        redRb=new JRadioButton("Red");
        pinkRb=new JRadioButton("Pink");
        grayRb=new JRadioButton("Grey");

        blackRb.setSize(100,30);
        blueRb.setSize(100,30);
        orangeRb.setSize(100,30);
        greenRb.setSize(100,30);
        yellowRb.setSize(100,30);
        redRb.setSize(100,30);
        pinkRb.setSize(100,30);
        grayRb.setSize(100,30);

        blackRb.setLocation(200,70);
        blueRb.setLocation(200,100);
        orangeRb.setLocation(200,130);
        greenRb.setLocation(200,160);
        yellowRb.setLocation(400,70);
        redRb.setLocation(400,100);
        pinkRb.setLocation(400,130);
        grayRb.setLocation(400,160);

        blackRb.addItemListener(this);
        blueRb.addItemListener(this);
        orangeRb.addItemListener(this);
        greenRb.addItemListener(this);
        yellowRb.addItemListener(this);
        redRb.addItemListener(this);
        pinkRb.addItemListener(this);
        grayRb.addItemListener(this);

        c.add(blackRb);
        c.add(blueRb);
        c.add(orangeRb);
        c.add(greenRb);
        c.add(yellowRb);
        c.add(redRb);
        c.add(pinkRb);
        c.add(grayRb);

        colorSelect=new ButtonGroup();

        colorSelect.add(blackRb);
        colorSelect.add(blueRb);
        colorSelect.add(orangeRb);
        colorSelect.add(greenRb);
        colorSelect.add(yellowRb);
        colorSelect.add(redRb);
        colorSelect.add(pinkRb);
        colorSelect.add(grayRb);

    }


    public void paint(Graphics g){
        super.paint(g);
        g.setColor(currentColor);

        switch (num) {
            case 0:
                g.fillRect(50,25,125,25);
                g.fillRect(50,50,25,175);
                g.fillRect(50,200,125,25);
                g.fillRect(150,25,25,175);
                break;

            case 1:
                g.fillRect(75,25,75,25);
                g.fillRect(100,50,50,125);
                g.fillRect(50,175,150,25);
                break;

            case 2:
                g.fillRect(50,25,125,25);
                g.fillRect(150,50,25,50);
                g.fillRect(50,100,125,25);
                g.fillRect(50,175,125,25);
                g.fillRect(50,125,25,50);
                break;

            case 3:
                g.fillRect(150,50,25,175);
                g.fillRect(50,50,100,25);
                g.fillRect(50,125,100,25);
                g.fillRect(50,200,100,25);
                break;

            case 4:
                g.fillRect(50,25,25,75);
                g.fillRect(50,100,100,25);
                g.fillRect(150,25,25,175);
                break;

            case 5:
                g.fillRect(50,25,125,25);
                g.fillRect(50,50,25,50);
                g.fillRect(50,100,125,25);
                g.fillRect(50,175,125,25);
                g.fillRect(150,125,25,50);
                break;

            case 6:
                g.fillRect(50,25,125,25);
                g.fillRect(50,50,25,50);
                g.fillRect(50,100,125,25);
                g.fillRect(50,175,125,25);
                g.fillRect(150,125,25,50);
                g.fillRect(50,125,25,50);
                break;

            case 7:
                g.fillRect(50,25,125,25);
                g.fillRect(150,50,25,150);
                break;

            case 8:
                g.fillRect(50,25,125,25);
                g.fillRect(50,50,25,50);
                g.fillRect(50,100,125,25);
                g.fillRect(50,175,125,25);
                g.fillRect(150,125,25,50);
                g.fillRect(50,125,25,50);
                g.fillRect(150,50,25,50);
                break;

            case 9:
                default:
                g.fillRect(50,25,125,25);
                g.fillRect(50,50,25,50);
                g.fillRect(50,100,125,25);
                g.fillRect(50,175,125,25);
                g.fillRect(150,125,25,50);
                g.fillRect(150,50,25,50);
                break;
        }
    }

    public void itemStateChanged(ItemEvent e) {        
        if(e.getSource()==blackRb)
            currentColor=Color.black;
        else if(e.getSource()==blueRb)
            currentColor=Color.blue;
        else if(e.getSource()==orangeRb)
            currentColor=Color.orange;
        else if(e.getSource()==greenRb)
            currentColor=Color.green;
        else if(e.getSource()==yellowRb)
            currentColor=Color.yellow;
        else if(e.getSource()==redRb)
            currentColor=Color.red;
        else if(e.getSource()==pinkRb)
            currentColor=Color.pink;
        else if(e.getSource()==grayRb)
            currentColor=Color.gray;
        repaint();
    }

    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("Ok"))
            num=Integer.parseInt(numberTF.getText());
        repaint();
    }
}