📜  Java Swing-JScrollPane

📅  最后修改于: 2020-10-01 03:17:59             🧑  作者: Mango

Java JScrollPane

JscrollPane用于制作组件的可滚动视图。当屏幕大小受到限制时,我们使用滚动窗格来显示大型组件或大小可以动态更改的组件。

建设者

Constructor Purpose
JScrollPane() It creates a scroll pane. The Component parameter, when present, sets the scroll pane’s client. The two int parameters, when present, set the vertical and horizontal scroll bar policies (respectively).
JScrollPane(Component)
JScrollPane(int, int)
JScrollPane(Component, int, int)

有用的方法

Modifier Method Description
void setColumnHeaderView(Component) It sets the column header for the scroll pane.
void setRowHeaderView(Component) It sets the row header for the scroll pane.
void setCorner(String, Component) It sets or gets the specified corner. The int parameter specifies which corner and must be one of the following constants defined in ScrollPaneConstants: UPPER_LEFT_CORNER, UPPER_RIGHT_CORNER, LOWER_LEFT_CORNER, LOWER_RIGHT_CORNER, LOWER_LEADING_CORNER, LOWER_TRAILING_CORNER, UPPER_LEADING_CORNER, UPPER_TRAILING_CORNER.
Component getCorner(String)
void setViewportView(Component) Set the scroll pane’s client.

JScrollPane示例

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JtextArea;

public class JScrollPaneExample {
private static final long serialVersionUID = 1L;

private static void createAndShowGUI() {

// Create and set up the window.
final JFrame frame = new JFrame("Scroll Pane Example");

// Display the window.
frame.setSize(500, 500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// set flow layout for the frame
frame.getContentPane().setLayout(new FlowLayout());

JTextArea textArea = new JTextArea(20, 20);
JScrollPane scrollableTextArea = new JScrollPane(textArea);

scrollableTextArea.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollableTextArea.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

frame.getContentPane().add(scrollableTextArea);
}
public static void main(String[] args) {


javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {
createAndShowGUI();
}
});
}
}

输出: