JavaGUI-JTextArea扩展但不收缩

JavaGUI-JTextArea扩展但不收缩,java,swing,jpanel,jscrollpane,Java,Swing,Jpanel,Jscrollpane,主要问题:在带有JPanel的JScrollPane中,包含一个JTextArea,如果GUI被扩展,文本将自动换行,但当GUI被收缩时,文本不会自动换行。见下面的例子 好的,我正在为我目前正在开发的一个应用程序构建GUI,我有点问题。 说明:我的GUI结构如下所示: 这就是它看起来的样子。 展开后,panelWithText内的JTextArea会展开文本并调整其大小: 但问题是,当您将GUI变小时会发生什么。问题是,我希望文本像以前一样向后扭曲。我对JScrollPane和panelW

主要问题:在带有JPanel的JScrollPane中,包含一个JTextArea,如果GUI被扩展,文本将自动换行,但当GUI被收缩时,文本不会自动换行。见下面的例子

好的,我正在为我目前正在开发的一个应用程序构建GUI,我有点问题。 说明:我的GUI结构如下所示:

这就是它看起来的样子。

展开后,panelWithText内的JTextArea会展开文本并调整其大小:

但问题是,当您将GUI变小时会发生什么。问题是,我希望文本像以前一样向后扭曲。我对JScrollPane和panelWithText都实现了一个ComponentListener,进行了一些实验,发现ComponentResistized是在展开时为panelWithText调用的,而不是收缩时为panelWithText调用的。有没有办法在panelWithText组件中实现文本向后扭曲的行为? PS:显然,如果我用常规JPanel切换JScrollPane,它就会工作。但我不能那样做!我有很多带有文本的面板要显示给用户。 对不起,这是我正在使用的代码。 JFrameExt.java

panelWithText.java


在玩了一会儿之后,我想到了这个

public class Test {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    JFrameExt frame = new JFrameExt();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public static class ScrollablePane extends JPanel implements Scrollable {

        @Override
        public Dimension getPreferredScrollableViewportSize() {
            return new Dimension(100, 100);
        }

        @Override
        public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
            return 64;
        }

        @Override
        public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
            return 128;
        }

        @Override
        public boolean getScrollableTracksViewportWidth() {
            return true;
        }

        @Override
        public boolean getScrollableTracksViewportHeight() {
            return false;
        }

    }

    public static class JFrameExt extends JFrame {

        private JPanel contentPane;

        /**
         * Create the frame.
         */
        public JFrameExt() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 246, 164);
            contentPane = new JPanel();
            contentPane.setBorder(null);
            contentPane.setLayout(new BorderLayout(0, 0));
            setContentPane(contentPane);

            JScrollPane scrollPane = new JScrollPane();
            scrollPane.setViewportBorder(null);
            scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
            contentPane.add(scrollPane, BorderLayout.CENTER);

            JPanel panel = new ScrollablePane();
            scrollPane.setViewportView(panel);
            GridBagLayout gbl_panel = new GridBagLayout();
//            gbl_panel.columnWidths = new int[]{0, 0};
//            gbl_panel.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0};
//            gbl_panel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
//            gbl_panel.rowWeights = new double[]{0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, Double.MIN_VALUE};
            panel.setLayout(gbl_panel);

            panelWithText panelWithText_ = new panelWithText();
            GridBagConstraints gbc_panelWithText_ = new GridBagConstraints();
            gbc_panelWithText_.anchor = GridBagConstraints.NORTH;
            gbc_panelWithText_.insets = new Insets(0, 0, 5, 0);
            gbc_panelWithText_.fill = GridBagConstraints.HORIZONTAL;
            gbc_panelWithText_.gridx = 0;
            gbc_panelWithText_.gridy = 0;
            gbc_panelWithText_.weightx = 1;
            panel.add(panelWithText_, gbc_panelWithText_);

//            panelWithText panelWithText__1 = new panelWithText();
//            GridBagConstraints gbc_panelWithText__1 = new GridBagConstraints();
//            gbc_panelWithText__1.insets = new Insets(0, 0, 5, 0);
//            gbc_panelWithText__1.anchor = GridBagConstraints.NORTH;
////            gbc_panelWithText__1.fill = GridBagConstraints.HORIZONTAL;
//            gbc_panelWithText__1.gridx = 0;
//            gbc_panelWithText__1.gridy = 1;
//            panel.add(panelWithText__1, gbc_panelWithText__1);
//
//            panelWithText panelWithText__2 = new panelWithText();
//            GridBagConstraints gbc_panelWithText__2 = new GridBagConstraints();
//            gbc_panelWithText__2.insets = new Insets(0, 0, 5, 0);
////            gbc_panelWithText__2.fill = GridBagConstraints.BOTH;
//            gbc_panelWithText__2.gridx = 0;
//            gbc_panelWithText__2.gridy = 2;
//            panel.add(panelWithText__2, gbc_panelWithText__2);
//
//            panelWithText panelWithText__3 = new panelWithText();
//            GridBagConstraints gbc_panelWithText__3 = new GridBagConstraints();
//            gbc_panelWithText__3.insets = new Insets(0, 0, 5, 0);
////            gbc_panelWithText__3.fill = GridBagConstraints.BOTH;
//            gbc_panelWithText__3.gridx = 0;
//            gbc_panelWithText__3.gridy = 3;
//            panel.add(panelWithText__3, gbc_panelWithText__3);
//
//            panelWithText panelWithText__4 = new panelWithText();
//            GridBagConstraints gbc_panelWithText__4 = new GridBagConstraints();
//            gbc_panelWithText__4.insets = new Insets(0, 0, 5, 0);
////            gbc_panelWithText__4.fill = GridBagConstraints.BOTH;
//            gbc_panelWithText__4.gridx = 0;
//            gbc_panelWithText__4.gridy = 4;
//            panel.add(panelWithText__4, gbc_panelWithText__4);
//
//            panelWithText panelWithText__5 = new panelWithText();
//            GridBagConstraints gbc_panelWithText__5 = new GridBagConstraints();
//            gbc_panelWithText__5.insets = new Insets(0, 0, 5, 0);
////            gbc_panelWithText__5.fill = GridBagConstraints.BOTH;
//            gbc_panelWithText__5.gridx = 0;
//            gbc_panelWithText__5.gridy = 5;
//            panel.add(panelWithText__5, gbc_panelWithText__5);
//
//            panelWithText panelWithText__6 = new panelWithText();
//            GridBagConstraints gbc_panelWithText__6 = new GridBagConstraints();
////            gbc_panelWithText__6.fill = GridBagConstraints.BOTH;
//            gbc_panelWithText__6.gridx = 0;
//            gbc_panelWithText__6.gridy = 6;
//            panel.add(panelWithText__6, gbc_panelWithText__6);
            setSize(300, 100);
        }
    }

    public static class panelWithText extends JPanel {

        /**
         * Create the panel.
         */
        public void me_resized(Dimension d) {
            System.out.println("CALLED..");
            super.setPreferredSize(d);
        }

        public panelWithText() {
            setBackground(Color.DARK_GRAY);
            setForeground(Color.WHITE);
            setLayout(new BorderLayout(0, 0));

            JTextArea txtrIveBeenReading = new JTextArea();
            txtrIveBeenReading.setEditable(false);
            txtrIveBeenReading.setColumns(28);
            txtrIveBeenReading.setFont(new Font("Tahoma", Font.PLAIN, 10));
            txtrIveBeenReading.setLineWrap(true);
            txtrIveBeenReading.setWrapStyleWord(true);
            txtrIveBeenReading.setText("\n A bunch of really important text here... A bunch of really important text here... A bunch of really important text here... A bunch of really important text here...\n");
            txtrIveBeenReading.setForeground(Color.WHITE);
            txtrIveBeenReading.setBackground(Color.DARK_GRAY);
            add(txtrIveBeenReading, BorderLayout.CENTER);

        }
    }
}
基本上,您需要一个实现可滚动界面的容器。这将允许您指定容器应跟踪/匹配视口宽度。这将导致在查看端口改变大小时放置容器


作为旁注,您可以使用GridBagConstraints的单个副本,当您将每个新组件添加到容器中时,GridBagLayout将生成它自己的副本。当您想要在组件之间共享GridBagConstraints的属性时,这是非常强大的

玩了一会儿之后,我想到了这个

public class Test {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    JFrameExt frame = new JFrameExt();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public static class ScrollablePane extends JPanel implements Scrollable {

        @Override
        public Dimension getPreferredScrollableViewportSize() {
            return new Dimension(100, 100);
        }

        @Override
        public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
            return 64;
        }

