单击exitbtn菜单项时如何关闭程序

时间:2016-09-18 07:58:38

标签: java netbeans

我正在学习如何使用GUI管理器在Java中创建GUI。我想把它放在哪里如果单击菜单项exitbtn,程序将关闭。老实说,我对如何实现这一点没有第一点想法。这就是我到目前为止所拥有的

package week3lab;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class posGUI extends javax.swing.JFrame {

    /**
     * Creates new form posGUI
     */
    public posGUI() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        exitbtn = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        addbtn = new javax.swing.JMenuItem();
        nextbtn = new javax.swing.JMenuItem();
        newbtn = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 277, Short.MAX_VALUE)
        );

        jMenu1.setText("File");

        exitbtn.setText("Exit");
        jMenu1.add(exitbtn);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Order");

        addbtn.setText("Add to Order");
        jMenu2.add(addbtn);

        nextbtn.setText("Clear for Next Item");
        jMenu2.add(nextbtn);

        newbtn.setText("New Order");
        jMenu2.add(newbtn);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    /**
     * @param args the command line arguments
     */



    @Override
    public void getInput (ActionEvent e)
    {

    }

    // Variables declaration - do not modify                     
    private javax.swing.JMenuItem addbtn;
    private javax.swing.JMenuItem exitbtn;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JMenuItem newbtn;
    private javax.swing.JMenuItem nextbtn;
    // End of variables declaration                   
}

1 个答案:

答案 0 :(得分:0)

创建onClickListener并使用活动的方法this.finishAffinity();