简体   繁体   English

Java中的对象有问题吗?

[英]Issue with objects in java?

I am making a window that reacts based on user input of yes or no. 我正在根据用户输入的是或否做出反应的窗口。 I am running into an issue in this part of the code: 我在代码的这一部分遇到一个问题:

public class Mywindow {

public static void main (String [] args){
    windowcontinued object = new windowcontinued();
    object.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //ISSUE HERE
    object.setSize(700, 1024); //ISSUE HERE
    object.setVisible(true); //ISSUE HERE
                                         }
                }

These simple operations should make sure the program terminates when closed, is a certain size, and is visible. 这些简单的操作应确保程序在关闭时终止,具有一定大小且可见。 For some reason though, on all three operations I am getting errors saying: 但是由于某种原因,在这三个操作上我都收到错误消息:

 The method setDefaultCloseOperation(int) is undefined for the type windowcontinued

The method setSize(int, int) is undefined for the type windowcontinued

The method setVisible(boolean) is undefined for the type windowcontinued

Although, all of these should have already been defined after importing. 虽然,所有这些都应该在导入后已定义。 Here is the full code: 这是完整的代码:

Main code: 主要代码:

package Myguipackage;

import javax.swing.JOptionPane;
import javax.swing.JFrame;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Mywindow {

public static void main (String [] args){
    windowcontinued object = new windowcontinued();
    object.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    object.setSize(700, 1024);
    object.setVisible(true);

                                         }
                }

Other class: 其他类:

package Myguipackage;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Windowcontinued{

public JLabel text;
public JTextField textfield;

public windowcontinued(){  //Constructor
/*  super("CHAT");
    setLayout(new FlowLayout());   */   

    text = new JLabel ("Would you like to talk to me?");
        text.add(text);

    textfield = new JTextField ("Yes or no? Erase this and type in your answer!");




                   }






 class hear implements ActionListener{



    public void actionPerformed(ActionEvent event){

        String string = "";

        if(event.getSource()==textfield)
            string = String.format("You typed in %s", event.getActionCommand());

         JOptionPane.showMessageDialog(null, string); //Open a new window that displays String string, which has changed based on which field you hit enter on.




                                                  }



                                         }


                        }

After using the extends keyword as suggested by fdsa, here is the new code: 在使用了fdsa建议的extends关键字之后,下面是新代码:

package Myguipackage; 打包Myguipackage;

import javax.swing.JFrame;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class mywindow extends JFrame{

public static void main (String [] args){
    windowcontinued object = new windowcontinued();
    object.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    object.setSize(700, 1024);
    object.setVisible(true);
                                         }
                }

Class windowcontinued is still the same though. 窗口继续的类仍然相同。

As other posters said, please follow the java naming conventions when naming your classes. 正如其他张贴者所说,在命名类时,请遵循Java命名约定。 It makes your code a easier to follow. 它使您的代码更易于遵循。

You seem to be confused as to the purpose of import statement. 您似乎对import语句的目的感到困惑。 When you import an object, you can only use its methods on objects of that type. 导入对象时,只能在该类型的对象上使用其方法。 If you would like your class to mimic the functionality of an existing class, you need to extend the original class, not just import it. 如果您希望您的类模仿现有类的功能,则需要extend原始类,而不仅仅是import它。

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

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