我在Eclipse Builder中使用Eclipse。但是我无法在Window Builder中使用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);
}
}
答案 0 :(得分:2)
您尚未向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());
}
});
答案 1 :(得分:0)
mntmStudentInformation.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
CardLayout cl = (CardLayout)(contentPane.getLayout());
cl.show(contentPane, (String)e.getActionCommand());
}
});
添加此代码效果很好