JInternalFrames中的JScrollPanes用于从右到左的组件方向-Java错误? [英] JScrollPanes in JInternalFrames for right to left component orientations - Java bug?

查看:59
本文介绍了JInternalFrames中的JScrollPanes用于从右到左的组件方向-Java错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我创建了一些演示代码,请参见下文.

我看到的是,如果JScrollPane在JInternalFrame中并且组件方向设置为从右到左,则在最小化框架时,滚动条将停留在内容的左侧.我希望看到的是RtL,它将保留在内容的右侧,如果滚动窗格未添加到内部框架(请参见两个框架-在演示中一个出现在另一个框架之后),则为true.

这是Java错误还是我忘了做某事?

这是演示代码:

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import java.awt.ComponentOrientation;
import java.awt.Dimension;


public class JScrollBarTest
{
    public static void main(String[] a)
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e) { }

        runInternalFrameDemo();
        runNormalDemo();
    }

    private static void runInternalFrameDemo()
    {
        // Frame...
        final JFrame frame = new JFrame("Internal Frame Demo");
        frame.setSize(new Dimension(500, 500));
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        // Desktop pane...
        JDesktopPane desktopPane = new JDesktopPane();

        // Table...
        JTable table = getTable();

        // Scroll pane...
        JScrollPane scrollPane = new JScrollPane();

        // Internal frame...
        final JInternalFrame internalFrame = new JInternalFrame("Test Internal Frame", true, true, true, true);
        internalFrame.setSize(400, 300);
        internalFrame.setLocation(50, 50);
        internalFrame.setVisible(true);

        // Add everything...
        frame.setContentPane(desktopPane);
        desktopPane.add(internalFrame);
        internalFrame.setContentPane(scrollPane);
        scrollPane.setViewportView(table);

        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                internalFrame.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
                frame.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
                frame.setVisible(true);
            }
        });
    }

    private static void runNormalDemo()
    {
        // Frame...
        final JFrame frame = new JFrame("Normal Demo");
        frame.setSize(new Dimension(500, 500));
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        // Table...
        JTable table = getTable();

        // Scroll pane...
        JScrollPane scrollPane = new JScrollPane();

        // Add everything...
        frame.setContentPane(scrollPane);
        scrollPane.setViewportView(table);

        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                frame.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
                frame.setVisible(true);
            }
        });
    }

    private static JTable getTable()
    {
        final String[] columns = { "test 1", "test 2", "test 3", "test 4" };
        final Object[][] data = { { "1", "2", "3", "4" }, { "1", "2", "3", "4" } };
        final JTable table = new JTable(data, columns);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        return table;
    }
}

谢谢.

道歉,缺乏明确性-匆忙编写.

问题是,当我减小表的 width 时,"Normal Demo"(不包含在JInternalFrame中)在水平滚动条从右侧开始时执行相同的操作对于内部框架演示",水平滚动条从左侧开始.

有什么想法吗?

解决方案

如前所述,对于Java 1.6,内部框架和标准框架的行为似乎没有区别.

错误" JScrollPane忽略了组件方向"您的问题,但很久以前就已解决.

错误" JScrollPane无法使用RTL(RIGHT_TO_LEFT)正确布局"仍处于打开状态,可能会导致您的问题.

我在 Sun论坛中找到了一个解决方案,该解决方案对我有用,尽管问题略有不同.垂直滚动条显示在右侧而不是左侧.

scrollPane.setLayout(new ScrollPaneLayout() {

    @Override
    public void layoutContainer(Container parent) {
        JScrollPane scrollPane = (JScrollPane) parent;
        scrollPane.setComponentOrientation(
          ComponentOrientation.LEFT_TO_RIGHT);
        super.layoutContainer(parent);
        scrollPane.setComponentOrientation(
          ComponentOrientation.RIGHT_TO_LEFT);
    }
});

对我来说,当前行为听起来像是个错误,因为该错误(请参见上文)已得到修复.

So I have created some demo code, see below.

What I am seeing is that if a JScrollPane is within a JInternalFrame and component orientation is set to right-to-left, when minmising the frame, the scroll bar stays to the left of the content. I would expect, seeing as RtL, that it would stay to the right of the content, which is true if the scroll pane is not added to an internal frame (see both frames - one appears behind the other in the demo).

So is this a Java bug or have I forgotten to do something?

Here's the demo code:

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import java.awt.ComponentOrientation;
import java.awt.Dimension;


public class JScrollBarTest
{
    public static void main(String[] a)
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e) { }

        runInternalFrameDemo();
        runNormalDemo();
    }

    private static void runInternalFrameDemo()
    {
        // Frame...
        final JFrame frame = new JFrame("Internal Frame Demo");
        frame.setSize(new Dimension(500, 500));
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        // Desktop pane...
        JDesktopPane desktopPane = new JDesktopPane();

        // Table...
        JTable table = getTable();

        // Scroll pane...
        JScrollPane scrollPane = new JScrollPane();

        // Internal frame...
        final JInternalFrame internalFrame = new JInternalFrame("Test Internal Frame", true, true, true, true);
        internalFrame.setSize(400, 300);
        internalFrame.setLocation(50, 50);
        internalFrame.setVisible(true);

        // Add everything...
        frame.setContentPane(desktopPane);
        desktopPane.add(internalFrame);
        internalFrame.setContentPane(scrollPane);
        scrollPane.setViewportView(table);

        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                internalFrame.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
                frame.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
                frame.setVisible(true);
            }
        });
    }

    private static void runNormalDemo()
    {
        // Frame...
        final JFrame frame = new JFrame("Normal Demo");
        frame.setSize(new Dimension(500, 500));
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        // Table...
        JTable table = getTable();

        // Scroll pane...
        JScrollPane scrollPane = new JScrollPane();

        // Add everything...
        frame.setContentPane(scrollPane);
        scrollPane.setViewportView(table);

        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                frame.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
                frame.setVisible(true);
            }
        });
    }

    private static JTable getTable()
    {
        final String[] columns = { "test 1", "test 2", "test 3", "test 4" };
        final Object[][] data = { { "1", "2", "3", "4" }, { "1", "2", "3", "4" } };
        final JTable table = new JTable(data, columns);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        return table;
    }
}

Thanks in advance.

EDIT:

Apologies for the lack of clarity - written in quite a rush.

The issue is that when I reduce the width of the table, the 'Normal Demo' (not contained within a JInternalFrame) the horizontal scroll bar starts on the right, where as, doing the same for the 'Internal Frame Demo', the horizontal scroll bar starts on the left.

Any ideas?

解决方案

As discussed, with Java 1.6 there does not seem to be a difference in the behaviors of internal and standard frames.

Bug "JScrollPane ignores ComponentOrientation" describes your problem, but it has been fixed long time ago.

Bug "JScrollPane does not layout properly with RTL (RIGHT_TO_LEFT)" is still open and could cause your problem.

I found a solution in Sun Forums that made it work for me, although the problem was slightly different. Vertical scrollbar is displayed on the right side instead of the left.

scrollPane.setLayout(new ScrollPaneLayout() {

    @Override
    public void layoutContainer(Container parent) {
        JScrollPane scrollPane = (JScrollPane) parent;
        scrollPane.setComponentOrientation(
          ComponentOrientation.LEFT_TO_RIGHT);
        super.layoutContainer(parent);
        scrollPane.setComponentOrientation(
          ComponentOrientation.RIGHT_TO_LEFT);
    }
});

The current behavior sounds like a bug to me, since the bug (see above) has been fixed.

这篇关于JInternalFrames中的JScrollPanes用于从右到左的组件方向-Java错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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