1

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

为了清楚易懂,您可以看一下代码...

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

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

提前致谢..

4

1 回答 1

1
class Tracker extends JPanel implements ActionListener

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

此代码有效:

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 );
   }
}
于 2013-02-16T07:54:14.480 回答