java 在 JPanel 中执行 ActionListener

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14908208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:48:33  来源:igfitidea点击:

performing ActionListener within the JPanel

javaswingjpanelactionlistener

提问by ram

Here, i wanted to perform an action when a button is pressed, it is quite easy but the main problem is that to be performed within an extended JPanel. There might be an easy solution like add action listener to that particular button and invoke actionperformed event but my case is not like that, i have 4 buttons(t1,t2,t3,t4) all these buttons are to be functioned within a single ActionPerfomed event ae (look the segment of code). And later you can see the code that calls another JButtons tr and rf calling the actionlistener and actionperformed events.

在这里,我想在按下按钮时执行一个操作,这很容易,但主要问题是要在扩展的 JPanel 中执行。可能有一个简单的解决方案,例如向该特定按钮添加动作侦听器并调用 actionperformed 事件,但我的情况并非如此,我有 4 个按钮(t1、t2、t3、t4)所有这些按钮都将在单个 ActionPerfomed 中运行事件 ae(查看代码段)。稍后您可以看到调用另一个 JButton tr 和 rf 调用 actionlistener 和 actionperformed 事件的代码。

for clear understandability you can have a glance at the code...

为了清晰易懂,您可以看一眼代码...

 class Tracker extends JPanel
{

public static void main(String[] args) {
    new Tracker();    }

public Tracker()
{

JButton tr=new JButton("TRACKER APPLET");
tr.setBounds(720,170,100,20);

JButton rf=new JButton("REFRESH");
rf.setBounds(200,170,100,20);

boolean tc1=false,tc2=false,tc3=false,tc4=false;

JButton t1=new JButton(" ");
t1.addActionListener(this);
JButton t2=new JButton(" ");
t2.addActionListener(this);
JButton t3=new JButton(" ");
t3.addActionListener(this);
JButton t4=new JButton(" ");
t4.addActionListener(this);


public void actionPerformed(ActionEvent ae)
{
    if(ae.getSource()==t1)
    {
            tc1=true;
    }
    if(ae.getSource()==t2)
    {
            tc2=true;
    }
    if(ae.getSource()==t3)
    {
            tc3=true;
    }
    if(ae.getSource()==t4)
    {
            tc4=true;
    }

}


tr.addActionListener(new ActionListener(){
 public void actionPerformed(ActionEvent e){

    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException ex){}
            catch(InstantiationException ex){}
            catch(IllegalAccessException ex){}
            catch(UnsupportedLookAndFeelException ex) {}

                //some food here....


                           }
                        });
            }
             });        

    rf.addActionListener(new ActionListener(){
 public void actionPerformed(ActionEvent e){

    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run()
        {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } 
            catch (ClassNotFoundException ex){}
            catch(InstantiationException ex){}
            catch(IllegalAccessException ex){}
            catch(UnsupportedLookAndFeelException ex) {}
                         //some extra work runs here...
        }
    });
 }
    });
add(tr);
add(rf);
add(t1);
add(t2);
add(t3);
add(t4);
}

My problem here is i couldn't implement ActionListener as the main class already extending JPanel. I'm just trying to get work with Jbuttons :: wanted to perfom a single action when the buttons(t1|t2|t3|t4) and the JButtons(tr|rf) are pressed in a chronological order..

我的问题是我无法实现 ActionListener 作为已经扩展 JPanel 的主类。我只是想使用 Jbuttons :: 想要在按时间顺序按下按钮(t1|t2|t3|t4)和 JButtons(tr|rf)时执行单个操作..

thanks in advance..

提前致谢..

回答by Aubin

class Tracker extends JPanel implements ActionListener

Your code contains some methods into methods...

您的代码包含一些方法到方法中...

This code works:

此代码有效:

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

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Tracker extends JPanel implements ActionListener {
   public Tracker() {
      tr.setBounds( 720, 170, 100, 20 );
      tr.addActionListener( this );

      rf.setBounds( 200, 170, 100, 20 );
      rf.addActionListener( this );

      t1.addActionListener( this );
      t2.addActionListener( this );
      t3.addActionListener( this );
      t4.addActionListener( this );
      add(tr);
      add(rf);
      add(t1);
      add(t2);
      add(t3);
      add(t4);
   }

   @Override
   public void actionPerformed( ActionEvent e ) {
      Object src = e.getSource();
      if( src == t1 ) {
         tc1 = true;
      }
      else if( src == t2 ) {
         tc2 = true;
      }
      else if( src == t3 ) {
         tc3 = true;
      }
      else if( src == t4 ) {
         tc4 = true;
      }
      else if( src == tr ) {

      }
      else if( src == rf ) {

      }
   }

   private final JButton tr = new JButton( "TRACKER APPLET" );
   private final JButton rf = new JButton( "REFRESH" );
   private final JButton t1 = new JButton( " " );
   private final JButton t2 = new JButton( " " );
   private final JButton t3 = new JButton( " " );
   private final JButton t4 = new JButton( " " );
   boolean tc1 = false, tc2 = false, tc3 = false, tc4 = false;

   public static void main( String[] args ) {
      JFrame frame = new JFrame( "" );
      frame.add( new Tracker());
      frame.pack();
      frame.setLocationRelativeTo( null );
      frame.setVisible( true );
   }
}