为JFrame创建滚动条 [英] Create Scroll Bar for JFrame

查看:140
本文介绍了为JFrame创建滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的JFrame创建滚动条.我创建了JPanel对象,并将组件设置为JPanel.然后为面板创建一个JScrollPane对象.然后将ScrollPane对象添加到JFrame.我没有看到任何滚动条.我也想知道JPanel中是否有一个选项可以根据JPanel的缩放级别自动调整Jpanel中对象的大小.任何帮助将不胜感激.

Hi I am trying to create Scroll Bar for my JFrame. I created JPanel object and set components into JPanel. Then created a JScrollPane object for the panel. Then add the ScrollPane object to JFrame. I am not seeing any scrollbar. Also I am wondering if there is a option in JPanel that would resize the object inside Jpanel automatically according to the zoom level of the JPanel. Any help would be highly appreciated.

公共类xmlottgui {

public class xmlottgui {

private JPanel Container;

private JFrame frmCreateXml;

private JTextField titlename;
private JLabel lbltitlename;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                xmlottgui window = new xmlottgui();
                window.frmCreateXml.setVisible(true);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public xmlottgui() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {

    Container = new JPanel();
    Container.setLayout(null);

    //JScrollPane pane=new JScrollPane(Container,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);



    frmCreateXml = new JFrame();
    frmCreateXml.setTitle("Create XML");
    frmCreateXml.setBounds(100, 100, 1000, 1200);
    frmCreateXml.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frmCreateXml.getContentPane().setLayout(null);

    //Create MenuBar
    JMenuBar menuBar = new JMenuBar();
    Container.add(menuBar);

    JMenu mnFile = new JMenu("File");
    menuBar.add(mnFile);

    JMenuItem mntmImportFromCsv = new JMenuItem("Import From Excel File");  
    //Add menu item Exit
    JMenuItem mntmexit = new JMenuItem("Exit");
    mntmexit.addActionListener(new ActionListener() { 
        public void actionPerformed(ActionEvent event) {
            System.exit(0);
        } 
    }); 
    mnFile.add(mntmexit);

    showform();

    JScrollPane pane=new JScrollPane(Container,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    pane.setLayout(null);
    frmCreateXml.setContentPane(pane);
    frmCreateXml.getContentPane().add(pane);

}

private void showform(){

    titlename = new JTextField();
    titlename.setBounds(164, 27, 749, 26);
    Container.add(titlename);
    titlename.setColumns(10);

    lbltitlename = new JLabel("Title Name");
    lbltitlename.setBackground(Color.GRAY);
    lbltitlename.setBounds(22, 1000, 90, 16);
    Container.add(lbltitlename);        
}

推荐答案

此:

pane.setLayout(null);

将完全禁用JScrollPane并阻止其运行,因为这将阻止JScrollPane正确显示和操纵其视口. JScrollPanes那里有一个非常特殊的布局管理器,除非您非常聪明并且知道自己在做什么,否则您永远不会想与它混为一谈.作为一般规则,您几乎永远不应使用空布局.

Will completely disable your JScrollPane and prevent it from working as it will prevent the JScrollPane from displaying and manipulating its view port properly. JScrollPanes have there own very special layout manager, one you never want to muck with unless you are very clever and know what you're doing. As a general rule you should almost never use null layouts.

这也不正确:

  frmCreateXml.setContentPane(pane);
  frmCreateXml.getContentPane().add(pane);

您使窗格成为contentPane,然后向其自身添加窗格.

You make pane the contentPane and then add pane to itself.

这让你很烦:

frmCreateXml.getContentPane().setLayout(null);

您将要学习和使用布局管理器,因为它会使您的生活更加轻松.

You will want to learn about and use the layout managers as it will make your life much easier.

这篇关于为JFrame创建滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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