【问题标题】:Force size restrictions on Applet embedded in html对嵌入在 html 中的 Applet 进行强制大小限制
【发布时间】:2013-05-26 06:28:13
【问题描述】:

我有一个带有 GridLayout 的 Java 小程序,其中包含我希望是方形的小部件,并且彼此紧密排列(因此它们的大小不受限制)。
但是,我希望 GridLayout 在屏幕太大或无法保留小部件“方形”之前尽可能多地占用空间。
注意GridLayout中的行数和列数不一定相等(Grid作为一个整体可以是非方形的)

这个小程序通过这个html文件显示;

<html>
<body>
<applet code=client.Grid.class 
        archive="program.jar"
        width=100% height=95%>
</applet>
</body>
</html>

目前,这会使 Applet 扩展到它所在的窗口中;网格可以通过调整窗口大小来调整大小,但这会导致每个小部件的几何形状发生变化(失去“平方”)。

所以;我在哪里以及如何放置这些几何限制?
它不能单独存在于 html 文件中,因为它不知道行数/列数,因此不知道制作 Applet 的最佳大小。
但是,我不知道如何在 GridLayout 或包含它的面板上设置大小,因为它必须知道查看浏览器的页面大小(使其尽可能大),而且我的印象是 html指定的几何图形会覆盖指定的 Applet。

编辑:
尝试执行安德鲁的建议;

screen = new JPanel(new GridLayout(rows, columns)) {

    public Dimension getPreferredSize() {

        Dimension expected = super.getPreferredSize();  
        // calculate preferred size using expected, rows, columns
        return new Dimension(100, 100) // testing
    }
    public Dimension getSize() {
        return getPreferredSize();
    }
};

我知道这忽略了“最小尺寸”的东西,但这并不重要。
屏幕放置在边框布局的中心,包含其他小部件

getContentPane().add(screen, BorderLayout.CENTER);
getContentPane().add(otherWidgets, BorderLayout.PAGE_END);

我知道这不会使 screen 居中于它所拥有的空间中,但目前这并不完全必要,所以我想让事情尽可能简单。

这根本行不通;除了最小尺寸的东西外,与我以前的东西没有明显的区别(当通过 Eclipse 查看时;我什至还没有达到 html 阶段)。屏幕组件仍由小程序在闲暇时重新调整大小,使单元格“不方形”。我做错了什么?

【问题讨论】:

  • 您是否尝试过骑马 getMinimumSize() ?我知道大多数人不喜欢它,但我通常将布局管理器设置为 null,而是使用我自己的 Java I 计算调用 setBounds。也许你也可以这样做。如果小程序没有改变大小的事件,那么它可能不是自动的,尽管我认为它应该是自动的。你可以在 Applet 上使用 getParent 直到你得到一个窗口的实例
  • 如需尽快获得更好的帮助,请发帖SSCCE
  • BorderLayoutCENTER 组件将被拉伸到可用大小,而不管孩子想要的大小。将JPanel 添加到CENTER 并进行布局GridBagLayout。然后将GridLayout(row,columns) 添加到GBL。

标签: java html swing applet grid-layout


【解决方案1】:

将网格布局容器作为唯一没有约束的组件放入网格包布局中,如this answer 所示。这将使它居中。

更新

当然,将它放在一个组件中,该组件返回一个首选大小,该大小等于它可以根据父大小管理的最大正方形大小。比如SquarePanel

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

/**
 * A square panel for rendering. NOTE: To work correctly, this must be the only
 * component in a parent with a layout that allows the child to decide the size.
 */
class SquarePanel extends JPanel {

    @Override
    public Dimension getPreferredSize() {
        Dimension d = super.getPreferredSize();
        System.out.println("Preferred Size: " + d);
        int w = (int) d.getWidth();
        int h = (int) d.getHeight();
        // Set s to the larger of the mimimum component width or height
        int s = (w > h ? w : h);
        Container c = getParent();
        if (c != null ){
            Dimension sz = c.getSize();
            if ( d.getWidth()<sz.getWidth() ) {
                // Increase w to the size available in the parent container
                w = (int)sz.getWidth();
                System.out.println("WxH: " + w + "x" + h);
                // recalculate s
                s = (w < h ? w : h);
            }
            if ( d.getHeight()<sz.getHeight()) {
                // Increase h to the size available in the parent container
                h = (int)sz.getHeight();
                System.out.println("WxH: " + w + "x" + h);
                // recalculate s
                s = (w < h ? w : h);
            }
        }
        // Use s as the basis of a square of side length s.
        System.out.println("Square Preferred Size: " + new Dimension(s, s));
        return new Dimension(s, s);
    }

    @Override
    public Dimension getMinimumSize() {
        return getPreferredSize();
    }

    @Override
    public Dimension getSize() {
        return getPreferredSize();
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                // the GUI as seen by the user (without frame)
                // A single component added to a GBL with no constraint
                // will be centered.
                JPanel gui = new JPanel(new GridBagLayout());
                gui.setBackground(Color.BLUE);

                SquarePanel p = new SquarePanel();
                p.setBorder(new EmptyBorder(5,15,5,15));
                p.setLayout(new GridLayout(3,0,2,2));
                for (int ii=1; ii<13; ii++) {
                    p.add(new JButton("" + ii));
                }
                p.setBackground(Color.red);
                gui.add(p);

                JFrame f = new JFrame("Demo");
                f.add(gui);
                // Ensures JVM closes after frame(s) closed and
                // all non-daemon threads are finished
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                // See https://stackoverflow.com/a/7143398/418556 for demo.
                f.setLocationByPlatform(true);

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.pack();
                // should be done last, to avoid flickering, moving,
                // resizing artifacts.
                f.setVisible(true);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}

【讨论】:

  • 我不希望网格居中;我希望它尽可能地扩展到可用空间,同时保持网格的恒定高度/宽度比。
  • 我对 SquarePanel 如何决定其长度和宽度感到有点困惑。 IE;为什么super.getPreferredSize()给了空间? (注意我的网格不需要与列相等的行并且元素之间没有间距)
  • 我试图实现这个(没有成功)并更新了我的问题。我做错了什么?
  • 您能解释一下您如何计算适当大小的逻辑吗?什么变量代表可用空间?代码很难理解。
  • // Use s as the basis of a square of side length s. 以单行 cmets 结尾是否更有意义?
猜你喜欢
  • 2011-09-15
  • 2011-09-06
  • 2012-04-10
  • 1970-01-01
  • 2011-09-16
  • 1970-01-01
  • 2011-07-29
  • 1970-01-01
  • 2019-08-22
相关资源
最近更新 更多