下面的代码就演示了为JTextArea、JList增加滚动条的代码:
package com.cownew.Char19;
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import javax.swing.DefaultListModel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.ListModel;
import java.awt.Rectangle;
import javax.swing.JList;
import javax.swing.JScrollPane;
public class ScrollPaneTest1 extends JFrame
{
private JPanel jContentPane = null;
private JTextArea jTextArea = null;
private JList jList = null;
private JScrollPane jScrollPane = null;
private JScrollPane jScrollPane1 = null;
private JList jList1 = null;
private JTextArea jTextArea1 = null;
private JTextArea getJTextArea()
{
if (jTextArea == null)
{
jTextArea = new JTextArea();
jTextArea.setBounds(new Rectangle(12, 7, 95, 71));
}
return jTextArea;
}
private JList getJList()
{
if (jList == null)
{
jList = new JList();
jList.setBounds(new Rectangle(8, 92, 106, 71));
DefaultListModel listModel = new DefaultListModel();
listModel.addElement("22222");
listModel.addElement("33333333");
listModel.addElement("55555555555555");
listModel.addElement("8888888888");
listModel.addElement("88888888");
listModel.addElement("999999999");
jList.setModel(listModel);
}
return jList;
}
private JScrollPane getJScrollPane()
{
if (jScrollPane == null)
{
jScrollPane = new JScrollPane();
jScrollPane.setBounds(new Rectangle(143, 7, 122, 75));
jScrollPane.setViewportView(getJTextArea1());
}
return jScrollPane;
}
private JScrollPane getJScrollPane1()
{
if (jScrollPane1 == null)
{
jScrollPane1 = new JScrollPane();
jScrollPane1.setBounds(new Rectangle(142, 96, 128, 68));
jScrollPane1.setViewportView(getJList1());
}
return jScrollPane1;
}
private JList getJList1()
{
if (jList1 == null)
{
jList1 = new JList();
DefaultListModel listModel = new DefaultListModel();
listModel.addElement("22222");
listModel.addElement("33333333");
listModel.addElement("8888888888888888888888888888");
listModel.addElement("8888888888");
listModel.addElement("88888888");
listModel.addElement("999999999");
jList1.setModel(listModel);
}
return jList1;
}
private JTextArea getJTextArea1()
{
if (jTextArea1 == null)
{
jTextArea1 = new JTextArea();
}
return jTextArea1;
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
ScrollPaneTest1 thisClass = new ScrollPaneTest1();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
public ScrollPaneTest1()
{
super();
initialize();
}
private void initialize()
{
this.setSize(300, 200);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
private JPanel getJContentPane()
{
if (jContentPane == null)
{
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJTextArea(), null);
jContentPane.add(getJList(), null);
jContentPane.add(getJScrollPane(), null);
jContentPane.add(getJScrollPane1(), null);
}
return jContentPane;
}
}
运行效果图:
  Swing中的滚动条_第1张图片
图 17.9
JScrollPane还能为组合界面增加滚动条:
package com.cownew.Char19;
import java.awt.Dimension;
import java.awt.Rectangle;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class ScrollPaneTest2 extends JFrame
{
private JPanel jContentPane = null;
private JScrollPane jScrollPane = null;
private JPanel jPanel = null;
private JButton jButton = null;
private JButton jButton1 = null;
private JCheckBox jCheckBox = null;
private JTextField jTextField = null;
private JSlider jSlider = null;
private JScrollPane getJScrollPane()
{
if (jScrollPane == null)
{
jScrollPane = new JScrollPane();
jScrollPane.setBounds(new Rectangle(28, 17, 142, 114));
jScrollPane.setViewportView(getJPanel());
}
return jScrollPane;
}
private JPanel getJPanel()
{
if (jPanel == null)
{
jPanel = new JPanel();
jPanel.setLayout(null);
jPanel.add(getJButton(), null);
jPanel.add(getJButton1(), null);
jPanel.add(getJCheckBox(), null);
jPanel.add(getJTextField(), null);
jPanel.add(getJSlider(), null);
jPanel.setPreferredSize(new Dimension(300,200));
}
return jPanel;
}
private JButton getJButton()
{
if (jButton == null)
{
jButton = new JButton();
jButton.setBounds(new Rectangle(6, 10, 74, 28));
}
return jButton;
}
private JButton getJButton1()
{
if (jButton1 == null)
{
jButton1 = new JButton();
jButton1.setBounds(new Rectangle(102, 9, 82, 30));
}
return jButton1;
}
private JCheckBox getJCheckBox()
{
if (jCheckBox == null)
{
jCheckBox = new JCheckBox();
jCheckBox.setBounds(new Rectangle(17, 56, 93, 21));
jCheckBox.setText("aaaaabbb");
}
return jCheckBox;
}
private JTextField getJTextField()
{
if (jTextField == null)
{
jTextField = new JTextField();
jTextField.setBounds(new Rectangle(126, 57, 99, 22));
}
return jTextField;
}
private JSlider getJSlider()
{
if (jSlider == null)
{
jSlider = new JSlider();
jSlider.setBounds(new Rectangle(20, 111, 205, 25));
}
return jSlider;
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
ScrollPaneTest2 thisClass = new ScrollPaneTest2();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
public ScrollPaneTest2()
{
super();
initialize();
}
private void initialize()
{
this.setSize(221, 177);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
private JPanel getJContentPane()
{
if (jContentPane == null)
{
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJScrollPane(), null);
}
return jContentPane;
}
}
运行效果图:
  Swing中的滚动条_第2张图片
图 17.10
对于这种组合界面必须为界面设定一个最佳尺寸(PreferredSize),这样JScrollPane才知道如何显示滚动条:jPanel.setPreferredSize( new Dimension(300,200))。
JScrollPane中的ViewPort是一种特殊的对象,通过它就可以查看基层组件,滚动条其实就是沿着组件移动“视点”,这样就可以查看隐藏的部分。
职场 休闲 Swing

0

收藏

上一篇:Java反编译工具Jodecli... 下一篇:XJL:Swing中的格式化文本...
Swing中的滚动条_第4张图片
cownew

289篇文章,54W+人气,0粉丝

Swing中的滚动条_第5张图片

Ctrl+Enter 发布

发布

取消

推荐专栏更多

Swing中的滚动条_第6张图片
基于Python的DevOps实战

自动化运维开发新概念

共20章 | 抚琴煮酒

¥51.00 494人订阅
订   阅
Swing中的滚动条_第7张图片
微服务技术架构和大数据治理实战

大数据时代的微服务之路

共18章 | 纯洁微笑

¥51.00 688人订阅
订   阅

猜你喜欢

我的友情链接 搭建了一个多项目、插件化的ASP.net MVC开发框架 Java线程:创建与启动 mysql启动报错:Starting MySQL... ERROR! The server quit without updating PID file 用UltraISO制作U盘启动盘,支持windows 7 启动网卡报错(Failed to start LSB: Bring up/down networking )解决办法总结 UITableView划动删除的实现 Linux下Tomcat的启动、关闭、杀死进程 Qt学习之路(12): 菜单和工具条 windows通过bat脚本设置服务开机启动 给DIV添加滚动条 windows XP系统Stop c0000218 unknown hard error 蓝屏故障的解决方法 JavaScript基础 从Preact中了解React组件和hooks基本原理 智能合约编程语言-solidity快速入门(下) Android中适配器的notifyDataSetChanged()为何有时不刷新 从hook开始聊聊那些windows内核数据结构 Scala的actor 正则性能调优 windows下kafka+ELK的日志系统
Swing中的滚动条_第8张图片
Swing中的滚动条_第9张图片

扫一扫,领取大礼包

0

分享
Swing中的滚动条_第10张图片
cownew
Swing中的滚动条_第11张图片