【swing】SplitPanel

news/2025/2/28 7:10:45/

当使用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);// 添加左右面板到SplitPanesplitPane.setLeftComponent(leftPanel);splitPane.setRightComponent(rightPanel);// 监听按钮点击事件garenButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {displayImage("garen.jpg");}});teemoButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {displayImage("teemo.jpg");}});annieButton.addActionListener(new ActionListener() {@Overridepublic 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在事件调度线程中创建并显示窗口。


http://www.ppmy.cn/news/86317.html

相关文章

Altium Designer18基础

原理图 第5课 元件库介绍及电阻容模型的创建.mp4_哔哩哔哩_bilibili 原理图库(schLib) 1. 创建原理图库: panels -> SCHLIB:左侧弹出SCH Lib窗口 Add:新建原理图库 点击原理图库名称:进入原理图库设置…

ubuntu server 18.04 安装zmq

安装依赖 sudo apt-get install libtool pkg-config build-essential autoconf automake 编译安装ZMQ使用的加密库 sudo wget https://download.libsodium.org/libsodium/releases/LATEST.tar.gz sudo tar -zxvf LATEST.tar.gz cd libsodium-stable ./configure make make c…

C++ 11 lock_guard 和 unique_lock

文章目录 前言一、简介1.1 lock_guard1.2 RAII1.3 原理 二、unique_lock2.1 简介2.2 使用2.3 原理 三、 lock_guard 和 unique_lock比较3.1 锁的管理方式3.2 灵活性3.3 可移动性 总结 前言 最近在写C程序,需要用到 lock_guard ,并记录 C11新特性 lock_g…

极米科技发布新一代光源技术,投影行业要变天?

作者:坚白 2018年以来,在年轻人群的追捧下,此前主要应用于商用场景的投影仪,逐渐切换到家用场景,而且随着投影技术的持续改进,投影效果也不断提升,家用投影市场进入高速发展期。 但进入2022年…

如何在华为OD机试中获得满分?Java实现【数字颠倒】一文详解!

✅创作者:陈书予 🎉个人主页:陈书予的个人主页 🍁陈书予的个人社区,欢迎你的加入: 陈书予的社区 🌟专栏地址: Java华为OD机试真题(2022&2023) 文章目录 1. 题目描述2. 输入描述3. 输出描述…

c++小游戏——谷歌小恐龙

请用Dev-cpp6.5或专业C程序&#xff0c;否则不可运行。 #include <iostream> #include <conio.h> #include <ctime> #include <chrono> #include <thread> #include <Windows.h> using namespace std; // 游戏画面宽度和高度 const int…

考研考公太卷了,出国留学能弯道超车吗?

这届年轻人太难了&#xff01; 国内高考人数越来越多&#xff0c;考上好大学的难度很大。2022年&#xff0c;高考报名人数是1193万&#xff0c;但考上本科的只有466万&#xff0c;考上双一流大学的不足50万&#xff0c;上双一流大学考生的比例不到5%。 高考只是卷的开始&…

java中collection的循环遍历

package com.test.Test01;import java.util.*;public class Test02 {//这是一个main方法&#xff0c;是程序的入口public static void main(String[] args) {Collection col new ArrayList();//调用方法//集合有一个特点&#xff1a;只能存放引用数据类型的数据&#xff0c;不…