JAVA learning Swing chapter JPanel and JScrollPane panel simple learning, jpaneljscrollpane
Package com. swing; import java. awt. container; import java. awt. gridLayout; import javax. swing. JButton; import javax. swing. JFrame; import javax. swing. JPanel; import javax. swing. windowConstants;/*** 1: the commonly used panel * is also a Swing container, which can be viewed as a container to accommodate other components, however, it must also be added to other containers. * commonly used panels in Swing include JPanel panel and JScrollPane panel ** 2: the JPanel can aggregate some components to Layout * the reader should first understand that the Panel is also a container because it inherits java. awt. container class * @ author biexiansheng **/public class JPanelTest extends JFrame {public JPanelTest () {Container container = getContentPane (); // set a container // set the entire container to grid layout manager x with two rows and one column, and y represents the rows and columns of container. setLayout (new GridLayout (,); // initialize a panel and set the grid layout JPanel p1 = new JPanel (new GridLayout (, 10, 10) in one row and three columns )); JPanel p2 = new JPanel (new GridLayout (,); JPanel p3 = new JPanel (new GridLayout )); JPanel p4 = new JPanel (new GridLayout (, 10, 10); // Add the button p1.add (new JButton ("1") in the panel ")); p1.add (new JButton ("2"); p1.add (new JButton ("3"); p2.add (new JButton ("4 ")); p2.add (new JButton ("5"); p3.add (new JButton ("6"); p3.add (new JButton ("7 ")); p4.add (new JButton ("8"); p4.add (new JButton ("9"); // the most important step is to add the Panel instance to the container. add (p1); container. add (p2); container. add (p3); container. add (p4); // instantiate the external features of the container setTitle ("case of JPanel"); setSize (400,250); // set the size width and height of the form setVisible (true ); // set the form visualization // set the form closing method setDefaultCloseOperation (WindowConstants. EXIT_ON_CLOSE);} public static void main (String [] args) {// TODO Auto-generated method stub JPanelTest jt = new JPanelTest ();}}
The running result of the case is as follows:
Package com. swing; import java. awt. container; import javax. swing. JFrame; import javax. swing. JScrollPane; import javax. swing. JTextArea; import javax. swing. windowConstants;/*** 1: when setting the interface, a large part of content may be displayed in a small container form, in this case, you can use the * JScrollPane panel ** 2: The JScrollPane panel is a panel with a scroll bar. It is also a panel, but JScrollPane can only * place one component, you cannot use the layout manager ** 3. If you need to place multiple components in the JScrollPane panel, you need to place multiple components on the JPanel, * Add the JPanel as a whole component to the JScrollPane component. ** 4: from this example, you can create a text editor with a scroll bar in the form. First, you need to initialize the editor. * The size of the compiler is specified during initialization, when creating a panel with a scroll bar, add the compiler to the Panel *, finally, place the compiler with a scroll bar in the container. * @ author biexiansheng **/public class JScrollPaneTest extends JFrame {// The shortcut key for importing the package shift + ctrl + o public JScrollPaneTest () {// define a constructor Container = getContentPane (); // create a container // create a text area component JTextArea ta = new JTextArea (20, 50); // create JScrollPane () panel object, and add the text domain object to the Panel JScrollPane sp = new JScrollPane (ta); // Add the panel to the container. add (sp); // setTitle ("text editor with scroll bar") set the external characteristics of the container; // set the title text setSize (400,400) of the window ); // set the window size setVisible (true); // set the visualization // set the window closing method setdefaclocloseoperation (WindowConstants. EXIT_ON_CLOSE);} public static void main (String [] args) {// TODO Auto-generated method stub JScrollPaneTest jp = new JScrollPaneTest ();}}
The running result of the case is as follows: