GUI部件未显示



GUI的某些部分不会显示。

public  class HospitalRecord extends JFrame implements ActionListener, MouseListener, ItemListener{
    BorderLayout bl;
    FlowLayout fl;
    GridLayout gl;
    JPanel jpSouth;
    JPanel jpCenter;
    JPanel jpNorth; 
    JLabel lblName;
    JLabel lblSurname;
    JLabel lblPhone;
    JLabel lblHeight;
    JLabel lblBirthYear;
    JLabel lblDepartment;
    JTextField txtName;
    JTextField txtSurname;
    JTextField txtPhone;
    JTextField txtHeight;

    JRadioButton rbEmergency;
    JRadioButton rbControl;
    JRadioButton rbOperation;
    JCheckBox cbocigaretteusage;
    JCheckBox cboalcoholusage;
    JCheckBox cbodrugusage;
    JComboBox jcAgeEntries;
    JButton jbSubmit;
    JButton jbPrint;
    JList jLdepartment;

    public HospitalRecord() {
        super("Registration Form");
        bl=new BorderLayout();
        setLayout(bl);
        addMouseListener(this);
        setFocusable(true);
        jpSouth = new JPanel();
        add(jpSouth,BorderLayout.SOUTH);
        south();

        jpCenter = new JPanel();
        add(jpCenter,BorderLayout.CENTER);
        center();

        jpNorth = new JPanel();
        add(jpNorth,BorderLayout.NORTH);
        north();

        txtName.addActionListener(this);
    }
    public void north(){
         lblBirthYear = new JLabel("Birth Year");
          Integer[] comboData = new Integer[114];
            for (int i = 0; i < comboData.length; i++) {
                comboData[i] = 1900 + i;
            }
         jcAgeEntries = new JComboBox(comboData);
         jcAgeEntries.addMouseListener(this);
         String[] listData = new String[3];
            listData[0] = "Accident and emergency";
            listData[1] = "Ear nose and throat (ENT)";
            listData[2] = "General surgery";
            lblDepartment = new JLabel("Department Type");
            jLdepartment= new JList(listData);
            jLdepartment.addMouseListener(this);

         jpNorth.add(lblBirthYear);
         jpNorth.add(jcAgeEntries);
         jpNorth.add(lblDepartment);
         jpNorth.add(jLdepartment);

    }

    public void center(){
        gl = new GridLayout(3,3);
        jpCenter.setLayout(gl);
        lblName = new JLabel("Name");
        lblSurname = new JLabel("Surname");
        lblPhone = new JLabel("Phone");
        lblHeight = new JLabel("Height");
        txtName = new JTextField("Name");
        txtSurname = new JTextField("Surname");
        txtPhone = new JTextField("Phone");
        txtHeight = new JTextField("Height");
        txtName.addMouseListener(this);
        txtSurname.addMouseListener(this);
        txtPhone.addMouseListener(this);
        txtHeight.addMouseListener(this);
         rbEmergency = new JRadioButton("Emergency");
         rbControl = new JRadioButton("Control");
         rbOperation = new JRadioButton("Operation");
         add(rbEmergency);
         add(rbControl);
         add(rbOperation);

         cbocigaretteusage = new JCheckBox("Cigarette Usage");
         cboalcoholusage = new JCheckBox("Alcohol Usage");
         cbodrugusage = new JCheckBox("Drug Usage");
         add(cbocigaretteusage);
         add(cboalcoholusage);
         add(cbodrugusage);

        jpCenter.add(lblName);
        jpCenter.add(txtName);
        jpCenter.add(lblSurname);
        jpCenter.add(txtSurname);
        jpCenter.add(lblPhone);     
        jpCenter.add(txtPhone); 
        jpCenter.add(lblHeight);        
        jpCenter.add(txtHeight);    

    }
    public void south(){
        fl = new FlowLayout();
        jpSouth.setLayout(fl);
        jbSubmit = new JButton("Submit");
        jbSubmit.addActionListener(this);
        jpSouth.add(jbSubmit);
    }
    public void printInfo(){
        String info = txtName.getText()+" "+txtSurname.getText()+" "+txtPhone.getText()+" ";
        int yes_no = JOptionPane.showConfirmDialog(null, info);
        if(yes_no == JOptionPane.YES_OPTION){
            JOptionPane.showMessageDialog(null, "Saved");
        }
        else if(yes_no == JOptionPane.NO_OPTION){
            JOptionPane.showMessageDialog(null, "Enter again");
            txtName.setText("");
            txtSurname.setText("");
            txtPhone.setText("");
        }
        else{
            JOptionPane.showMessageDialog(null, "Canceled");
        }
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == jbPrint){
            printInfo();
        }
    }
    @Override
    public void mouseClicked(MouseEvent e) {
        if(e.getSource()==txtName){
            txtName.setText("");        
        }
        else if(e.getSource()==txtSurname){
            txtSurname.setText("");     
        }
        else if(e.getSource()==txtPhone){
            txtPhone.setText("");       
        }
        else{
        }
    }
    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub
    }
    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub
    }
    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub
    }
    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub
    }
    @Override
    public void itemStateChanged(ItemEvent arg0) {
        // TODO Auto-generated method stub
    }

}

在center()中,您有:

    add(rbEmergency);
    add(rbControl);
    add(rbOperation);

如果要将这些项目直接添加到框架中,则需要将它们添加到中心面板中。因此,将中心面板的大小增加到16,因为在center()中有6个add(…)。

gl = new GridLayout(4, 4);
jpCenter.setLayout(gl);

现在将center()中的这些更改为:

    jpCenter.add(rbEmergency);
    jpCenter.add(rbControl);
    jpCenter.add(rbOperation);
    jpCenter.add(cbocigaretteusage);
    jpCenter.add(cboalcoholusage);
    jpCenter.add(cbodrugusage);

你也可以添加一个很好的显示方法到你的类

public void display()
{
    this.pack();
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
}

所以要使用它,只需进行

new HospitalRecord().display();

相关内容

  • 没有找到相关文章

最新更新