如何在 Java 中僅停用水平捲軸?
在 Java 中僅停用水平捲軸,請使用 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER。假設您使用一些按鈕元件建立了一個 Box。現在,建立一個 JScrollPane −
JScrollPane scrollPane = new JScrollPane();
將視窗檢視設為 Box −
scrollPane.setViewportView(box);
現在,停用水平捲軸 −
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
以下是僅停用水平捲軸的一個示例 −
示例
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("Tutorials");
JButton button2 = new JButton("Quiz");
JButton button3 = new JButton("Questions and Answers");
JButton button4 = new JButton("Videos");
JButton button5 = new JButton("Tools");
JButton button6 = new JButton("Online Compiler");
Box box = Box.createVerticalBox();
box.setPreferredSize(new Dimension(900,900));
box.add(button1);
box.add(button2);
box.add(button3);
box.add(button4);
box.add(button5);
box.add(button6);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(box);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(550, 250);
frame.setVisible(true);
}
}將產生以下輸出 −

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