简体   繁体   English

Java Swing中的JButton错误

[英]JButton Error in Java Swing

I am using a AppDev (App Development) and i created a JButton with JFrame with testing button with name "Button", i get this error below: 我正在使用AppDev(应用程序开发),并且使用带有测试按钮且名称为“ Button”的JFrame创建了一个JButton,在下面出现此错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at apptutorial.AppDev.initApp(AppDev.java:18)
at apptutorial.AppDev.<init>(AppDev.java:10)
at apptutorial.AppDev$1.run(AppDev.java:30)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:312)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:738)
at java.awt.EventQueue.access$300(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:699)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:708)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

and my code with file "AppDev.java" is: 我的文件“ AppDev.java”的代码是:

package apptutorial;
import java.awt.*;
import javax.swing.*;

public class AppDev extends JFrame {

    public AppDev() {
        initApp();
    }
    /*
    This Generated App Code generates a app which has running now.
    */

    // <editor-fold defaultstate="collapsed" desc="Generated App Code">
    private void initApp() {
        frame.setTitle("Alpha Application");
        frame.setLayout(new FlowLayout());
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.add(new JButton("Button"));
        frame.setSize(400,350);
        frame.setVisible(true);
    }
    // </editor-fold>

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AppDev().setVisible(true);
            }
        });
    }
    private JFrame frame;
}

can you anyone fix me to resolve error. 有人可以解决我的问题吗?

You haven't initialized the varaible frame 您尚未初始化可变框架

 private void initApp() {
   frame=new JFrame();//you missed this
    frame.setTitle("Alpha Application");
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.add(new JButton("Button"));
    frame.setSize(400,350);
    frame.setVisible(true);
}

Idk why you are creating a frame inside frame IDK为什么您要在框架内创建框架

But there is no need for that as you already extends the JFrame by 但是没有必要这样做,因为您已经通过以下方式扩展了JFrame:

public class AppDev extends JFrame

You can simply do like 你可以简单地喜欢

 private void initApp() {

    setTitle("Alpha Application");
    setLayout(new FlowLayout());
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    add(new JButton("Button"));
    setSize(400,350);
    setVisible(true);
}

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

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