【swing】SplitPanel

当使用Java的Swing库来实现一个左右风格的SplitPanel时,可以使用JSplitPane作为容器,并在左边的面板中放置三个按钮,以及在右边的面板中显示图片。以下是一个示例代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SplitPanelExample extends JFrame {

    private JLabel imageLabel;

    public SplitPanelExample() {
        setTitle("SplitPanel Example");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(600, 400));

        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        splitPane.setDividerLocation(200); // 设置分割条位置

        // 左边面板
        JPanel leftPanel = new JPanel();
        leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
        JButton garenButton = createButton("盖伦");
        JButton teemoButton = createButton("提莫");
        JButton annieButton = createButton("安妮");
        leftPanel.add(garenButton);
        leftPanel.add(teemoButton);
        leftPanel.add(annieButton);

        // 右边面板
        JPanel rightPanel = new JPanel();
        rightPanel.setBackground(Color.WHITE);
        imageLabel = new JLabel(new ImageIcon("garen.jpg")); // 默认显示盖伦图片
        rightPanel.add(imageLabel);

        // 添加左右面板到SplitPane
        splitPane.setLeftComponent(leftPanel);
        splitPane.setRightComponent(rightPanel);

        // 监听按钮点击事件
        garenButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                displayImage("garen.jpg");
            }
        });

        teemoButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                displayImage("teemo.jpg");
            }
        });

        annieButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                displayImage("annie.jpg");
            }
        });

        add(splitPane);
        pack();
        setLocationRelativeTo(null); // 居中显示窗口
    }

    private JButton createButton(String text) {
        JButton button = new JButton(text);
        button.setAlignmentX(Component.CENTER_ALIGNMENT);
        return button;
    }

    private void displayImage(String imagePath) {
        ImageIcon imageIcon = new ImageIcon(imagePath);
        imageLabel.setIcon(imageIcon);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new SplitPanelExample().setVisible(true);
            }
        });
    }
}

在这个示例中,创建了一个SplitPanelExample类,继承自JFrame。在构造函数中,首先设置窗口的标题、关闭操作和首选大小。

然后,创建一个JSplitPane作为主要容器,并设置分割条的位置。

左边的面板使用JPanel,使用BoxLayout布局管理器,垂直排列三个按钮。通过createButton方法创建按钮,并将其添加到左边面板。

右边的面板也是一个JPanel,背景设置为白色。创建一个JLabel用于显示图片,默认显示盖伦的图片。将JLabel添加到右边面板。

接下来,使用setLeftComponentsetRightComponent方法将左边面板和右边面板添加到JSplitPane

为三个按钮添加ActionListener,当按钮被点击时,调用displayImage方法来显示对应的图片。displayImage方法将创建一个ImageIcon对象,并将其设置为JLabel的图标。

最后,将JSplitPane添加到窗口中,并设置窗口的位置居中。通过SwingUtilities.invokeLater在事件调度线程中创建并显示窗口。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值