gpt4 book ai didi

java - CardLayout gui错误eclipse

转载 作者:行者123 更新时间:2023-12-02 04:50:22 24 4
gpt4 key购买 nike

我正在使用 Eclipse 和 Window Builder。但是我无法在 Window Builder 中使用卡片布局。所以我开始输入自己的代码,现在我卡在显示第一张卡上,该卡显示正确,但在单击 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());
}
});

关于java - CardLayout gui错误eclipse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29294722/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com