Java common panel JScrollPane scroll panel example details

  • 2020-09-16 07:28:47
  • OfStack

The JScrollPane panel can be used when setting up the interface in a situation where a larger portion of the content is displayed on a smaller container form.

The JScrollPane panel is a panel with a scroll bar and is also a container, but JScrollPane can only hold one component and does not use the layout manager. If you need to place more than one component on the JScrollPane panel, you need to place more than one component on the JPanel, and then add the JPanel panel to the JScrollPane component as a whole. You must pay attention to this! Below we will understand its use method and skill through an example.

Source:


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){
 @SuppressWarnings("unused")
		JscrollPaneDemo example=new JscrollPaneDemo(); 
 }
}

Suggest friends in the process of learning not to copy the code as far as possible, to do it yourself, especially beginners. The first aspect can deepen the understanding, the first aspect can also practice the speed of typing code, as a programmer, not only must have good programming habits and level, but also must have speed.

These are the examples of Java's common panel JScrollPane and some tips for you to learn how to program. If you like it, please stay tuned!


Related articles: