如何在 Java 中在較小的顯示區中顯示較大的元件
要在一個較小的顯示區中顯示大型元件,請使用 JScrollPane,以便使用者能夠輕鬆滾動瀏覽元件。下面是在 Java 中較小的顯示區中顯示大型元件的示例:-
示例
package my;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
public class SwingDemo {
public static void main(String args[]) {
JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button1 = new JButton("One");
JButton button2 = new JButton("Two");
JButton button3 = new JButton("Three");
JButton button4 = new JButton("Four");
JButton button5 = new JButton("Five");
JButton button6 = new JButton("Six");
Box box = Box.createVerticalBox();
box.add(button1);
box.add(button2);
box.add(button3);
box.add(button4);
box.add(button5);
box.add(button6);
box.setPreferredSize(new Dimension(900,900));
JScrollPane jScrollPane = new JScrollPane();
jScrollPane.setViewportView(box);
frame.add(jScrollPane, BorderLayout.CENTER);
frame.setSize(550, 250);
frame.setVisible(true);
}
}這將生成以下輸出:-

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP