对于orbital_app类型,方法setVisible(boolean)未定义

时间:2015-06-06 08:57:13

标签: java eclipse swing

  1. 这是代码。我的主框架是orbital_app,我希望当我点击JButton(按钮)时,数据被保存,当前窗口关闭,另一个窗口orbital_app打开。

    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. 我收到错误消息The method setVisible(boolean) is undefined for the type orbital_app。我该怎么做才能解决这个问题?在这里,我想关闭此当前帧(signup_try)并转到其他帧(orbital_app).. setVisible带有下划线红色并显示"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);
        }
    

    }

  3. Orbital_app代码(在此代码中setVisible正常工作,没有任何错误):

    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在这里工作

                    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.此处也是

                    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);
        }
    
    
    
    }
    

2 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,它是通过排除JFrame类来解决的。 您的类应该扩展JFrame,因为setVisible()方法属于JFrame类。

答案 1 :(得分:1)

您的代码似乎存在一些问题。

最重要的是:您的计划中有多个入口点 您应该只在应用程序中有一个public static void main(String[] args) ...函数,即“程序启动”。

您收到的错误消息(The method setVisible(boolean) is undefined for the type orbital_app)来自于orbital_app类没有setVisible函数,其成员之一,但是没有物质

您的orbital_app有一位JFrame的私人会员,这使您可以通过访问该框架来调用JFrames内的orbital_app方法,但是你无法从外面到达它 看来你混淆了继承和所有权。

如果您希望orbital_app课程为JFrame,则需要继承JFrame。否则,您可以实施您希望为其他课程公开的方法 或者您可以为私有JFrame对象创建一个getter,以便您可以从外部访问它。