简体   繁体   English

在JPanel中执行ActionListener

[英]performing ActionListener within the JPanel

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. 在这里,我想在按下按钮时执行一个动作,这很容易,但是主要的问题是要在扩展的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). 可能有一个简单的解决方案,例如将动作侦听器添加到该特定按钮并调用actionperformed事件,但是我的情况并非如此,我有4个按钮(t1,t2,t3,t4),所有这些按钮都应在单个ActionPerfomed中运行事件ae(查看代码段)。 And later you can see the code that calls another JButtons tr and rf calling the actionlistener and actionperformed events. 之后,您将看到调用另一个JButtons 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. 我的问题是我无法将ActionListener用作已扩展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.. 我只是想开始使用Jbuttons ::要按时间顺序按一下button(t1 | t2 | t3 | t4)和JButtons(tr | rf)时执行单个动作。

thanks in advance.. 提前致谢..

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 );
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM