gpt4 book ai didi

java - GUI 部分未显示

转载 作者:行者123 更新时间:2023-12-01 23:03:27 25 4
gpt4 key购买 nike

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();

关于java - GUI 部分未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23180785/

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