Как сделать всплывающую кнопку
В течение нескольких часов я пытался создать свою кнопку с именем verwijderen (что означает удаление), чтобы щелкнуть по ней и всплывающее сообщение, что-то вроде : Вы уверены, что хотите продолжить? появляется.
Я сделал мой экран в WindowBuilder, и код ниже:
Спасибо:)
package View.Klas;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
public class KlasVerwijderen extends JPanel implements ActionListener {
private JTextField txtNaam;
private JTextField txtNiveau;
private JLabel lblJaar;
private JLabel lblMentor;
private JLabel lblGebruikersnaam;
private JLabel lblDocent;
private JTextField txtMentor;
private JTextField txtGebruikersnaam;
public KlasVerwijderen() {
setLayout(null);
JLabel lblNaam = new JLabel("Naam");
lblNaam.setBounds(12, 13, 143, 33);
add(lblNaam);
txtNaam = new JTextField();
txtNaam.setBounds(167, 13, 149, 33);
add(txtNaam);
txtNaam.setColumns(10);
JLabel lblNiveau = new JLabel("Niveau");
lblNiveau.setBounds(12, 59, 143, 33);
add(lblNiveau);
txtNiveau = new JTextField();
txtNiveau.setColumns(10);
txtNiveau.setBounds(167, 64, 149, 33);
add(txtNiveau);
lblJaar = new JLabel("Jaar");
lblJaar.setBounds(12, 105, 143, 33);
add(lblJaar);
lblMentor = new JLabel("Mentor\r\n");
lblMentor.setBounds(12, 151, 143, 33);
add(lblMentor);
lblGebruikersnaam = new JLabel("Gebruikersnaam");
lblGebruikersnaam.setBounds(12, 197, 143, 33);
add(lblGebruikersnaam);
lblDocent = new JLabel("Docent\r\n");
lblDocent.setBounds(12, 243, 143, 33);
add(lblDocent);
txtMentor = new JTextField();
txtMentor.setColumns(10);
txtMentor.setBounds(167, 156, 149, 33);
add(txtMentor);
txtGebruikersnaam = new JTextField();
txtGebruikersnaam.setColumns(10);
txtGebruikersnaam.setBounds(167, 202, 149, 33);
add(txtGebruikersnaam);
JComboBox comboBoxDocent = new JComboBox();
comboBoxDocent.setModel(new DefaultComboBoxModel(new String[] {"Selecteer Docent", "Van Huele", "Dijks", "Schipper", "Lijcha"}));
comboBoxDocent.setBounds(167, 248, 149, 28);
add(comboBoxDocent);
JComboBox comboBoxJaar = new JComboBox();
comboBoxJaar.setModel(new DefaultComboBoxModel(new String[] {"Leerjaar\t", "1e Jaar\t", "2e Jaar", "3e Jaar", "4e Jaar", "5e Jaar", "6e Jaar"}));
comboBoxJaar.setBounds(167, 110, 149, 28);
add(comboBoxJaar);
JButton btnVerwijderen = new JButton("Verwijderen");
btnVerwijderen.setBounds(12, 444, 143, 33);
add(btnVerwijderen);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
1 ответ
Вам нужно добавить actionListener
на вашу кнопку. actionPerformed
Затем метод вызывается при нажатии на кнопку. Оттуда вы можете отобразить ваше всплывающее сообщение. JOptionPane
может удовлетворить ваши потребности для этого. Увидеть ниже:
JButton btnVerwijderen = new JButton("Verwijderen");
btnVerwijderen.setBounds(12, 444, 143, 33);
add(btnVerwijderen);
btnVerwijderen.addActionListener(this);
Тогда в actionPerformed
метод:
public void actionPerformed(ActionEvent e)
{
Object target=e.getSource();
if (target == btnVerwijderen)
{
JOptionPane.showConfirmDialog(null, "are you sure");
}
}
Конечно, вам нужно будет решить, что делать, в зависимости от того, какая опция нажата. Смотрите Javadoc для showConfirmDialog
для получения дополнительной информации.