第一次来这里
我正在尝试第一次使用秋千。单击时我的按钮不起作用。我找不到错误。我有2个按钮,一个是“添加”,一个是“清除”。添加将稍后将文本字段中的文本添加到文件中,而清除将清除所有文本字段。但是搞定没有回应。我没有看到警告消息框“请填写所有字段!”。或“成功添加”
这是我的代码:
public class CustomerList {
private JFrame frame;
private JTextField txtCompanyName;
private JTextField txtSector;
private JTextField txtCity;
private JTextField txtAddress;
private JTextField txtNotes;
private JButton btnAdd;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CustomerList window = new CustomerList();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public CustomerList() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 646, 469);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblCompanyName = new JLabel("Company Name");
lblCompanyName.setBounds(12, 27, 104, 16);
frame.getContentPane().add(lblCompanyName);
JLabel lblSector = new JLabel("Sector");
lblSector.setBounds(12, 56, 56, 16);
frame.getContentPane().add(lblSector);
JLabel lblCountry = new JLabel("Country");
lblCountry.setBounds(12, 85, 56, 16);
frame.getContentPane().add(lblCountry);
JLabel lblCity = new JLabel("City");
lblCity.setBounds(12, 114, 56, 16);
frame.getContentPane().add(lblCity);
JLabel lblAddress = new JLabel("Address");
lblAddress.setBounds(12, 143, 56, 16);
frame.getContentPane().add(lblAddress);
JLabel lblNotes = new JLabel("Notes");
lblNotes.setBounds(12, 251, 56, 16);
frame.getContentPane().add(lblNotes);
txtCompanyName = new JTextField();
txtCompanyName.setBounds(128, 24, 224, 22);
frame.getContentPane().add(txtCompanyName);
txtCompanyName.setColumns(10);
txtSector = new JTextField();
txtSector.setBounds(128, 53, 224, 22);
frame.getContentPane().add(txtSector);
txtSector.setColumns(10);
JComboBox comboCountry = new JComboBox();
comboCountry.addItem("Select");
comboCountry.addItem("Turkey");
comboCountry.addItem("Afghanistan");
comboCountry.addItem("Albania");
comboCountry.addItem("Algeria");
comboCountry.addItem("Andorra");
comboCountry.addItem("Angola");
comboCountry.addItem("Antigua and Barbuda");
comboCountry.setToolTipText("Country");
comboCountry.setBounds(128, 82, 224, 22);
frame.getContentPane().add(comboCountry);
txtCity = new JTextField();
txtCity.setBounds(128, 111, 224, 22);
frame.getContentPane().add(txtCity);
txtCity.setColumns(10);
txtAddress = new JTextField();
txtAddress.setBounds(128, 140, 224, 99);
frame.getContentPane().add(txtAddress);
txtAddress.setColumns(10);
txtNotes = new JTextField();
txtNotes.setBounds(128, 248, 224, 99);
frame.getContentPane().add(txtNotes);
txtNotes.setColumns(10);
/*
JButton btnAdd = new JButton("Add");
*/
btnAdd = new JButton("Add");
btnAdd.setBounds(388, 23, 97, 25);
frame.getContentPane().add(btnAdd);
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if((txtCompanyName.getText().isEmpty())||(txtSector.getText().isEmpty())||(txtCity.getText().isEmpty())||(txtAddress.getText().isEmpty())||(txtNotes.getText().isEmpty())||(comboCountry.getSelectedItem().equals("Select")))
JOptionPane.showMessageDialog(null, "Please fill all fields!");
else
JOptionPane.showMessageDialog(null, "Successfully Added");
}
});
JButton btnClear = new JButton("Clear");
btnClear.setBounds(497, 23, 97, 25);
frame.getContentPane().add(btnClear);
btnClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
txtCompanyName.setText(null);
txtSector.setText(null);
txtCity.setText(null);
txtAddress.setText(null);
txtNotes.setText(null);
comboCountry.setSelectedItem("Select");
}
});
JButton btnClose = new JButton("Close");
btnClose.setBounds(497, 81, 97, 25);
frame.getContentPane().add(btnClose);
btnClose.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
}
答案 0 :(得分:0)
您的代码对我来说很好用,但是在JOptionPane.showMessageDialog()
的parentComponent参数中使用if ((txtCompanyName.getText().isEmpty()) || (txtSector.getText().isEmpty()) || (txtCity.getText().isEmpty()) || (txtAddress.getText().isEmpty())
|| (txtNotes.getText().isEmpty()) || (comboCountry.getSelectedItem().equals("Select"))) {
JOptionPane.showMessageDialog(frame, "Please fill all fields!");
} else {
JOptionPane.showMessageDialog(frame, "Successfully Added");
}
可能在不同平台上表现不同。也就是说,也许对话框正在显示,但没有显示在您期望的位置。
既然有父框架,为什么不指定它呢?
func getSubscriptionPricePerMonth() -> String {}