        @Override
        public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
            return 128;
        }

        @Override
        public boolean getScrollableTracksViewportWidth() {
            return true;
        }

        @Override
        public boolean getScrollableTracksViewportHeight() {
            return false;
        }

    }

    public static class JFrameExt extends JFrame {

        private JPanel contentPane;

        /**
         * Create the frame.
         */
        public JFrameExt() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 246, 164);
            contentPane = new JPanel();
            contentPane.setBorder(null);
            contentPane.setLayout(new BorderLayout(0, 0));
            setContentPane(contentPane);

            JScrollPane scrollPane = new JScrollPane();
            scrollPane.setViewportBorder(null);
            scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
            contentPane.add(scrollPane, BorderLayout.CENTER);

            JPanel panel = new ScrollablePane();
            scrollPane.setViewportView(panel);
            GridBagLayout gbl_panel = new GridBagLayout();
//            gbl_panel.columnWidths = new int[]{0, 0};
//            gbl_panel.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0};
//            gbl_panel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
//            gbl_panel.rowWeights = new double[]{0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, Double.MIN_VALUE};
            panel.setLayout(gbl_panel);

            panelWithText panelWithText_ = new panelWithText();
            GridBagConstraints gbc_panelWithText_ = new GridBagConstraints();
            gbc_panelWithText_.anchor = GridBagConstraints.NORTH;
            gbc_panelWithText_.insets = new Insets(0, 0, 5, 0);
            gbc_panelWithText_.fill = GridBagConstraints.HORIZONTAL;
            gbc_panelWithText_.gridx = 0;
            gbc_panelWithText_.gridy = 0;
            gbc_panelWithText_.weightx = 1;
            panel.add(panelWithText_, gbc_panelWithText_);

//            panelWithText panelWithText__1 = new panelWithText();
//            GridBagConstraints gbc_panelWithText__1 = new GridBagConstraints();
//            gbc_panelWithText__1.insets = new Insets(0, 0, 5, 0);
//            gbc_panelWithText__1.anchor = GridBagConstraints.NORTH;
////            gbc_panelWithText__1.fill = GridBagConstraints.HORIZONTAL;
//            gbc_panelWithText__1.gridx = 0;
//            gbc_panelWithText__1.gridy = 1;
//            panel.add(panelWithText__1, gbc_panelWithText__1);
//
//            panelWithText panelWithText__2 = new panelWithText();
//            GridBagConstraints gbc_panelWithText__2 = new GridBagConstraints();
//            gbc_panelWithText__2.insets = new Insets(0, 0, 5, 0);
////            gbc_panelWithText__2.fill = GridBagConstraints.BOTH;
//            gbc_panelWithText__2.gridx = 0;
//            gbc_panelWithText__2.gridy = 2;
//            panel.add(panelWithText__2, gbc_panelWithText__2);
//
//            panelWithText panelWithText__3 = new panelWithText();
//            GridBagConstraints gbc_panelWithText__3 = new GridBagConstraints();
//            gbc_panelWithText__3.insets = new Insets(0, 0, 5, 0);
////            gbc_panelWithText__3.fill = GridBagConstraints.BOTH;
//            gbc_panelWithText__3.gridx = 0;
//            gbc_panelWithText__3.gridy = 3;
//            panel.add(panelWithText__3, gbc_panelWithText__3);
//
//            panelWithText panelWithText__4 = new panelWithText();
//            GridBagConstraints gbc_panelWithText__4 = new GridBagConstraints();
//            gbc_panelWithText__4.insets = new Insets(0, 0, 5, 0);
////            gbc_panelWithText__4.fill = GridBagConstraints.BOTH;
//            gbc_panelWithText__4.gridx = 0;
//            gbc_panelWithText__4.gridy = 4;
//            panel.add(panelWithText__4, gbc_panelWithText__4);
//
//            panelWithText panelWithText__5 = new panelWithText();
//            GridBagConstraints gbc_panelWithText__5 = new GridBagConstraints();
//            gbc_panelWithText__5.insets = new Insets(0, 0, 5, 0);
////            gbc_panelWithText__5.fill = GridBagConstraints.BOTH;
//            gbc_panelWithText__5.gridx = 0;
//            gbc_panelWithText__5.gridy = 5;
//            panel.add(panelWithText__5, gbc_panelWithText__5);
//
//            panelWithText panelWithText__6 = new panelWithText();
//            GridBagConstraints gbc_panelWithText__6 = new GridBagConstraints();
////            gbc_panelWithText__6.fill = GridBagConstraints.BOTH;
//            gbc_panelWithText__6.gridx = 0;
//            gbc_panelWithText__6.gridy = 6;
//            panel.add(panelWithText__6, gbc_panelWithText__6);
            setSize(300, 100);
        }
    }

    public static class panelWithText extends JPanel {

        /**
         * Create the panel.
         */
        public void me_resized(Dimension d) {
            System.out.println("CALLED..");
            super.setPreferredSize(d);
        }

        public panelWithText() {
            setBackground(Color.DARK_GRAY);
            setForeground(Color.WHITE);
            setLayout(new BorderLayout(0, 0));

            JTextArea txtrIveBeenReading = new JTextArea();
            txtrIveBeenReading.setEditable(false);
            txtrIveBeenReading.setColumns(28);
            txtrIveBeenReading.setFont(new Font("Tahoma", Font.PLAIN, 10));
            txtrIveBeenReading.setLineWrap(true);
            txtrIveBeenReading.setWrapStyleWord(true);
            txtrIveBeenReading.setText("\n A bunch of really important text here... A bunch of really important text here... A bunch of really important text here... A bunch of really important text here...\n");
            txtrIveBeenReading.setForeground(Color.WHITE);
            txtrIveBeenReading.setBackground(Color.DARK_GRAY);
            add(txtrIveBeenReading, BorderLayout.CENTER);

        }
    }
}
基本上,您需要一个实现可滚动界面的容器。这将允许您指定容器应跟踪/匹配视口宽度。这将导致在查看端口改变大小时放置容器


作为旁注,您可以使用GridBagConstraints的单个副本,当您将每个新组件添加到容器中时,GridBagLayout将生成它自己的副本。当您想要在组件之间共享GridBagConstraints的属性时,这是非常强大的

为了更快地获得更好的帮助,请发布一个。如果确实必须这样做,请将DocumentListener添加到JTextArea,并在发生更改时,使父容器无效,强制其放弃布局。您可以通过使其无效来扩展您的意思吗?你还有什么建议吗?我看到了一个使用FlowLayout的解决方案,但扩展了它,使其在大小更改时垂直扩展。似乎编码太多了,我真的必须很快完成此应用。@user1000232抱歉,误解了问题,认为您正在编辑文本:p为了更快地获得帮助,请发布一个。如果确实必须这样做,请将DocumentListener添加到JTextArea,并在更改时使父容器无效,强迫它放弃它的布局,你能进一步说明你所说的使它失效的意思吗?你还有什么建议吗?我看到了一个使用FlowLayout的解决方案,但扩展了它,使其在大小更改时垂直扩展。似乎编码太多了,我真的必须尽快完成这个应用程序。@user1000232抱歉,误解了问题,以为你在编辑文本:PFunny我找到了相同的解决方案。除了我决定用盒子布局…我不想要像盒子一样重的东西grid@user1000232你应该看看SwingLabs SwingX图书馆的垂直布局。我发现了同样的解决方案。除了我决定用盒子布局…我不想要像盒子一样重的东西grid@user1000232您应该看看SwingLabs SwingX库中的垂直布局
public class Test {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    JFrameExt frame = new JFrameExt();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public static class ScrollablePane extends JPanel implements Scrollable {

        @Override
        public Dimension getPreferredScrollableViewportSize() {
            return new Dimension(100, 100);
        }

        @Override
        public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
            return 64;
        }

        @Override
        public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
            return 128;
        }

        @Override
        public boolean getScrollableTracksViewportWidth() {
            return true;
        }

        @Override
        public boolean getScrollableTracksViewportHeight() {
            return false;
        }

    }

    public static class JFrameExt extends JFrame {

        private JPanel contentPane;

        /**
         * Create the frame.
         */
        public JFrameExt() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 246, 164);
            contentPane = new JPanel();
            contentPane.setBorder(null);
            contentPane.setLayout(new BorderLayout(0, 0));
            setContentPane(contentPane);

            JScrollPane scrollPane = new JScrollPane();
            scrollPane.setViewportBorder(null);
            scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
            contentPane.add(scrollPane, BorderLayout.CENTER);

            JPanel panel = new ScrollablePane();
            scrollPane.setViewportView(panel);
            GridBagLayout gbl_panel = new GridBagLayout();
//            gbl_panel.columnWidths = new int[]{0, 0};
//            gbl_panel.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0};
//            gbl_panel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
//            gbl_panel.rowWeights = new double[]{0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, Double.MIN_VALUE};
            panel.setLayout(gbl_panel);

            panelWithText panelWithText_ = new panelWithText();
            GridBagConstraints gbc_panelWithText_ = new GridBagConstraints();
            gbc_panelWithText_.anchor = GridBagConstraints.NORTH;
            gbc_panelWithText_.insets = new Insets(0, 0, 5, 0);
            gbc_panelWithText_.fill = GridBagConstraints.HORIZONTAL;
            gbc_panelWithText_.gridx = 0;
            gbc_panelWithText_.gridy = 0;
            gbc_panelWithText_.weightx = 1;
            panel.add(panelWithText_, gbc_panelWithText_);

//            panelWithText panelWithText__1 = new panelWithText();
//            GridBagConstraints gbc_panelWithText__1 = new GridBagConstraints();
//            gbc_panelWithText__1.insets = new Insets(0, 0, 5, 0);
//            gbc_panelWithText__1.anchor = GridBagConstraints.NORTH;
////            gbc_panelWithText__1.fill = GridBagConstraints.HORIZONTAL;
//            gbc_panelWithText__1.gridx = 0;
//            gbc_panelWithText__1.gridy = 1;
//            panel.add(panelWithText__1, gbc_panelWithText__1);
//
//            panelWithText panelWithText__2 = new panelWithText();
//            GridBagConstraints gbc_panelWithText__2 = new GridBagConstraints();
//            gbc_panelWithText__2.insets = new Insets(0, 0, 5, 0);
////            gbc_panelWithText__2.fill = GridBagConstraints.BOTH;
//            gbc_panelWithText__2.gridx = 0;
//            gbc_panelWithText__2.gridy = 2;
//            panel.add(panelWithText__2, gbc_panelWithText__2);
//
//            panelWithText panelWithText__3 = new panelWithText();
//            GridBagConstraints gbc_panelWithText__3 = new GridBagConstraints();
//            gbc_panelWithText__3.insets = new Insets(0, 0, 5, 0);
////            gbc_panelWithText__3.fill = GridBagConstraints.BOTH;
//            gbc_panelWithText__3.gridx = 0;
//            gbc_panelWithText__3.gridy = 3;
//            panel.add(panelWithText__3, gbc_panelWithText__3);
//
//            panelWithText panelWithText__4 = new panelWithText();
//            GridBagConstraints gbc_panelWithText__4 = new GridBagConstraints();
//            gbc_panelWithText__4.insets = new Insets(0, 0, 5, 0);
////            gbc_panelWithText__4.fill = GridBagConstraints.BOTH;
//            gbc_panelWithText__4.gridx = 0;
//            gbc_panelWithText__4.gridy = 4;
//            panel.add(panelWithText__4, gbc_panelWithText__4);
//
//            panelWithText panelWithText__5 = new panelWithText();
//            GridBagConstraints gbc_panelWithText__5 = new GridBagConstraints();
//            gbc_panelWithText__5.insets = new Insets(0, 0, 5, 0);
////            gbc_panelWithText__5.fill = GridBagConstraints.BOTH;
//            gbc_panelWithText__5.gridx = 0;
//            gbc_panelWithText__5.gridy = 5;
//            panel.add(panelWithText__5, gbc_panelWithText__5);
//
//            panelWithText panelWithText__6 = new panelWithText();
//            GridBagConstraints gbc_panelWithText__6 = new GridBagConstraints();
////            gbc_panelWithText__6.fill = GridBagConstraints.BOTH;
//            gbc_panelWithText__6.gridx = 0;
//            gbc_panelWithText__6.gridy = 6;
//            panel.add(panelWithText__6, gbc_panelWithText__6);
            setSize(300, 100);
        }
    }

    public static class panelWithText extends JPanel {

        /**
         * Create the panel.
         */
        public void me_resized(Dimension d) {
            System.out.println("CALLED..");
            super.setPreferredSize(d);
        }

        public panelWithText() {
            setBackground(Color.DARK_GRAY);
            setForeground(Color.WHITE);
            setLayout(new BorderLayout(0, 0));

            JTextArea txtrIveBeenReading = new JTextArea();
            txtrIveBeenReading.setEditable(false);
            txtrIveBeenReading.setColumns(28);
            txtrIveBeenReading.setFont(new Font("Tahoma", Font.PLAIN, 10));
            txtrIveBeenReading.setLineWrap(true);
            txtrIveBeenReading.setWrapStyleWord(true);
            txtrIveBeenReading.setText("\n A bunch of really important text here... A bunch of really important text here... A bunch of really important text here... A bunch of really important text here...\n");
            txtrIveBeenReading.setForeground(Color.WHITE);
            txtrIveBeenReading.setBackground(Color.DARK_GRAY);
            add(txtrIveBeenReading, BorderLayout.CENTER);

        }
    }
}