gpt4 book ai didi

java - 在 JPanel 中执行 ActionListener

转载 作者:太空宇宙 更新时间:2023-11-04 07:48:29 25 4
gpt4 key购买 nike

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

提前致谢..

最佳答案

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

关于java - 在 JPanel 中执行 ActionListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14908208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com