Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.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 为什么我们必须导入并扩展JFrame,为什么我们不能通过导入来使用它?_Java_Swing_Inheritance_Import - Fatal编程技术网

Java 为什么我们必须导入并扩展JFrame,为什么我们不能通过导入来使用它?

Java 为什么我们必须导入并扩展JFrame,为什么我们不能通过导入来使用它?,java,swing,inheritance,import,Java,Swing,Inheritance,Import,在下面的代码中,先导入JFrame,然后对其进行扩展。在已经导入后扩展它的原因是什么?为什么我们不能直接使用它来导入它,就像扫描仪一样 import java.awt.FlowLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JFrame; import javax.swing.JButton; im

在下面的代码中,先导入JFrame,然后对其进行扩展。在已经导入后扩展它的原因是什么?为什么我们不能直接使用它来导入它,就像扫描仪一样

    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JOptionPane;

    public class tuna extends JFrame{

        private JButton reg;
        private JButton custom;

        public tuna(){
            super("The Title");
            setLayout(new FlowLayout());            

            reg = new JButton("reg Button");
            add(reg);

            Icon x = new ImageIcon(getClass().getResource("xxx.png"));
            Icon y = new ImageIcon(getClass().getResource("yyy.png"));

            custom = new JButton("custom button" , x);
            custom.setRolloverIcon(y);
            add(custom);

            handler thehandler = new handler();
            reg.addActionListener(thehandler);
            custom.addActionListener(thehandler);
        }

        public class handler implements ActionListener{

            public void actionPerformed(ActionEvent event){

                JOptionPane.showMessageDialog(null, String.format("%s", event.getSource()));

            }
        }
}

提前谢谢

导入一个类,就是使您可以使用该类,而无需限定您正在编写的当前类中的全名

扩展一个类就是创建一个新类,它是其他类的子类。这将使您的子类继承超类的属性和方法


顺便说一句,您不需要扩展JFrame。

这里您基于JFrame对象定义tuna类。您需要首先导入JFrame类,否则JVM将不知道什么是JFrame,因此无法创建tuna类

您应该了解“导入”在Java中的含义


顺便说一下,类的第一个字母应该始终是大写字母。金枪鱼应该被称为金枪鱼。

不要扩展JFrame,将此对象创建为本地变量您不必扩展
JFrame。
您的问题基于一个错误的前提。我在试图找到它时读到了同样的内容。。但是当我们导入它时会发生什么呢?我知道不是编写java.util.Scanner sc=new java.util.Scanner(System.in);,您只需编写Scanner sc=newscanner(System.in);通过导入我们可以使用哪些函数?当您导入一个包或一个特定的类时,您可以实例化(如果可能)同一个类并访问其公共方法感谢您的帮助!谢谢你的提示和帮助!