Java GUI: Add JPanel into JScrollPane

Source: Internet
Author: User
Tags stub

Goals to achieve:

Because there are a number of Java GUI components in the scroll box, the JPanel panel is used to wrap these components in a scroll-through-jscrollpane implementation.

Question 1: The layout rubs together

JPanel has its own default layout, so here we have to set the flow layout ourselves

Jpanel_qanda.setlayout (null);
Issue 2: Scroll bar does not take effect

At first I was setting the size of the JPanel directly. setSize (int width, int height) No matter how large the setting is, there is no scroll bar

Finally Baidu, saw a blog: https://www.cnblogs.com/tianguook/archive/2012/03/21/2410807.html

Jpanel_qanda.setpreferredsize (new Dimension (800,1000));

Scroll bar appears!

Issue 3: Component does not display

PS: Today, when writing the GUI, because of carelessness, there is a problem:

Container add component, the runtime does not appear, when the mouse moved to the target location, the component only surfaced

Cause: SetBounds is written in front of the Add method.

Paste all the code, for personal use, a bit messy:

 Public Static voidQandA () {FinalJFrame Frame=menu (NewJFrame ()); User.setnickname ("Zs"); User.setrole (1); List<Qanda> questions=qandadao.getquestions (); JButton Jbutton_ask=NewJButton ("Ask Question");        Frame.add (Jbutton_ask); Jbutton_ask.setbounds (440, 200, 120, 30); Jbutton_ask.addactionlistener (NewActionListener () {@Override Public voidactionperformed (ActionEvent e) {//TODO auto-generated Method Stub                            }        }); JPanel Jpanel_qanda=NewJPanel (); Jpanel_qanda.setpreferredsize (NewDimension (800,150*questions.size ())); Jpanel_qanda.setlayout (NULL);  for(inti = 0; I < questions.size (); i++) {String name=Questions.get (i). GetName (); String Time=Questions.get (i). GetTime (); String content=Questions.get (i). GetContent (); JLabel Jlabel_name=NewJLabel ("Name:" +name);            Jpanel_qanda.add (Jlabel_name); Jlabel_name.setbounds (20+150*i, 60, 20); JLabel Jlabel_time=NewJLabel ("Time:" +Time );            Jpanel_qanda.add (Jlabel_time); Jlabel_time.setbounds (20+150*i, 160, 20); JButton Jbutton_answer=NewJButton ("Answer");            Jpanel_qanda.add (Jbutton_answer); Jbutton_answer.setbounds (420, 20+150*i, 120, 30); Jbutton_answer.addactionlistener (NewActionListener () {@Override Public voidactionperformed (ActionEvent e) {//TODO auto-generated Method Stub                                    }            }); JButton Jbutton_check=NewJButton ("Check");            Jpanel_qanda.add (Jbutton_check); Jbutton_check.setbounds (620, 20+150*i, 120, 30); Jbutton_check.addactionlistener (NewActionListener () {@Override Public voidactionperformed (ActionEvent e) {//TODO auto-generated Method Stub                                    }            }); JTextArea jtextarea_content=NewJTextArea (content);            Jpanel_qanda.add (jtextarea_content); Jtextarea_content.setbounds (50+150*i, 740, 80); } JScrollPane jscrollpane_userinfo=NewJScrollPane (Jpanel_qanda,scrollpaneconstants.vertical_scrollbar_always, Scrollpaneconstants.horizontal        _scrollbar_always);        Frame.add (Jscrollpane_userinfo); Jscrollpane_userinfo.setbounds (100, 250, 800, 200); Jscrollpane_userinfo.setfont (NewFont ("Dialog", 0, 20)); }    /*** User Information*/     Public Static voidUserInfo () {JFrame frame=NewJFrame (); User.setnickname ("Zs"); Frame.setsize (1000,600); Frame.setvisible (true); Frame.setresizable (false); Frame.setlocation ((Toolkit.getdefaulttoolkit (). Getscreensize (). Width-400)/2, (Toolkit.getdefaulttoolkit (). Getscreensize (). Height-320)/2); Frame.settitle ("Notice"); Frame.setlayout (NULL); Frame=menu (frame); List<User> users=userdao.getusers (); String[][] Userinfos=NewString[users.size ()][4];  for(intI=0;i<users.size (); i++) {userinfos[i][0]=Users.get (i). Getnickname (); userinfos[i][1]=Users.get (i). Getaccount (); userinfos[i][2]=Users.get (i). Getemail (); if(Users.get (i). Getrole () ==0) {userinfos[i][3]= "Student"; }Else{userinfos[i][3]= "Teacher"; }} string[] Header=Newstring[]{"Name", "Account", "Email", "Role"}; JTable Jtable_userinfo=NewJTable (Userinfos,header); JScrollPane Jscrollpane_userinfo=NewJScrollPane (Jtable_userinfo,scrollpaneconstants.vertical_scrollbar_always, Scrollpaneconstants.horizon        Tal_scrollbar_always);        Frame.add (Jscrollpane_userinfo); Jscrollpane_userinfo.setbounds (100, 200, 800, 200); Jscrollpane_userinfo.setfont (NewFont ("Dialog", 0, 20)); }

Java GUI: Add JPanel into JScrollPane

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.