Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.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 CardLayout gui错误eclipse_Java_Eclipse_Swing_Layout Manager_Cardlayout - Fatal编程技术网

Java CardLayout gui错误eclipse

Java CardLayout gui错误eclipse,java,eclipse,swing,layout-manager,cardlayout,Java,Eclipse,Swing,Layout Manager,Cardlayout,我正在使用Eclipse和WindowBuilder。但是,我无法在WindowBuilder中使用cardlayout。所以我开始输入我自己的代码,现在我只能显示正确显示的第一张卡,但在jmenubar中单击jmenuitem时,第二张卡或jpanel不会显示 import java.awt.BorderLayout; import java.awt.CardLayout; import java.awt.Container; import java.awt.EventQueue; impor

我正在使用Eclipse和WindowBuilder。但是,我无法在WindowBuilder中使用cardlayout。所以我开始输入我自己的代码,现在我只能显示正确显示的第一张卡,但在jmenubar中单击jmenuitem时,第二张卡或jpanel不会显示

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JTextField;


public class Dashboard extends JFrame {

    private JPanel contentPane;

    private JTextField textFieldName, sitextFieldName;
    private JTextField textFieldRollNo, sitextFieldRollNo;
    private JTextField textFieldPhoneNo, sitextFieldPhoneNo;
    Connection connection = null;
    PreparedStatement ps = null;
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Dashboard frame = new Dashboard();
                    frame.addComponentToFrame(frame.getContentPane());
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    protected void addComponentToFrame(Container containerPane) {
        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        JMenu mnStudent = new JMenu("Student");
        menuBar.add(mnStudent);

        JMenuItem mntmStudentRegistration = new JMenuItem("Student Registration");
        mnStudent.add(mntmStudentRegistration);

        mntmStudentRegistration.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                CardLayout cl = (CardLayout)(contentPane.getLayout());
                cl.show(contentPane, (String)e.getActionCommand());
            }
        });

        JMenuItem mntmStudentInformation = new JMenuItem("Student Information");
        mnStudent.add(mntmStudentInformation);

        JMenu mnEmployee = new JMenu("Employee");
        menuBar.add(mnEmployee);

        JMenuItem mntmEmployeeRegistration = new JMenuItem("Employee Registration");
        mnEmployee.add(mntmEmployeeRegistration);

        JMenuItem mntmEmployeeInformation = new JMenuItem("Employee Information");
        mnEmployee.add(mntmEmployeeInformation);

        //StudentRegistration items starts here
        JPanel jpStudentRegistration = new JPanel(new GridLayout(0,2));

        JLabel lblName = new JLabel("Name");
        lblName.setBounds(50, 50, 47, 25);
        jpStudentRegistration.add(lblName);

        textFieldName = new JTextField();
        textFieldName.setColumns(10);
        textFieldName.setBounds(127, 50, 100, 20);
        jpStudentRegistration.add(textFieldName);

        JLabel lblRollNo = new JLabel("RollNo");
        lblRollNo.setBounds(50, 81, 47, 25);
        jpStudentRegistration.add(lblRollNo);

        textFieldRollNo = new JTextField();
        textFieldRollNo.setColumns(10);
        textFieldRollNo.setBounds(127, 81, 100, 20);
        jpStudentRegistration.add(textFieldRollNo);

        JLabel lblPhoneNo = new JLabel("PhoneNo");
        lblPhoneNo.setBounds(50, 112, 47, 25);
        jpStudentRegistration.add(lblPhoneNo);

        textFieldPhoneNo = new JTextField();
        textFieldPhoneNo.setColumns(10);
        textFieldPhoneNo.setBounds(127, 112, 100, 20);
        jpStudentRegistration.add(textFieldPhoneNo);

        JButton btnSubmit = new JButton("Submit");
        btnSubmit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try{
                    Class.forName("com.mysql.jdbc.Driver");
                    connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/studentinformationsystem","root","");
                    String query = "insert into student(Name, RollNo, PhoneNo) values (?,?,?)";
                    PreparedStatement ps = connection.prepareStatement(query);
                    ps.setString(1, textFieldName.getText());
                    ps.setString(2, textFieldRollNo.getText());
                    ps.setString(3, textFieldPhoneNo.getText());
                    ps.execute();

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

                    ps.close();
                } catch(Exception e1){
                    System.out.println(e1.getMessage());
                }
            }
        });
        btnSubmit.setBounds(50, 148, 89, 23);
        jpStudentRegistration.add(btnSubmit);
        //StudentRegistration items ends here

        //StudentInformation items starts here
        JPanel jpStudentInformation = new JPanel(new GridLayout(0,2));

        JLabel silblName = new JLabel("Name");
        silblName.setBounds(50, 50, 47, 25);
        jpStudentInformation.add(silblName);

        sitextFieldName = new JTextField();
        sitextFieldName.setColumns(10);
        sitextFieldName.setBounds(127, 50, 100, 20);
        jpStudentInformation.add(sitextFieldName);

        JLabel silblRollNo = new JLabel("RollNo");
        silblRollNo.setBounds(50, 81, 47, 25);
        jpStudentInformation.add(silblRollNo);

        sitextFieldRollNo = new JTextField();
        sitextFieldRollNo.setColumns(10);
        sitextFieldRollNo.setBounds(127, 81, 100, 20);
        jpStudentInformation.add(sitextFieldRollNo);

        JLabel silblPhoneNo = new JLabel("PhoneNo");
        silblPhoneNo.setBounds(50, 112, 47, 25);
        jpStudentInformation.add(silblPhoneNo);

        sitextFieldPhoneNo = new JTextField();
        sitextFieldPhoneNo.setColumns(10);
        sitextFieldPhoneNo.setBounds(127, 112, 100, 20);
        jpStudentInformation.add(sitextFieldPhoneNo);

        JButton btnRefresh = new JButton("Refresh");
        btnRefresh.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try{
                    Class.forName("com.mysql.jdbc.Driver");
                    connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/studentinformationsystem","root","");
                    String query = "select * from student";
                    PreparedStatement ps = connection.prepareStatement(query);
                    //ps.setString(1, textFieldName.getText());
                    //ps.setString(2, textFieldRollNo.getText());
                    //ps.setString(3, textFieldPhoneNo.getText());
                    ps.execute();

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

                    ps.close();
                } catch(Exception e1){
                    System.out.println(e1.getMessage());
                }
            }
        });
        btnRefresh.setBounds(50, 148, 89, 23);
        jpStudentInformation.add(btnRefresh);
        //StudentInformation items ends here

        contentPane = new JPanel(new CardLayout());
        JPanel jpanel = new JPanel(new GridLayout(0,2));
        contentPane.add(jpanel, "");
        contentPane.add(jpStudentRegistration, "Student Registration");
        contentPane.add(jpStudentInformation, "Student Information");

        containerPane.add(contentPane, BorderLayout.PAGE_START);
    }

    /**
     * Create the frame.
     */
    public Dashboard() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
    }

}

您尚未将
ActionListener
添加到
mntmStudentInformation

    mntmStudentRegistration.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            CardLayout cl = (CardLayout) (contentPane.getLayout());
            cl.show(contentPane, (String) e.getActionCommand());
        }
    });

    JMenuItem mntmStudentInformation = new JMenuItem("Student Information");
    mntmStudentInformation.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            CardLayout cl = (CardLayout) (contentPane.getLayout());
            cl.show(contentPane, (String) e.getActionCommand());
        }
    });

添加此代码效果很好

import java.sql.Connection。。。司机经理。。。编制报表布局问题不需要这些。如果需要,请硬编码一些数据,但为了更快地获得更好的帮助,请发布一个(最小完整的可验证示例)或(简短、自包含、正确的示例)。没有
ActionListener
附加到
mntmStudentInformation
…问题是数据库工作正常,这就是为什么我没有发布它的原因。如此愚蠢的错误:P。再次感谢。。。
mntmStudentInformation.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                CardLayout cl = (CardLayout)(contentPane.getLayout());
                cl.show(contentPane, (String)e.getActionCommand());
            }
        });