繁体   English   中英

强制对嵌入在html中的Applet进行大小限制

[英]Force size restrictions on Applet embedded in html

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

此小程序通过此html文件显示;

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

当前,这使Applet可以扩展到其放入的窗口中。 可以通过调整窗口的大小来调整Grid的大小,但这会导致每个小部件的几何形状发生改变(丢失“平方度”)。

所以; 我在哪里以及如何放置这些几何限制?
它不能单独存在于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居中显示在screen中央,但是目前这并不是完全必要的,因此我想使事情尽可能简单。

这根本没有用; 除了最小尺寸的东西外,与我以前(​​通过Eclipse查看时;我什至还没有到达html阶段)没有明显的区别。 小应用程序仍然可以随意调整屏幕组件的大小,从而使单元格“不方形”。 我究竟做错了什么?

将网格布局容器放入网格袋布局中,作为唯一不受限制的组件,如本答案所示 那将使它居中。

更新

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

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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM