我有一个带有文本字段和按钮的 JDialog。如果我按下按钮并且文本字段为空,则会打印一条错误消息。但现在我想打开一个新窗口(我猜是 JDialog),其中包含文本字段的信息。
{
JButton aceptarButton = new JButton("Aceptar");
aceptarButton.setBounds(332, 387, 86, 23);
contentPanel.add(aceptarButton);
aceptarButton.setFont(new Font("Tahoma", Font.PLAIN, 11));
aceptarButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
ArticuloDAO dao = new ArticuloDAO();
Articulo a = new Articulo();
if (nombreField.getText().equals("")) {
System.out.println("Nombre");
}else{
String nombre = nombreField.getText();
a.setNombre(nombre);
dao.insert(a);
Success s = new Success();
s.setVisible(true);
setVisible(false);
}
}
});
aceptarButton.setActionCommand("OK");
getRootPane().setDefaultButton(aceptarButton);
}
每当我单击“确定”并且 textField nombreField 为空时,就会打开一个显示“名称丢失”的新窗口。怎么样?
最佳答案
您只需使用JOptionPane
即可实现此目的。就像这样调用它:
JOptionPane.showMessageDialog(this, "Name is missing");
它将在对话框中显示消息并等待确定。
关于Java - 从另一个 JDialog 获取 var 信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18132130/