Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.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 - Fatal编程技术网

Java 它编译并运行,但当我按下任何按钮时,它会给我一条错误消息。谢谢

Java 它编译并运行,但当我按下任何按钮时,它会给我一条错误消息。谢谢,java,Java,当我按下一个按钮时,我会在命令提示符上看到一条错误消息。这是错误的: import javax.swing.*; import java.awt.*; import java.awt.event.*; class cwp2 { public static void main(String[] args) { MyFrame F1 = new MyFrame(); } }

当我按下一个按钮时,我会在命令提示符上看到一条错误消息。

这是错误的:

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

    class cwp2
    {
        public static void main(String[] args)
        {
            MyFrame F1 = new MyFrame();
        }
    }

    class MyFrame extends JFrame 
    {
        MyFrame()
        {
        //main window
            setSize(480,300);
        MyPanel mainpanel = new MyPanel();
            mainpanel.setLocation(300,300);
        add(mainpanel);
            setTitle("Super Screen & Keyboard");
        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        setVisible(true);
    }
    }

   class MyPanel extends JPanel implements ActionListener
    {
        private JTextArea screen;
        private JButton spacebar,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z;

        MyPanel()   
    {
        //text screen 
        JTextArea screen = new JTextArea();
        screen.setColumns(40);
        screen.setRows(9);
        screen.setWrapStyleWord(true);
        screen.setLineWrap(true);

        //buttons and actions
        spacebar = new JButton("spacebar");
        spacebar.addActionListener(this);
        a = new JButton("a");
        a.addActionListener(this);
        b = new JButton("b");
        b.addActionListener(this);
        c = new JButton("c");
        c.addActionListener(this);
        d = new JButton("d");
        d.addActionListener(this);
        e = new JButton("e");
        e.addActionListener(this);
        f = new JButton("f");
        f.addActionListener(this);
        g = new JButton("g");
        g.addActionListener(this);
        h = new JButton("h");
        h.addActionListener(this);
        i = new JButton("i");
        i.addActionListener(this);
        j = new JButton("j");
        j.addActionListener(this);
        k = new JButton("k");
        k.addActionListener(this);
        l = new JButton("l");
        l.addActionListener(this);
        m = new JButton("m");
        m.addActionListener(this);
        n = new JButton("n");
        n.addActionListener(this);
        o = new JButton("o");
        o.addActionListener(this);
        p = new JButton("p");
        p.addActionListener(this);
        q = new JButton("q");
        q.addActionListener(this);
        r = new JButton("r");
        r.addActionListener(this);
        s = new JButton("s");
        s.addActionListener(this);
        t = new JButton("t");
        t.addActionListener(this);
        u = new JButton("u");
        u.addActionListener(this);
        v = new JButton("v");
        v.addActionListener(this);
        w = new JButton("w");
        w.addActionListener(this);
        x = new JButton("x");
        x.addActionListener(this);
        y = new JButton("y");
        y.addActionListener(this);
        z = new JButton("z");
        z.addActionListener(this);

        add(q);
        add(w);
        add(e);
        add(r);
        add(t);
        add(y);
        add(u);
        add(i);
        add(o);
        add(p);
        add(a);
        add(s);
        add(d);
        add(f);
        add(g);
        add(h);
        add(j);
        add(k);
        add(l);
        add(z);
        add(x);
        add(c);
        add(v);
        add(b);
        add(n);
        add(m);
        add(spacebar);
        add(screen);
    }

    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == spacebar)
        {
            screen.append(" ");
        }

        else 
        {
            screen.append(e.getSource.getActionCommand());
        }
    }
getSource()
方法返回对象类型的对象,而对象没有
getActionCommand()
方法

这很简单

e.getSource.getActionCommand()
因为e是一个ActionEvent变量,而这个类型实际上有这个方法


建议:下次你有类似问题时,请显示所有完整的错误或异常消息,并在评论中指出哪一行抛出它


编辑

您的新问题是由于对屏幕变量进行了阴影处理

您的代码:

e.getActionCommand()
这意味着您在构造函数中初始化的屏幕变量是一个局部变量,而不是类中声明的字段

这可以通过不重新声明变量来解决:

class MyPanel extends JPanel implements ActionListener {  
    private JTextArea screen;  // screen declared here
    private JButton spacebar,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z;

    MyPanel() {
      //text screen 
      JTextArea screen = new JTextArea();  // but re-declared here!

错误消息是什么…?感谢您的帮助,但它仍然不起作用。当我按下按钮时,出现在命令提示符上的错误消息如下:线程“AWT-EventQueue-0”java.lang.NullPointerException位于MyPanel.actionPerformed(cwp2.java:141)位于javax.swing.AbstractButton.fireActionPerformed(UnknownEtc等)它不允许我复制所有错误here@user2975205:我猜这是另一条错误消息?请指出哪一行抛出了NPE。
class MyPanel extends JPanel implements ActionListener {  
    private JTextArea screen;  // screen declared here
    private JButton spacebar,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z;

    MyPanel() {
      //text screen 
      // JTextArea screen = new JTextArea();  // but re-declared here!
      screen = new JTextArea();   // note the difference?