当前位置: 动力学知识库 > 问答 > 编程问答 >

java - The method setVisible(boolean) is undefined for the type orbital_app

问题描述:

  1. This is the code. My main frame is orbital_app and I would like it to when I click on JButton (button), data is being saved, the current window closes and another window orbital_app opens.

    public class signup_try {

    private JFrame frame;

    private JTextField txtname;

    private JTextField textusername;

    private JTextField txtpass;

    private JTextField textmail;

    /**

    * Launch the application.

    */

    public static void main(String[] args) {

    EventQueue.invokeLater(new Runnable() {

    public void run() {

    try {

    signup_try window = new signup_try();

    window.frame.setVisible(true);

    } catch (Exception e) {

    e.printStackTrace();

    }

    }

    });

    }

    /**

    * Create the application.

    */

    public signup_try() {

    initialize();

    }

    Connection connection=null;

    /**

    * Initialize the contents of the frame.

    */

    private void initialize() {

    connection=dbase.dBase();

    frame = new JFrame();

    frame.setBounds(100, 100, 450, 300);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.getContentPane().setLayout(null);

    JLabel label = new JLabel("Orbital");

    label.setForeground(SystemColor.activeCaption);

    label.setFont(new Font("Arial Black", Font.BOLD, 17));

    label.setBounds(170, 11, 71, 25);

    frame.getContentPane().add(label);

    JLabel label_1 = new JLabel("Name:");

    label_1.setForeground(SystemColor.activeCaption);

    label_1.setFont(new Font("Arial Black", Font.PLAIN, 12));

    label_1.setBounds(21, 66, 46, 14);

    frame.getContentPane().add(label_1);

    txtname = new JTextField();

    txtname.setColumns(10);

    txtname.setBounds(102, 63, 200, 22);

    frame.getContentPane().add(txtname);

    textusername = new JTextField();

    textusername.setColumns(10);

    textusername.setBounds(102, 108, 200, 22);

    frame.getContentPane().add(textusername);

    txtpass = new JTextField();

    txtpass.setColumns(10);

    txtpass.setBounds(102, 150, 200, 22);

    frame.getContentPane().add(txtpass);

    textmail = new JTextField();

    textmail.setColumns(10);

    textmail.setBounds(102, 192, 200, 22);

    frame.getContentPane().add(textmail);

    JLabel label_2 = new JLabel("Username:");

    label_2.setForeground(SystemColor.activeCaption);

    label_2.setFont(new Font("Arial Black", Font.PLAIN, 12));

    label_2.setBounds(21, 111, 71, 14);

    frame.getContentPane().add(label_2);

    JLabel label_3 = new JLabel("Password:");

    label_3.setForeground(SystemColor.activeCaption);

    label_3.setFont(new Font("Arial Black", Font.PLAIN, 12));

    label_3.setBounds(21, 154, 71, 14);

    frame.getContentPane().add(label_3);

    JLabel label_4 = new JLabel("Email:");

    label_4.setForeground(SystemColor.activeCaption);

    label_4.setFont(new Font("Arial Black", Font.PLAIN, 12));

    label_4.setBounds(21, 196, 46, 14);

    frame.getContentPane().add(label_4);

    JButton button = new JButton("Sign Up");

    button.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent arg0) {

    try {

    String query="insert into Users(Name, Username, Password, Email) values(?,?,?,?)";

    PreparedStatement prepstat=connection.prepareStatement(query);

    prepstat.setString(1, txtname.getText());

    prepstat.setString(2, textusername.getText());

    prepstat.setString(3, txtpass.getText());

    prepstat.setString(4, textmail.getText());

    prepstat.execute();

    JOptionPane.showMessageDialog(null, "Data saved");

    prepstat.close();

    } catch (SQLException e) {

    JOptionPane.showMessageDialog(null, e);

    }

  2. I get the error saying that The method setVisible(boolean) is undefined for the type orbital_app. what should I do to fix this? Here I want to close this present frame(signup_try) and go to other frame (orbital_app).. setVisible is underlined red and says "The method setVisible(boolean) is undefined for the type orbital_app".

     frame.dispose();

    orbital_app orb=new orbital_app();

    orb.setVisible(true);

    }

    });

    button.setBounds(170, 239, 91, 23);

    frame.getContentPane().add(button);

    }

    }

