Ruchir Baronia
Ruchir Baronia

Reputation: 7571

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:

package 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.

Upvotes: 1

Views: 185

Answers (1)

fdsa
fdsa

Reputation: 1409

As other posters said, please follow the java naming conventions when naming your classes. It makes your code a easier to follow.

You seem to be confused as to the purpose of import statement. 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.

Upvotes: 2

Related Questions