Java摇摆 | ScrollPaneLayout 类
JScrollPane 使用的布局管理器。 JScrollPaneLayout基于九个组件:一个视口、两个滚动条、一个行标题、一个列标题和四个“角”组件。
类的构造函数:
- ScrollPaneLayout():用于构造一个新的ScrollPanelLayout。
常用方法:
- removeLayoutComponent(Component comp):从布局中移除指定的组件。
- getColumnHeader():它返回作为列标题的 JViewport 对象。
- getVerticalScrollBar():返回处理垂直滚动的 JScrollBar 对象。
- getHorizontalScrollBar():返回处理水平滚动的 JScrollBar 对象。
- addLayoutComponent(String st, Component c):将指定组件添加到布局中。
- getViewport():返回显示可滚动内容的 JViewport 对象。
- getCorner(String key):用于返回指定角的Component。
下面的程序说明了 ScrollPanelLayout 类的使用:
1. 下面的程序通过在一个实例类为“ Geeks ”的JFrame中排列几个JLabel组件来说明 ScrollPaneLayout 的使用。我们创建一个名为“ scrollpane ”的JScrollPane组件和一个名为“ list ”的JList组件。我们使用setSize()和setVisible()方法设置框架的大小和可见性。使用setLayout()方法设置布局。
Java
// Java Program to illustrate the
// ScrollPaneLayout class
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
// create a class Geeks extending JFrame
public class Geeks extends JFrame
{
// Declaration of objects of the
// JScrollPane class.
JScrollPane scrollpane;
// Constructor of Geeks class
public Geeks()
{
// used to call super class
// variables and methods
super("JScrollPane Demonstration");
// Function to set size of JFrame.
setSize(300, 200);
// Function to set Default close
// operation of JFrame.
setDefaultCloseOperation(EXIT_ON_CLOSE);
// to contain a string value
String categories[] = {"Geeks", "Language", "Java",
"Sudo Placement", "Python",
"CS Subject", "Operating System",
"Data Structure", "Algorithm",
"PHP language", "JAVASCRIPT",
"C Sharp" };
// Creating Object of "JList" class
JList list = new JList(categories);
// Creating Object of
// "scrollpane" class
scrollpane = new JScrollPane(list);
// to get content pane
getContentPane().add(scrollpane, BorderLayout.CENTER);
}
// Main Method
public static void main(String args[])
{
// Creating Object of Geeks class.
Geeks sl = new Geeks();
// Function to set visibility of JFrame.
sl.setVisible(true);
}
}
Java
// Java Program to illustrate the
// ScrollPaneLayout class
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
// create a class ScrollPanel
// extending JFrame
public class ScrollPanel extends JFrame {
// Declaration of objects of the
// JScrollPane class
JScrollPane scrollpane;
// Constructor of ScrollPanel class
public ScrollPanel()
{
// used to call super class
// variables and methods
super("JScrollPane Demonstration");
// Function to set size of JFrame.
setSize(300, 200);
// Function to set Default
// close operation of JFrame.
setDefaultCloseOperation(EXIT_ON_CLOSE);
init();
// Function to set
// visible of JFrame.
setVisible(true);
}
// class init
public void init()
{
// Declaration of objects
// of JRadioButton class.
JRadioButton form[][] = new JRadioButton[12][5];
// to contain a string count
String counts[] = {"", "1 star", "2 star",
"3 star", "4 star", "5 star"};
// to contain a string value
String categories[] = {"Geeks", "Language", "Java",
"Sudo Placement", "Python",
"CS Subject", "Operating System",
"Data Structure", "Algorithm",
"PHP language", "JAVASCRIPT",
"C Sharp" };
// Declaration of objects
// of the JPanel class.
JPanel p = new JPanel();
// Function to set size of JFrame.
p.setSize(600, 400);
// Function to set Layout of JFrame.
p.setLayout(new GridLayout(13, 6, 10, 0));
// for loop
for (int row = 0; row < 13; row++) {
// Declaration of objects
// of ButtonGroup class
ButtonGroup bg = new ButtonGroup();
for (int col = 0; col < 6; col++)
{
// If condition
if (row == 0) {
// add new Jlabel
p.add(new JLabel(counts[col]));
}
else {
// If condition
if (col == 0)
{
// add new Jlabel
p.add(new JLabel(categories[row - 1]));
}
else
{
form[row - 1][col - 1] = new JRadioButton();
// add form in ButtonGroup
bg.add(form[row - 1][col - 1]);
// add form in JFrame
p.add(form[row - 1][col - 1]);
}
}
}
}
// Declaration of objects
// of scrollpane class.
scrollpane = new JScrollPane(p);
// to get content pane
getContentPane().add(scrollpane, BorderLayout.CENTER);
}
// Main Method
public static void main(String args[])
{
new ScrollPanel();
}
}
输出:
2. 下面的程序通过在一个JFrame中排列几个JLabel组件来说明ScrollPaneLayout的使用,它的实例类被命名为“ ScrollPanel ”。我们创建了一个名为“ scrollpane ”的JScrollPane组件。此外,还创建了JRadioButton和ButtonGroup 。我们使用setSize()和setVisible()方法设置框架的大小和可见性。使用setLayout()方法设置布局。
Java
// Java Program to illustrate the
// ScrollPaneLayout class
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
// create a class ScrollPanel
// extending JFrame
public class ScrollPanel extends JFrame {
// Declaration of objects of the
// JScrollPane class
JScrollPane scrollpane;
// Constructor of ScrollPanel class
public ScrollPanel()
{
// used to call super class
// variables and methods
super("JScrollPane Demonstration");
// Function to set size of JFrame.
setSize(300, 200);
// Function to set Default
// close operation of JFrame.
setDefaultCloseOperation(EXIT_ON_CLOSE);
init();
// Function to set
// visible of JFrame.
setVisible(true);
}
// class init
public void init()
{
// Declaration of objects
// of JRadioButton class.
JRadioButton form[][] = new JRadioButton[12][5];
// to contain a string count
String counts[] = {"", "1 star", "2 star",
"3 star", "4 star", "5 star"};
// to contain a string value
String categories[] = {"Geeks", "Language", "Java",
"Sudo Placement", "Python",
"CS Subject", "Operating System",
"Data Structure", "Algorithm",
"PHP language", "JAVASCRIPT",
"C Sharp" };
// Declaration of objects
// of the JPanel class.
JPanel p = new JPanel();
// Function to set size of JFrame.
p.setSize(600, 400);
// Function to set Layout of JFrame.
p.setLayout(new GridLayout(13, 6, 10, 0));
// for loop
for (int row = 0; row < 13; row++) {
// Declaration of objects
// of ButtonGroup class
ButtonGroup bg = new ButtonGroup();
for (int col = 0; col < 6; col++)
{
// If condition
if (row == 0) {
// add new Jlabel
p.add(new JLabel(counts[col]));
}
else {
// If condition
if (col == 0)
{
// add new Jlabel
p.add(new JLabel(categories[row - 1]));
}
else
{
form[row - 1][col - 1] = new JRadioButton();
// add form in ButtonGroup
bg.add(form[row - 1][col - 1]);
// add form in JFrame
p.add(form[row - 1][col - 1]);
}
}
}
}
// Declaration of objects
// of scrollpane class.
scrollpane = new JScrollPane(p);
// to get content pane
getContentPane().add(scrollpane, BorderLayout.CENTER);
}
// Main Method
public static void main(String args[])
{
new ScrollPanel();
}
}
输出:
注意:上述程序可能无法在在线 IDE 中运行。请使用离线编译器。
参考: https://docs.oracle.com/javase/7/docs/api/javax/swing/ScrollPaneLayout.html