簡體   English   中英

我的JButton沒有對ActionListener和actionPerformed做出反應

[英]My JButton does not react with ActionListener and actionPerformed

作為我項目的一部分,我編寫了這部分代碼,當按下Button時應該采取行動。 它正常工作,直到我添加了一個新代碼,即設置為“”的變量。 現在它沒有用。 我的意思是,代碼不會等到按下按鈕(我也放了一個標志,在控制台中寫了一個“確定”,但似乎actionPerformed沒有啟動。出了什么問題?我找不到任何理由為什么它不應該等一下按鈕。謝謝

con = new JPanel(); 
JButton bt = new JButton (" Insert ");
con.add (bt);
frame.add (con)
frame.setVisible(true);

bt.addActionListener(
    new ActionListener() {
        public void actionPerformed(ActionEvent e) {              
            radio =  rb.getSelection().getActionCommand();
            speed = "";     
        }
    }   
);

我添加了一些代碼,如下所示,我希望它有所幫助。 我還有些麻煩, ActionPerformed方法顯示錯誤。 代碼中的詳細信息

public class Creaframe extends JFrame implements ActionListener  {  
    public static JPanel con  = new JPanel();
    public static JFrame frame =new Creaframe();
    public static String radio = "";
    public static String speed = "";

    public static void main(String[] args)  {       
        frame.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        frame.addWindowListener(new java.awt.event.WindowAdapter() {
            @Override
            public void windowClosing (java.awt.event.WindowEvent windowEvent) {
            if (JOptionPane.showConfirmDialog (frame, 
                     " Vuoi veramente Uscire ?") == 
                    JOptionPane.YES_OPTION)
                     {              
                    System.exit (0);
                      }
                    }   
                 });

        frame.setSize (1300, 900);
        frame.setLocation (200,100);
        con = new JPanel();
        con.setBackground(Color.YELLOW);

        public Creaframe  () {
    super (" My program " );
    menuBar = new JMenuBar();
    menuBar.add(makeFileMenu());
    menuBar.add(cerca());
    menuBar.add(trova());
    menuBar.add(excel());
    setJMenuBar(menuBar);
    pack(); 
}

    public void button () {

        JRadioButton rb1 = new JRadioButton ("CD");
        rb1.setActionCommand("CD");

        JRadioButton rb2 = new JRadioButton ("SACD"); 
        rb2.setActionCommand("SACD");

        JRadioButton rb3 = new JRadioButton ("Vinile",true);
        rb3.setActionCommand("Vinile");

        JRadioButton rb4 = new JRadioButton ("Vinile 180");
        rb4.setActionCommand("180");

        JRadioButton rb5 = new JRadioButton ("45 Giri");
        rb5.setActionCommand("Y");

        JRadioButton rb6 = new JRadioButton ("33 Giri",true);
        rb6.setActionCommand("N");

        ButtonGroup rb = new ButtonGroup();
        rb.add(rb1);
        rb.add(rb2);
        rb.add(rb3);
        rb.add(rb4);
        ButtonGroup rsp = new ButtonGroup();
        rsp.add(rb5);
        rsp.add(rb6);      

        con.add(rb1);
        con.add(rb2);
        con.add(rb3);
        con.add(rb4);
        JButton bt = new JButton (" Insert ");
        con.add (bt);
        bt.addActionListener(this);
        frame.add (con);
        txt11.setVisible(false);
        frame.setVisible(true); 

       @Override
           public void actionPerformed (ActionEvent event) {   

        **// this method is in fatal error, here below the message.        
        // Multiple markers at this line
        // - Syntax error on token "(", ; expected
        // - void is an invalid type for the variable 
        // actionPerformed
        //  - Syntax error on token ")", ; expected**               


             radio =  rb.getSelection().getActionCommand();
             speed = "";       
             System.out.println (radio + " ok");
}

試試下面的代碼,

使用ActionListerner實現,您不需要使用匿名consturctor多次編寫actionperformed方法。 您可以使用一個actionPerformed方法來處理多個事件

1)在您的類中實現ActionListener接口

    con = new JPanel(); 
    JButton bt = new JButton (" Insert ");
    con.add (bt);

    bt.addActionListener(this);
    frame.add (con)
    frame.setVisible(true);

    @Override
       public void actionPerformed(ActionEvent evt) {      
         radio =  rb.getSelection().getActionCommand();
         speed = "";       
         //extra code//
    }

並且您可以使用以下方法刪除偵聽器。

bt.removeActionListener(this);

編輯添加了完整的類供您使用,我認為您已正確實現構造函數以獲取MenuBar

檢查創建JMenubar的鏈接單擊此處

public class Creaframe extends JFrame implements ActionListener  {  
    public static JPanel con  = new JPanel();
    public static JFrame frame =new Creaframe();
    public static String radio = "";
    public static String speed = "";
    ButtonGroup rb ;
    public static void main(String[] args)  {       
        frame.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        frame.addWindowListener(new java.awt.event.WindowAdapter() {
            @Override
            public void windowClosing (java.awt.event.WindowEvent windowEvent) {
            if (JOptionPane.showConfirmDialog (frame, 
                     " Vuoi veramente Uscire ?") == 
                    JOptionPane.YES_OPTION)
                     {              
                    System.exit (0);
                      }
                    }   
                 });

        frame.setSize (1300, 900);
        frame.setLocation (200,100);
        con = new JPanel();
        //con.setBackground(Color.YELLOW);
    }

        public Creaframe  () {
            super (" My program " );
    //Your menubar related code ned to add here
           }


    public void button () {

        JRadioButton rb1 = new JRadioButton ("CD");
        rb1.setActionCommand("CD");

        JRadioButton rb2 = new JRadioButton ("SACD"); 
        rb2.setActionCommand("SACD");

        JRadioButton rb3 = new JRadioButton ("Vinile",true);
        rb3.setActionCommand("Vinile");

        JRadioButton rb4 = new JRadioButton ("Vinile 180");
        rb4.setActionCommand("180");

        JRadioButton rb5 = new JRadioButton ("45 Giri");
        rb5.setActionCommand("Y");

        JRadioButton rb6 = new JRadioButton ("33 Giri",true);
        rb6.setActionCommand("N");

        rb= new ButtonGroup();
        rb.add(rb1);
        rb.add(rb2);
        rb.add(rb3);
        rb.add(rb4);
        ButtonGroup rsp = new ButtonGroup();
        rsp.add(rb5);
        rsp.add(rb6);      

        con.add(rb1);
        con.add(rb2);
        con.add(rb3);
        con.add(rb4);
        JButton bt = new JButton (" Insert ");
        con.add (bt);
        bt.addActionListener(this);
        frame.add (con);
        frame.setVisible(true); 
    }

       @Override
           public void actionPerformed (ActionEvent event) { 
             radio =  rb.getSelection().getActionCommand();
             speed = "";       
             System.out.println (radio + " ok");
                            }
}

暫無
暫無

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

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