在Applet的力的大小限制嵌入在HTML [英] Force size restrictions on Applet embedded in html

查看:163
本文介绍了在Applet的力的大小限制嵌入在HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java小程序包含我希望是正方形,并​​保持紧凑对方(所以它们的大小不受限制)小部件网格布局。结果
不过,我希望为网格布局,以占据尽可能多的空间可能是在屏幕过大或无法preserve部件'垂直'了。结果
注意,在GridLayout的行和列的数目不一定是相等的(网格作为一个整体可以是非正方形)

I have a Java Applet with a GridLayout containing widgets which I wish to be square, and remain tightly packed to each other (so their sizes are unrestricted).
However, I wish for the GridLayout to take up as much space as possible before being too large for the screen or unable to preserve widget 'squareness'.
Note that the number of rows and columns in the GridLayout are not necessarily equal (the Grid as a whole can be non-square)

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

This Applet is displayed via this html file;

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

目前,这使得小程序扩展到它被放在窗口;网格可以通过调整窗口被调整大小,但是这会导致改变(失去'squaredness')每个插件的几何形状。

Currently, this makes the Applet expand into the window it is put in; the Grid can be resized by resizing the window, but this causes the geometry of each widget to be changed (losing 'squaredness').

左右;在这里,我如何把这些几何限制?结果
它不能在html文件单独的,因为它不具有行/列数的知识,并且因此不知道使小程序的最佳的尺寸。结果,
但是,我不知道如何设置大小的网格布局,或包含它的面板,因为它必须知道观看浏览器的页面大小(以使其尽可能大),我在IM $ P $的该HTML指定几何pssion覆盖指定的小程序。

So; where and how do I place these geometrical restrictions?
It can't be in the html file alone, since it has no knowledge of row/column count, and so doesn't know the best size to make the Applet.
However, I don't know how to set the size on the GridLayout or the Panel containing it, since it must know the viewing-browser's page size (to make it as large as possible) and I'm of the impression that the html specified geometry overrides the Applet specified.

编辑:结果
试图实施安德鲁的建议;


Attempting to implement Andrew's suggestion;

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

我明白这忽略了最小尺寸的东西,但是,这并不此刻没关系。结果
屏幕放置在边界布局的中心,含有其它窗口小部件

I understand this ignores the 'minimum size' stuff, but that doesn't matter at the moment.
Screen is placed in the center of a border layout, containing other widgets

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

我知道这不会使屏幕在它有空间为中心,但事实并非完全必要的时刻,所以我希望保持尽可能简单。

I know this doesn't make screen centered in the space it has, but that's not entirely necessary at the moment so I want to keep things as simple as possible.

这是不是在所有的工作;有从我收到没有明显的区别(通过Eclipse中查看时,我还没有达到HTML阶段尚)的除外最小尺寸的东西。屏幕部分依然被小程序在闲暇重新调整大小,使细胞的unsquare。我在做什么错了?

This isn't at all working; there's no visible difference from what I had before (when viewed through Eclipse; I haven't even reached the html stage yet) excepting the minimum size stuff. The screen component is still being re-sized by the applet at leisure, making the cells 'unsquare'. What am I doing wrong?

推荐答案

把网格布局容器放入网格包布局与没有限制的唯一组成部分,因为看到的这个答案。这将居中。

Put the grid layout container into a grid bag layout as the only component with no constraint, as seen in this answer. That will center it.

当然,把它放在一个返回preferred大小等同于最大见方大小可以根据父规模管理组件。如在 SquarePanel

And of course, put it in a component that returns a preferred size equating to the maximum square size it can manage depending on the parent size. Such as in 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 http://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);
    }
}

这篇关于在Applet的力的大小限制嵌入在HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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