This article mainly studies the implementation code example of the JScrollPane scrollbar of the swing component, as follows.
/* * Scrollbar*/import javax.swing.*;public class Demo1 extends JFrame{JTextArea jta = null;JScrollPane jsp = null;public static void main(String[] args) {// TODO Auto-generated method stub Demo1 d = new Demo1();}public Demo1() {jta = new JTextArea();/* * JScrollPane(Component view, int vsbPolicy, int hsbPolicy) * Create a JScrollPane that displays the view component in a jta viewport, and the view position is controlled by a pair of scrollbars. * vsbPolicy Display policy for vertical scroll bars. Default is ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED. * hsbPolicy is a display policy for horizontal scroll bars. Default is ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED. * If the horizontal scroll bar is not displayed, use JScrollPane.HORIZONTAL_SCROLLBAR_NEVER * If the vertical scroll bar is not displayed, use JScrollPane.VERTICAL_SCROLLBAR_NEVER * * If you want to display scroll bars in both directions in the end, you cannot do this directly * jsp = new JScrollPane(jta); * The display effect is the same * * The scroll bar is displayed as needed, and will not be displayed when not needed */jsp = new JScrollPane(jta, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);// this.add(jta); this.add(jsp); this.setTitle("Notepad"); this.setSize(800, 600);//Center the settings window to display this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Prohibit the user from changing the form size setResizable(false); this.setVisible(true);}} /** * JScrollPane Panel of java swing* When setting up the interface, you may encounter a larger portion of the content displayed in a smaller container form. You can use the * JScrollPane panel. The JscrollPane panel is a panel with scrollbars and is also a container, but it is often used to arrange a single * control and cannot use the layout manager. If you need to place multiple controls in the JScrollPane panel, you need to place multiple * controls onto the JPanel panel and then add the JPanel panel as a whole control to the JScrollPane control. * * @author gao */package com.gao;import java.awt.BorderLayout;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JScrollPane;import javax.swing.Border.EmptyBorder;public class JScrollPaneDemo extends JFrame{private JPanel contentPane;private JScrollPane scrollPane;private JTextArea textArea;public JScrollPaneDemo(){contentPane=new JPanel();contentPane.setBorder(new EmptyBorder(5,5,5,5));contentPane.setLayout(new BorderLayout(0,0));this.setContentPane(contentPane);scrollPane=new JScrollPane();contentPane.add(scrollPane,BorderLayout.CENTER);textArea=new JTextArea();//scrollPane.add(textArea); scrollPane.setViewportView(textArea); this.setTitle("Scroll Panel Use"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setBounds(100, 100, 250, 200); this.setVisible(true);}public static void main(String []args){JScrollPaneDemo example=new JScrollPaneDemo();}}Effect:
The above is the entire content of this article about the swing component JScrollPane scrollbar instance code. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!