Java 如何在不同类中的按钮上使用操作侦听器
我是GUI的新手,在这项任务上遇到了麻烦。基本上,我们总共有八个类,必须使用三个面板类三种不同的方式,一种方式仅使用TopPanel和InitialPanel类,一种方式仅使用BottomPanel和InitialPanel类,另一种方式仅使用InitialPanel类。目标是在按下顶部面板上的按钮时,使底部面板上的按钮显示有关足球运动员的信息Java 如何在不同类中的按钮上使用操作侦听器,java,swing,user-interface,actionlistener,Java,Swing,User Interface,Actionlistener,我是GUI的新手,在这项任务上遇到了麻烦。基本上,我们总共有八个类,必须使用三个面板类三种不同的方式,一种方式仅使用TopPanel和InitialPanel类,一种方式仅使用BottomPanel和InitialPanel类,另一种方式仅使用InitialPanel类。目标是在按下顶部面板上的按钮时,使底部面板上的按钮显示有关足球运动员的信息 public class BottomPanel extends JPanel implements ActionListener { publ
public class BottomPanel extends JPanel implements ActionListener
{
public JButton b1;
public BottomPanel(final JPanel topPanel)
{
super();
setBackground(Color.pink);
//setLayout(new GridLayout(3,1));
b1 = new JButton("When the user clicks on the button in the UPPER panel, displays the football player's position here" );
add(b1);
}
public void actionPerformed(ActionEvent event)
{
Object obj = event.getSource();
if(obj == b1)
{
b1.setText(fp1.getPosition());
}
}
}
public class InitialPanel extends JPanel
{
public InitialPanel()
{
super();
setBackground(Color.gray);
setLayout(new BorderLayout());
TopPanel p1 = new TopPanel();
add(p1,"North");
BottomPanel p2 = new BottomPanel(p1);
add(p2,"Center");
}
}`
public class TopPanel extends JPanel
{
public TopPanel()
{
super();
setBackground(Color.yellow);
footballPlayer fp1 = new footballPlayer("Mark","Allen",22, "IST", 5.6f, 180, "Junior","Running Back");
// the whatsUp of this student has to shown in the other panel
JButton jl1 = new JButton(fp1.getInfo());
add(jl1);
}
}`
我想我只运行了TopPanel和InitialPanel,但我仍在为另外两个做些什么。另外,getInfo()是设置底部按钮文本时要调用的方法,我们不能创建TopPanel中使用的对象以外的其他足球运动员对象。任何帮助都将不胜感激 您已经将
jl1
按钮添加到TopPanel
,现在您应该向该按钮添加一个侦听器:您可以在底部面板
类中添加它
您将在底部面板
构造函数中收到对topPanel
的引用,这样您就可以将他保留为成员并创建侦听器,如下所示:
public class BottomPanel extends JPanel implements ActionListener
{
public JButton b1;
/** added member to topPanel **/
private JPanel mTopPanel;
public BottomPanel(final JPanel topPanel)
{
super();
setBackground(Color.pink);
this.mTopPanel = topPanel;
b1 = new JButton("When the user clicks on the button in the UPPER
panel, displays the football player's position here" );
add(b1);
/** the topPanel jli button listener **/
this.mTopPanel.jl1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
/** edit your button with the info of the player **/
b1.setText("player info added here");
}
});
}
public void actionPerformed(ActionEvent event)
{
Object obj = event.getSource();
if(obj == b1)
{
b1.setText(fp1.getPosition());
}
}
}
您应该将底部面板注册为顶部面板中按钮的
侦听器。要实现这一点,您应该在TopPanel
类中创建一个列表,存储按钮的所有侦听器。一旦按下按钮,当前播放机的信息将广播给所有听众。您需要做的是使ButtonPanel
类实现侦听器
,我在这里称之为displayerlinfolistener
class TopPanel extends JPanel {
JButton displayButton = new JButton("show player info");
FootballPlayer currentPlayer = new FootballPlayer();
List<DisplayPlayerInfoListener> listeners = new ArrayList<>();
public TopPanel() {
add(displayButton);
displayButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
displayButtonPressed();
}
});
}
void displayButtonPressed() {
for (DisplayPlayerInfoListener listener : listeners) {
listener.displayPlayer(currentPlayer);
}
}
public void addDisplayPlayerInfoListener(DisplayPlayerInfoListener listener) {
listeners.add(listener);
}
}
interface DisplayPlayerInfoListener {
void displayPlayer(FootballPlayer player);
}
class BottomPanel extends JPanel implements DisplayPlayerInfoListener {
@Override
public void displayPlayer(FootballPlayer player) {
// show the player's information on your bottom panel's UI
}
}
通过这样做,您可以注册任意数量的侦听器,因为这些信息可能也需要广播到其他UI组件。另外,它将两个面板分开,您不需要通过访问方法上的其他面板字段来获取信息。当信息准备好发送时,将对其进行处理。TopPanel只将底部面板视为DisplayPlayerFoliStener实例,因此耦合更少。谢谢!一切看起来都很好,除了一件事,TopPanel的对象仍然没有被识别,有没有办法将mTopPanel与TopPanel类关联起来?如何识别?如果BottomPanel的构造函数接收topPanel作为参数?我不太确定,final JPanel topPanel如何连接到topPanel类?我想这就是问题所在
topPanel.addDisplayPlayerInfoListener(bottomPanel);