Java GUI:將JPanel添加進JScrollPane

來源:互聯網
上載者:User

標籤:log   action   www   else   問題   idt   使用者資訊   ide   role   

實現的目標:

因為在捲動方塊中含有很多個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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.