public class CustomerEvent extends JApplet
{
JPanel panelObject;
JLabel labelCustName;
JLabel labelCustNo;
JLabel labelCustSex;
JLabel labelCustAge;
JButton buttonAccept;
JTextField textCustName;
JTextField textCustNo;
JComboBox comboCustSex;
JTextField textCustAge;
GridBagLayout gbObject;
GridBagConstraints gbc;
public void init()
{
gbObject = new GridBagLayout();
gbc = new GridBagConstraints();
panelObject = (JPanel)getContentPane();
panelObject.setLayout(gbObject);
buttonAccept = new JButton("Accept");
labelCustName = new JLabel("Customer Name");
labelCustNo = new JLabel("Customer Number");
labelCustSex = new JLabel("Sex");
labelCustAge = new JLabel("Age");
textCustName = new JTextField(5);
textCustNo = new JTextField(5);
textCustAge = new JTextField(10);
String Sex[] = { "Male","Female"};
comboCustSex = new JComboBox(Sex);
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridx = 1;
gbc.gridy = 5;
gbObject.setConstraints(labelCustName,gbc);
panelObject.add(labelCustName);
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridx = 4;
gbc.gridy = 5;
gbObject.setConstraints(textCustName,gbc);
panelObject.add(textCustName);
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridx = 1;
gbc.gridy = 8;
gbObject.setConstraints(labelCustNo,gbc);
panelObject.add(labelCustNo);
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridx = 4;
gbc.gridy = 8;
gbObject.setConstraints(textCustNo,gbc);
panelObject.add(textCustNo);
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridx = 1;
gbc.gridy = 11;
gbObject.setConstraints(labelCustSex,gbc);
panelObject.add(labelCustSex);
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridx = 4;
gbc.gridy = 11;
gbObject.setConstraints(comboCustSex,gbc);
panelObject.add(comboCustSex);
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridx = 1;
gbc.gridy = 14;
gbObject.setConstraints(labelCustAge,gbc);
panelObject.add(labelCustAge);
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridx = 4;
gbc.gridy = 14;
gbObject.setConstraints(textCustAge,gbc);
panelObject.add(textCustAge);
gbc.anchor = GridBagConstraints.NORTHEAST;
gbc.gridx = 5;
gbc.gridy = 17;
gbObject.setConstraints(buttonAccept,gbc);
panelObject.add(buttonAccept);
ValidateAction validateButton = new ValidateAction();
buttonAccept.addActionListener(validateButton);
}
class ValidateAction implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
Object obj = evt.getSource();
if(obj == buttonAccept)
{
String customerNo = textCustNo.getText();
if(customerNo.length() == 0)
{
getAppletContext().showStatus("Customer Number cannot be empty");
return;
}
}
}
}
}
參考答案:可能 String 类名有冲突了
用 String 的时候也用显示
java.lang.String Sex[] = { "Male","Female"};
java.lang.String customerNo = textCustNo.getText();