Java GUI:将JPanel添加进JScrollPane

2021-07-04 23:07

阅读:767

标签: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 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 ) {
            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 users=userDao.getUsers();
        String[][] userInfos=new String[users.size()][4];
        for(int i=0;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

标签:log   action   www   else   问题   idt   用户信息   ide   role   

原文地址:https://www.cnblogs.com/fdzang/p/9601010.html


评论


亲,登录后才可以留言!