簡體   English   中英

如何制作一個彈出按鈕

[英]How to make an pop up button

我試圖用幾個小時來制作名為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

    }
}

您需要在按鈕上添加一個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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM