多个JTextPane(Java)的滚动条

时间:2014-08-06 21:41:04

标签: java scroll jframe jscrollpane jtextpane

附上我的窗口代码:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import java.awt.BorderLayout;
import javax.swing.JTextPane;
public class Window extends JFrame{
    private JPanel panel;
    private JTextPane textPane;
    private JTextPane textPane_1;   
    public Window() {
        super("Window");
        this.init();
        this.setSize(800, 600);
        this.setVisible(true);
    }
    void init(){
        panel = new JPanel();
        getContentPane().add(panel, BorderLayout.CENTER);
        panel.setLayout(null);      
        textPane = new JTextPane();
        textPane.setBounds(6, 48, 788, 185);
        panel.add(textPane);
        textPane.setFocusable(true);        
        textPane_1 = new JTextPane();
        textPane_1.setBounds(6, 346, 788, 185);
        panel.add(textPane_1);      
        JScrollPane scroll1 = new JScrollPane(textPane, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scroll1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        scroll1.setViewportView(textPane);
        panel.add(scroll1);
        this.add(scroll1);      
        JScrollPane scroll2 = new JScrollPane(textPane_1, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scroll2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        scroll2.setViewportView(textPane);
        panel.add(scroll2);
        this.add(scroll2);      
        this.add(panel);
    }
}

我的目标是让两个JTextPanes都有自己的滚动条。屏幕上显示的只是一个JTextPane(不确定哪一个),它只有一个垂直滚动条(我认为这是因为JTextPanes有自动换行)。第二个JTextPane没有显示出来。任何人都可以帮助我吗?

感谢所有回复的人。

2 个答案:

答案 0 :(得分:2)

在这种情况下,您可以使用GridLayout。请参阅How to Use GridLayout

上的 Swing Tutorial

以下是包含GridLayout的代码以及内联注释。

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;

public class MyWindow extends JFrame {
    private JPanel panel;
    private JTextPane textPane;
    private JTextPane textPane_1;

    public MyWindow() {
        super("Window");
        this.init();
    }

    void init() {
        // panel with GridLayout having 2 rows and 1 column
        panel = new JPanel(new GridLayout(2,1)); 

        textPane = new JTextPane();
        // enclose the text pane inside the scroll pane
        // scroll pane shows scrollbars when needed
        JScrollPane scroll1 = new JScrollPane(textPane);
        // add scroll pane at first column of the first row
        // never add text pane again in the panel because 
        //  it's already added in scroll pane
        panel.add(scroll1);

        textPane_1 = new JTextPane();
        JScrollPane scroll2 = new JScrollPane(textPane_1);
        // add scroll pane  at first column of the second row
        panel.add(scroll2);

        // finally add the panel in the JFrame's content pane in the center
        getContentPane().add(panel, BorderLayout.CENTER);
    }

    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable(){

            @Override
            public void run() {
                MyWindow window=new MyWindow();
                window.setVisible(true);
            }
        });
    }
}

快照:

enter image description here


请查看How to Use Various Layout Managers

上的 Swing Tutorial

答案 1 :(得分:1)

首先,JFrame默认使用BorderLayoutBorderLayout只允许单个组件占据其五个可用位置中的任何一个。其次,组件只能属于单个父母

看看你的代码......

    scroll1.setViewportView(textPane);
    panel.add(scroll1);
    this.add(scroll1);      
    //...
    scroll2.setViewportView(textPane);
    panel.add(scroll2);
    this.add(scroll2);      
    this.add(panel);
  1. 您将textPane设置为scroll1
  2. 的视口视图
  3. 您将scroll1添加到panel
  4. 您将scroll添加到this,有效地将其从panel移除......
  5. 您将textPane设置为scroll2的视口视图,将其从scroll1
  6. 的视口中移除
  7. 您将scroll2添加到panel
  8. 您向此添加scroll2,有效地将其从panel ...
  9. 中删除
  10. 您向此添加panel,覆盖之前添加到框架中的所有内容......
  11. 这实际上意味着panelBorderLayout尝试在框架上布局的唯一可见组件,但它不包含任何内容

    相反,您可以在将每个滚动窗格添加到框架时为每个滚动窗格指定一个位置,例如..

        scroll1.setViewportView(textPane);
        this.add(scroll1, BorderLayout.NORTH);      
        //...
        scroll2.setViewportView(textPane_1);
        this.add(scroll2, BorderLayout.SOUTH);      
    

    更新了工作示例

    Does Work

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextPane;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class Window extends JFrame {
    
        private JTextPane textPane;
        private JTextPane textPane_1;
    
        public Window() {
            super("Window");
            this.init();
            this.setSize(800, 600);
            this.setVisible(true);
        }
    
        void init() {
            textPane = new JTextPane();
            textPane_1 = new JTextPane();
            JScrollPane scroll1 = new JScrollPane(textPane);
            scroll1.setViewportView(textPane);
            JScrollPane scroll2 = new JScrollPane(textPane_1);
            add(scroll1, BorderLayout.NORTH);
            add(scroll2, BorderLayout.SOUTH);
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    Window frame = new Window();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    }
    

    这是JTextPane的默认行为,其总体大小取决于其内容的大小......

    现在,您可以使用Scrollable界面并指定" initial",PreferredScrollableViewportSize,向滚动窗格建议您的组件想要使用多少空间。

    幸运的是,JTextPane已经实现了此界面,因此您只需要覆盖getPreferredScrollableViewportSize方法,例如...

    Does Work

    textPane = new JTextPane() {
        @Override
        public Dimension getPreferredScrollableViewportSize() {
            return new Dimension(200, 200);
        }
    };
    textPane_1 = new JTextPane() {
        @Override
        public Dimension getPreferredScrollableViewportSize() {
            return new Dimension(200, 200);
        }
    };
    

    请查看Laying Out Components Within a Container了解详情

    避免使用null布局,像素完美布局是现代ui设计中的一种幻觉。影响组件个体大小的因素太多,您无法控制。 Swing旨在与布局管理器一起工作,放弃这些将导致问题和问题的终结,您将花费越来越多的时间来纠正