-1

I create the multiple JInternalFrame. It is working properly. But when create the JInternalFrame, it is load over the existing one. I need the JInternalFrame like as table cells (row by row).

4

1 回答 1

3

您需要告诉JInternalFrames 您希望它们放置的位置和大小。

在此处输入图像描述

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.SystemColor;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestInternalFrameLayout {

    public static void main(String[] args) {
        new TestInternalFrameLayout();
    }

    public TestInternalFrameLayout() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                final JDesktopPane desktop = new JDesktopPane();

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(desktop);
                frame.setSize(420, 420);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        int rowCount = 2;
                        int colCount = 4;

                        int width = desktop.getWidth() / colCount;
                        int height = desktop.getHeight() / rowCount;

                        System.out.println(width + "x" + height);

                        for (int row = 0; row < rowCount; row++) {
                            int y = row * height;
                            for (int col = 0; col < colCount; col++) {
                                int x = col * width;
                                JInternalFrame frame = new JInternalFrame(row + "x" + col, true, true, true, true);
                                frame.setBounds(x, y, width, height);
                                frame.setVisible(true);
                                desktop.add(frame);
                            }
                        }
                    }
                });
            }
        });
    }
}

现在,您完全没有为您的问题提供任何上下文,因此很难确切知道您需要什么。

如果随着时间的推移添加框架,那么您需要根据需要确定放置它们的最佳位置。一种常见的方法是使用 astatic xy值,在添加新框架时根据需要递增。

您可以编写自己的“打包”算法,该算法会根据您的需要自动平铺窗口。

您可以简单地使用 aGridLayout而不是JInternalFrames....

于 2013-09-13T04:59:37.330 回答