我想创建一个面板类并在MyFrame类上插入。我有几个要插入的面板类。现在的问题是框架类没有显示面板。
这是我的Frame类。
public class MyFrame extends JFrame{
private JLabel jLabel1 = new JLabel();
private JLabel jLabel2 = new JLabel();
private JLabel jLabel3 = new JLabel();
private JLabel jLabel4 = new JLabel();
private JLabel jLabel5 = new JLabel();
private JLabel jLabel6 = new JLabel();
private JLabel jLabel7 = new JLabel();
private JLabel jLabel8 = new JLabel();
private JLabel jLabel9 = new JLabel();
private JTextField FirstName = new JTextField();
private JTextField Surname = new JTextField();
private JTextField SurnameAMarriage = new JTextField();
private JTextField Gender = new JTextField();
private JTextField StreetNumber = new JTextField();
private JTextField StreetName = new JTextField();
private JTextField Suburb = new JTextField();
private JScrollPane jScrollPane1 = new JScrollPane();
private JTextArea Remark = new JTextArea();
private JRadioButton GMale = new JRadioButton();
private JRadioButton GFemale = new JRadioButton();
private JButton BtAddSpouse = new JButton();
private JButton BtAddMother = new JButton();
private JButton BtAddFather = new JButton();
private JButton BtAddChildren = new JButton();
private JButton BtNewFamily = new JButton();
private JButton BtEditingMode = new JButton();
private JButton BtViewingMode = new JButton();
private JButton BtSubmit = new JButton();
private JButton BtReset = new JButton();
private JButton BtBack = new JButton();
ArrayList <Person> Family = new ArrayList<Person>();
int i = 0;
AddNew Addnew = new AddNew();
public MyFrame(){
CardLayout C1 = new CardLayout();
ViewingMode VM = new ViewingMode();
JPanel panelCont = new JPanel();
panelCont.setLayout(C1);
add(VM);
C1.show(panelCont, "1");
JFrame Frame = new JFrame();
Frame.add(panelCont);
Frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Frame.pack();
Frame.setVisible(true);
}
这将是我的Viewing类。
public class ViewingMode extends JPanel{
private class ActionNewFamily implements ActionListener{
public void actionPerformed(ActionEvent event){
Family.clear();
Addnew.setVisible(true);
}
}
private class ActionViewingMode implements ActionListener{
public void actionPerformed(ActionEvent event){
}
}
private class ActionAddFather implements ActionListener{
public void actionPerformed(ActionEvent event){
}
}public ViewingMode(){
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();
JLabel jLabel6 = new JLabel();
JLabel jLabel7 = new JLabel();
JLabel jLabel8 = new JLabel();
JLabel jLabel9 = new JLabel();
JTextField FirstName = new JTextField();
JTextField Surname = new JTextField();
JTextField SurnameAMarriage = new JTextField();
JTextField Gender = new JTextField();
JTextField StreetNumber = new JTextField();
JTextField StreetName = new JTextField();
JTextField Suburb = new JTextField();
JScrollPane jScrollPane1 = new JScrollPane();
JTextArea Remark = new JTextArea();
JRadioButton GMale = new JRadioButton();
JRadioButton GFemale = new JRadioButton();
JButton BtAddSpouse = new JButton();
JButton BtAddMother = new JButton();
JButton BtAddFather = new JButton();
JButton BtAddChildren = new JButton();
JButton BtNewFamily = new JButton();
JButton BtEditingMode = new JButton();
JButton BtViewingMode = new JButton();
JButton BtSubmit = new JButton();
JButton BtReset = new JButton();
JButton BtViewFamily = new JButton();
JButton BtBack = new JButton();
JScrollPane jScrollPane2 = new JScrollPane();
JList<String> jList1 = new JList<>();
jLabel1.setText("First Name");
jLabel2.setText("Surname");
jLabel3.setText("Surname after marriage");
jLabel4.setText("Gender");
jLabel5.setText("Street Number");
jLabel6.setText("Remark");
SurnameAMarriage.setToolTipText("");
Remark.setColumns(20);
Remark.setRows(5);
jScrollPane1.setViewportView(Remark);
FirstName.setEditable(false);
Surname.setEditable(false);
SurnameAMarriage.setEditable(false);
StreetNumber.setEditable(false);
Remark.setEditable(false);
StreetName.setEditable(false);
Suburb.setEditable(false);
Gender.setEditable(false);
jLabel7.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
jLabel7.setText("Viewing mode");
BtViewFamily.setText("View Family");
BtEditingMode.setText("Editing Mode");
BtEditingMode.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event){
EditingMode EDM = new EditingMode();
EDM.setVisible(true);
}
});
jLabel8.setText("Street Name");
jLabel9.setText("Suburb");
jList1.setModel(new AbstractListModel<String>() {
String[] strings = { "" };
public int getSize() { return strings.length; }
public String getElementAt(int i) { return strings[i]; }
});
jScrollPane2.setViewportView(jList1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(59, 59, 59)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel6)
.addGap(129, 129, 129)
.addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, 265, GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(jLabel8)
.addComponent(jLabel9))
.addGap(101, 101, 101)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(StreetName, GroupLayout.PREFERRED_SIZE, 265, GroupLayout.PREFERRED_SIZE)
.addComponent(Suburb, GroupLayout.Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 265, GroupLayout.PREFERRED_SIZE))))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jLabel3)
.addComponent(jLabel4)
.addComponent(jLabel5))
.addGap(34, 34, 34)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
.addComponent(FirstName, GroupLayout.DEFAULT_SIZE, 265, Short.MAX_VALUE)
.addComponent(Surname)
.addComponent(SurnameAMarriage)
.addComponent(Gender)
.addComponent(StreetNumber)
.addGroup(layout.createSequentialGroup()
.addGap(37, 37, 37)
.addComponent(jLabel7)))))
.addGap(83, 83, 83)
.addComponent(jScrollPane2, GroupLayout.PREFERRED_SIZE, 171, GroupLayout.PREFERRED_SIZE)
.addContainerGap(91, Short.MAX_VALUE))
.addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(253, 253, 253)
.addComponent(BtEditingMode)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(BtViewFamily)
.addGap(123, 123, 123))
);
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel7)
.addGap(20, 20, 20)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(FirstName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(Surname, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(SurnameAMarriage, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(jLabel4)
.addComponent(Gender, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(jLabel5)
.addComponent(StreetNumber, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(jLabel8)
.addComponent(StreetName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(jLabel9)
.addComponent(Suburb, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(jLabel6)
.addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
.addComponent(jScrollPane2, GroupLayout.Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 289, GroupLayout.PREFERRED_SIZE))
.addGap(31, 31, 31)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(BtViewFamily)
.addComponent(BtEditingMode))
.addContainerGap(139, Short.MAX_VALUE))
);
}
}