Orbital_app code(in this code setVisible works properly without any error):

package project;

import java.awt.EventQueue;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.SwingConstants;

import java.awt.Font;

import java.awt.SystemColor;

import javax.swing.JButton;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.Color;

import javax.swing.UIManager;

public class orbital_app{

private JFrame frame;

/**

* Launch the application.

*/

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

orbital_app window = new orbital_app();

window.frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

/**

* Create the application.

*/

public orbital_app() {

initialize();

}

/**

* Initialize the contents of the frame.

*/

private void initialize() {

frame = new JFrame();

frame.getContentPane().setBackground(UIManager.getColor("Button.background"));

frame.setResizable(false);

frame.setBounds(100, 100, 450, 260);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().setLayout(null);

JLabel toplbl = new JLabel("Orbital");

toplbl.setForeground(SystemColor.activeCaption);

toplbl.setFont(new Font("Arial Black", Font.BOLD, 17));

toplbl.setVerticalAlignment(SwingConstants.TOP);

toplbl.setBounds(182, 11, 71, 25);

frame.getContentPane().add(toplbl);

JLabel infolbl = new JLabel("Multipurpose app == orbital 1.0\r\n");

infolbl.setFont(new Font("Arial", Font.PLAIN, 11));

infolbl.setForeground(SystemColor.activeCaption);

infolbl.setBounds(138, 47, 165, 25);

frame.getContentPane().add(infolbl);

JButton signup_btn = new JButton("Sign Up");

signup_btn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

frame.dispose();

signup_form sup_for=new signup_form();

3.setVisible works here

 sup_for.setVisible(true);

}

});

signup_btn.setFont(new Font("Arial", Font.PLAIN, 11));

signup_btn.setForeground(SystemColor.activeCaption);

signup_btn.setBounds(61, 133, 91, 23);

frame.getContentPane().add(signup_btn);

JButton signin_btn = new JButton("Sign In");

signin_btn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

frame.dispose();

signin_form log_for=new signin_form();

4.and here too

 log_for.setVisible(true);

}

});

signin_btn.setFont(new Font("Arial", Font.PLAIN, 11));

signin_btn.setForeground(SystemColor.activeCaption);

signin_btn.setBounds(285, 133, 91, 23);

frame.getContentPane().add(signin_btn);

JLabel notelbl = new JLabel("note: click Sign Up for new account or Sign In for existing account.");

notelbl.setHorizontalAlignment(SwingConstants.CENTER);

notelbl.setBounds(10, 199, 406, 25);

frame.getContentPane().add(notelbl);

}

}

网友答案:

There seems to be a few issues with your code.

Most importantly: You have multiple entry points in your program.
You should only have one public static void main(String[] args) ... function in your application, that is where the 'program starts'.

The error message you get (The method setVisible(boolean) is undefined for the type orbital_app) comes from the fact that the class orbital_app don´t have a setVisible function, one of its members does, but that doesn't matter.

Your orbital_app have a private member that is a JFrame, which makes it possible for you to call the JFrames methods from inside the orbital_app by accessing the frame, but you cant reach it from outside.
It seems you have mixed up inheritance and ownership.

If you wish your orbital_app class to be a JFrame, you need to inherit from the JFrame. Else you could just implement the methods you wish to make public for your other classes.
Or you could just create a getter for the private JFrame object so that you can access it from outside.

网友答案:

I faced the same problem, it is solved by exteding the JFrame. Your class should extends JFrame because setVisible() method belongs to JFrame.

分享给朋友:
您可能感兴趣的文章:
随机阅读: