1. 程式人生 > >Java GUI:將JPanel添加進JScrollPane

Java GUI:將JPanel添加進JScrollPane

tco eache jtextarea 因此 add lse screen 滾動條 ner

實現的目標:

技術分享圖片

因為在滾動框中含有很多個Java GUI 組件,因此這裏采用JPanel面板包住這些組件,在用JScrollPane實現滾動

問題1:布局揉在一起

JPanel有自己默認的布局方式,因此在這裏我們要自己設置流式布局

jPanel_qanda.setLayout(null);

問題2:滾動條未生效

剛開始的時候我是直接設置JPanel的大小 setSize(int width, int height) 不管設置多大,都沒有滾動條

最後百度,看到了一個博客:https://www.cnblogs.com/tianguook/archive/2012/03/21/2410807.html

jPanel_qanda.setPreferredSize(new Dimension(800,1000));

滾動條出現!

問題3:組件不顯示

PS:今天在寫GUI的時候,因為粗心,出現了一個問題:

容器add組件後,運行時不出現,當鼠標移動到目標位置時,組件才浮現出來

原因:setBounds 寫在了 add方法的前面去了

貼一下所有代碼,自用的,有點亂:

  public static void qanda(){
        final JFrame frame=menu(new JFrame());
        user.setNickname("zs");
        user.setRole(
1); List<Qanda> questions=qandaDao.getQuestions(); JButton jButton_ask=new JButton("Ask Question"); frame.add(jButton_ask); jButton_ask.setBounds(440, 200, 120, 30); jButton_ask.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub } }); JPanel jPanel_qanda=new JPanel(); jPanel_qanda.setPreferredSize(new Dimension(800,150*questions.size())); jPanel_qanda.setLayout(null); for (int i = 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=new JLabel("Name:"+name); jPanel_qanda.add(jLabel_name); jLabel_name.setBounds(20, 20+150*i, 60, 20); JLabel jLabel_time=new JLabel("Time:"+time); jPanel_qanda.add(jLabel_time); jLabel_time.setBounds(220, 20+150*i, 160, 20); JButton jButton_answer=new JButton("Answer"); jPanel_qanda.add(jButton_answer); jButton_answer.setBounds(420, 20+150*i, 120, 30); jButton_answer.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub } }); JButton jButton_check=new JButton("Check"); jPanel_qanda.add(jButton_check); jButton_check.setBounds(620, 20+150*i, 120, 30); jButton_check.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub } }); JTextArea jTextArea_content=new JTextArea(content); jPanel_qanda.add(jTextArea_content); jTextArea_content.setBounds(20, 50+150*i, 740, 80); } JScrollPane jScrollPane_userInfo=new JScrollPane(jPanel_qanda,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); frame.add(jScrollPane_userInfo); jScrollPane_userInfo.setBounds(100, 250, 800, 200); jScrollPane_userInfo.setFont(new Font("Dialog", 0, 20)); } /** * 用戶信息 */ public static void userInfo(){ JFrame frame=new JFrame(); 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=new String[users.size()][4]; for(int i=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=new String[]{"Name","Account","Email","Role"}; JTable jTable_userInfo=new JTable(userInfos,header); JScrollPane jScrollPane_userInfo=new JScrollPane(jTable_userInfo,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); frame.add(jScrollPane_userInfo); jScrollPane_userInfo.setBounds(100, 200, 800, 200); jScrollPane_userInfo.setFont(new Font("Dialog", 0, 20)); }

Java GUI:將JPanel添加進JScrollPane