【好文章】JScrollPane JPanel FlowLayout自动换行

2023-12-16 03:58

本文主要是介绍【好文章】JScrollPane JPanel FlowLayout自动换行,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

今天上午没什么事儿,打开CSDN的BBS,发现有人在问一个关于布局管理器的问题,请看:关于布局的一个问题 。说实话,开始我并不相信楼主说的这句话“然后我在JPanel外面套了一个JScrollPane,却发现图片会一直往右边加,超出JPanel的宽度则出现横向的滚动条,与预想的效果相差较大。”


于是写下下面的代码:


view plaincopy to clipboardprint?
01.import java.awt.*;  
02.import java.awt.event.*;  
03.import javax.swing.*;  
04. 
05. 
06.public class SimpleTest extends JFrame{  
07.    private ImageIcon image;  
08.    private JLabel label;  
09.    private JButton button;  
10.    private JPanel buttonPanel, imagePanel;  
11.    private JScrollPane scrollPane;  
12.    private Container container;  
13.      
14.    public SimpleTest(int xPixels, int yPixels){  
15.        super("Add Image");   
16.          
17.        button = new JButton("Add Image");  
18.        image = new ImageIcon("C:/1.jpg");  
19.        imagePanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));  
20.        scrollPane = new JScrollPane(imagePanel);  
21.              
22.        button.addActionListener(new ActionListener(){  
23.            public void actionPerformed(ActionEvent e){   
24.                label = new JLabel(image);  
25.                imagePanel.add(label);  
26.                validate();  
27.            }  
28.        });  
29. 
30.        buttonPanel = new JPanel(new GridLayout(1, 5));  
31.        buttonPanel.add(button);  
32.          
33.        container = getContentPane();  
34.        container.setLayout(new GridLayout(2, 1));    
35.          
36.        container.add(buttonPanel);  
37.        container.add(scrollPane);  
38.          
39.        setSize(xPixels, yPixels);  
40.        setVisible(true);  
41.    }  
42.      
43.    public static void main(String[] args) {   
44.        new SimpleTest(400, 400);  
45.    }  
46.} 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;



public class SimpleTest extends JFrame{
 private ImageIcon image;
 private JLabel label;
 private JButton button;
 private JPanel buttonPanel, imagePanel;
 private JScrollPane scrollPane;
 private Container container;
 
 public SimpleTest(int xPixels, int yPixels){
  super("Add Image"); 
  
  button = new JButton("Add Image");
  image = new ImageIcon("C:/1.jpg");
  imagePanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
  scrollPane = new JScrollPane(imagePanel);
   
  button.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){ 
    label = new JLabel(image);
    imagePanel.add(label);
    validate();
   }
  });


  buttonPanel = new JPanel(new GridLayout(1, 5));
  buttonPanel.add(button);
  
  container = getContentPane();
  container.setLayout(new GridLayout(2, 1)); 
  
  container.add(buttonPanel);
  container.add(scrollPane);
  
  setSize(xPixels, yPixels);
  setVisible(true);
 }
 
 public static void main(String[] args) {
  new SimpleTest(400, 400);
 }
}
 


输出请看图:


 

这篇关于【好文章】JScrollPane JPanel FlowLayout自动换行的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/499007

相关文章

SpringBoot实现RSA+AES自动接口解密的实战指南

《SpringBoot实现RSA+AES自动接口解密的实战指南》在当今数据泄露频发的网络环境中,接口安全已成为开发者不可忽视的核心议题,RSA+AES混合加密方案因其安全性高、性能优越而被广泛采用,本... 目录一、项目依赖与环境准备1.1 Maven依赖配置1.2 密钥生成与配置二、加密工具类实现2.1

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

SQL Server跟踪自动统计信息更新实战指南

《SQLServer跟踪自动统计信息更新实战指南》本文详解SQLServer自动统计信息更新的跟踪方法,推荐使用扩展事件实时捕获更新操作及详细信息,同时结合系统视图快速检查统计信息状态,重点强调修... 目录SQL Server 如何跟踪自动统计信息更新:深入解析与实战指南 核心跟踪方法1️⃣ 利用系统目录

Spring Security 单点登录与自动登录机制的实现原理

《SpringSecurity单点登录与自动登录机制的实现原理》本文探讨SpringSecurity实现单点登录(SSO)与自动登录机制,涵盖JWT跨系统认证、RememberMe持久化Token... 目录一、核心概念解析1.1 单点登录(SSO)1.2 自动登录(Remember Me)二、代码分析三、

MyBatis-Plus 自动赋值实体字段最佳实践指南

《MyBatis-Plus自动赋值实体字段最佳实践指南》MyBatis-Plus通过@TableField注解与填充策略,实现时间戳、用户信息、逻辑删除等字段的自动填充,减少手动赋值,提升开发效率与... 目录1. MyBATis-Plus 自动赋值概述1.1 适用场景1.2 自动填充的原理1.3 填充策略

Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发

《SpringBoot结合WxJava实现文章上传微信公众号草稿箱与群发》本文将详细介绍如何使用SpringBoot框架结合WxJava开发工具包,实现文章上传到微信公众号草稿箱以及群发功能,... 目录一、项目环境准备1.1 开发环境1.2 微信公众号准备二、Spring Boot 项目搭建2.1 创建

SpringBoot+Docker+Graylog 如何让错误自动报警

《SpringBoot+Docker+Graylog如何让错误自动报警》SpringBoot默认使用SLF4J与Logback,支持多日志级别和配置方式,可输出到控制台、文件及远程服务器,集成ELK... 目录01 Spring Boot 默认日志框架解析02 Spring Boot 日志级别详解03 Sp

浏览器插件cursor实现自动注册、续杯的详细过程

《浏览器插件cursor实现自动注册、续杯的详细过程》Cursor简易注册助手脚本通过自动化邮箱填写和验证码获取流程,大大简化了Cursor的注册过程,它不仅提高了注册效率,还通过友好的用户界面和详细... 目录前言功能概述使用方法安装脚本使用流程邮箱输入页面验证码页面实战演示技术实现核心功能实现1. 随机

HTML5实现的移动端购物车自动结算功能示例代码

《HTML5实现的移动端购物车自动结算功能示例代码》本文介绍HTML5实现移动端购物车自动结算,通过WebStorage、事件监听、DOM操作等技术,确保实时更新与数据同步,优化性能及无障碍性,提升用... 目录1. 移动端购物车自动结算概述2. 数据存储与状态保存机制2.1 浏览器端的数据存储方式2.1.

一文详解MySQL如何设置自动备份任务

《一文详解MySQL如何设置自动备份任务》设置自动备份任务可以确保你的数据库定期备份,防止数据丢失,下面我们就来详细介绍一下如何使用Bash脚本和Cron任务在Linux系统上设置MySQL数据库的自... 目录1. 编写备份脚本1.1 创建并编辑备份脚本1.2 给予脚本执行权限2. 设置 Cron 任务2