如何使JPanel可滚动? [英] How to make a JPanel scrollable?

查看:108
本文介绍了如何使JPanel可滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JFrame,我想在其中显示一个JInternal Frame,其中包含一个Jpanel.此JPanel包含一些JLabel和JTextField.由于JInternal Frame的大小小于JPanel的大小. JInternal Frame不可调整大小,因此我想使包含表单的jpanel应该可滚动.这是此代码中的演示代码,我没有使用JInternal Frame来简化它.但是,当我执行此代码时,它没有显示滚动条. 请告诉我朋友这段代码有什么问题.我想使用setBounds()方法代替setDimension()方法或setPreferredSize()方法.这样有可能吗?

I have a JFrame and In which I want to show a JInternal Frame whihc contains a Jpanel. This JPanel contains some JLabels and JTextFields. As the size of the JInternal Frame is smaller than the size of the JPanel. The JInternal Frame is not resizable so I want to make the jpanel which contains the form should be scrollable. Here is the demo code of it in this code I have not used the JInternal Frame for making it simple. But when I execute this code its not showing scrollbar. Please tell me friends whats wrong with this code. I want to use setBounds() method instead of setDimension() method or setPreferredSize() method. So is it possible?

这是我的代码

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class InterfaceDesign {

public static void main(String args[]){
    InterfaceDesign id=new InterfaceDesign();
    id.getPnlProjectDetail("My Project");
}

public void getPnlProjectDetail(String strProjectName){
    JPanel pnlProjectDetail=new JPanel();
    JScrollPane scrProjectDetail;
    pnlProjectDetail.setLayout(null);
    pnlProjectDetail.setBounds(0, 0, 400,400);
    JFrame frmtest=new JFrame();
    frmtest.setBounds(1,1,300,200);

    JLabel lblFirstName=new JLabel("First Name");
    JLabel lblLastName=new JLabel("Last Name");
    JLabel lblAddress=new JLabel("Address");
    JLabel lblCity=new JLabel("City");
    JLabel lblZipCode=new JLabel("Zip Code");
    JLabel lblPhone=new JLabel("Phone");
    JLabel lblEmailID=new JLabel("Emain Id");

    JTextField tfFirstName=new JTextField();
    JTextField tfLastName=new JTextField();
    JTextField tfAddress=new JTextField();
    JTextField tfCity=new JTextField();
    JTextField tfZipCode=new JTextField();
    JTextField tfPhone=new JTextField();
    JTextField tfEmailID=new JTextField();

    lblFirstName.setBounds(25, 55, 85, 20);
    tfFirstName.setBounds(25, 85, 85, 20);
    pnlProjectDetail.add(lblFirstName);
    pnlProjectDetail.add(tfFirstName);

    lblLastName.setBounds(25, 115, 85, 20);
    tfLastName.setBounds(25, 145, 85, 20);
    pnlProjectDetail.add(lblLastName);
    pnlProjectDetail.add(tfLastName);

    lblAddress.setBounds(25, 175, 85, 20);
    tfAddress.setBounds(25, 205, 85, 20);
    pnlProjectDetail.add(lblAddress);
    pnlProjectDetail.add(tfAddress);

    lblCity.setBounds(25, 235, 85, 20);
    tfCity.setBounds(25, 265, 85, 20);
    pnlProjectDetail.add(lblCity);
    pnlProjectDetail.add(tfCity);

    lblZipCode.setBounds(25, 295, 85, 20);
    tfZipCode.setBounds(25, 325, 85, 20);
    pnlProjectDetail.add(lblZipCode);
    pnlProjectDetail.add(tfZipCode);

    lblPhone.setBounds(25, 355, 85, 20);
    tfPhone.setBounds(25, 385, 85, 20);
    pnlProjectDetail.add(lblPhone);
    pnlProjectDetail.add(tfPhone);

    lblEmailID.setBounds(25, 415, 85, 20);
    tfEmailID.setBounds(25, 445, 85, 20);
    pnlProjectDetail.add(lblEmailID);
    pnlProjectDetail.add(tfEmailID);

    scrProjectDetail=new JScrollPane(pnlProjectDetail);
    frmtest.setContentPane(scrProjectDetail);
    //frmtest.add(scrProjectDetail);
    frmtest.setVisible(true);
    frmtest.setResizable(false);
    frmtest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //return pnlProjectDetail;      
}


}

请指导我的朋友们! 谢谢!

Please guide me friends! Thanks you!

推荐答案

在JScrollPane中调用面板的setPreferredSize().

Call setPreferredSize() of the panel in the JScrollPane.

但是使用LayoutManager会好得多!

But it's much much better to use LayoutManager!

这篇关于如何使JPanel可滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