Java 如何在一个类中使用Jbutton来触发另一个类中执行的按钮操作中的panelSlider效果

Java 如何在一个类中使用Jbutton来触发另一个类中执行的按钮操作中的panelSlider效果,java,Java,如何在一个类中使用Jbutton来触发另一个类中执行的按钮操作中的panelSlider效果 下面是面板滑动效果的示例。您可以将其作为参考并实现您的需求 import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.J

如何在一个类中使用Jbutton来触发另一个类中执行的按钮操作中的panelSlider效果


下面是面板滑动效果的示例。您可以将其作为参考并实现您的需求

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

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

public class SlidingPanel {

    JPanel panel;

    public void makeUI() {
        panel = new JPanel();
        panel.setBackground(Color.RED);
        panel.setBounds(0, 0, 400, 400);

        JButton button = new JButton("Click");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ((JButton) e.getSource()).setEnabled(false);
                new Timer(1, new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        panel.setLocation(panel.getX() - 1, 0);
                        if (panel.getX() + panel.getWidth() == 0) {
                            ((Timer) e.getSource()).stop();
                            System.out.println("Timer stopped");
                        }
                    }
                }).start();
            }
        });
        panel.add(button);
        JFrame frame = new JFrame("Sliding Panel");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setLocationRelativeTo(null);
        frame.setLayout(null);
        frame.add(panel);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new SlidingPanel().makeUI();
            }
        });
    }
}
更新

在HomePane.java中

rightPane rPane = new rightPane();
JPanel lPane = new leftPane(rPane);
private rightPane rPane;
public leftPane(final rightPane rPane)
    {
        pane = new JPanel();
        this.rPane = rPane;
        staffLogin = new JButton("STAFF LOGIN");
        adminLogin = new JButton("ADMIN LOGIN");
        guestLogin = new JButton("GUEST LOGIN");

        staffLogin.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e)
            {
                rPane.trigerEvent();
            }
        });

        this.setLayout(null);

        this.add(staffLogin);
        this.add(adminLogin);
        this.add(guestLogin);

        staffLogin.setFont(new Font("Serif", Font.BOLD, 20));
        adminLogin.setFont(new Font("Serif", Font.BOLD, 20));
        guestLogin.setFont(new Font("Serif", Font.BOLD, 20));

        staffLogin.setBounds(20, 10, 200, 50);
        adminLogin.setBounds(20, 75, 200, 50);
        guestLogin.setBounds(20, 140, 200, 50);
        this.setBorder(BorderFactory.createLineBorder(Color.BLUE));
        pane.setBorder(BorderFactory.createLineBorder(Color.red));
    }
在leftPane.java中

rightPane rPane = new rightPane();
JPanel lPane = new leftPane(rPane);
private rightPane rPane;
public leftPane(final rightPane rPane)
    {
        pane = new JPanel();
        this.rPane = rPane;
        staffLogin = new JButton("STAFF LOGIN");
        adminLogin = new JButton("ADMIN LOGIN");
        guestLogin = new JButton("GUEST LOGIN");

        staffLogin.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e)
            {
                rPane.trigerEvent();
            }
        });

        this.setLayout(null);

        this.add(staffLogin);
        this.add(adminLogin);
        this.add(guestLogin);

        staffLogin.setFont(new Font("Serif", Font.BOLD, 20));
        adminLogin.setFont(new Font("Serif", Font.BOLD, 20));
        guestLogin.setFont(new Font("Serif", Font.BOLD, 20));

        staffLogin.setBounds(20, 10, 200, 50);
        adminLogin.setBounds(20, 75, 200, 50);
        guestLogin.setBounds(20, 140, 200, 50);
        this.setBorder(BorderFactory.createLineBorder(Color.BLUE));
        pane.setBorder(BorderFactory.createLineBorder(Color.red));
    }

你能添加更多的代码吗?在
程序菜单中使用的
programDisplay()
构造函数在哪里?在哪里实例化按钮和
jpanel
?公共类programDisplay扩展了javax.swing.jpanel{/***创建了新的表单programDisplay*/public programDisplay(){initComponents();}我正在努力粘贴代码,所以我在thanxmhh上面截取了一个屏幕截图,这也只是一个片段。只需将代码复制到您的问题中,选择代码并按Ctrl+K。thanx这么多我使用了您的滑块代码,但仍然无法实现我想要的功能。如果我能提供帮助,请使用图像清楚地解释您的要求。我添加了一个链接上传整个程序的视频截图…太多了…Dipali将截图添加到问题我有3个类根据视频…一个是homepane.java,leftpane.java和rightPane.java…所以当我单击左窗格中的Admin Login按钮时…它应该会触发右窗格中的一个按钮,将窗格滑动到同一个右窗格中