2013-11-20 56 views
0

我想了幾個小時,使我的按鈕,名爲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 

    } 
} 

回答

0

你需要一個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

相關問題