Java html中嵌入的小程序的强制大小限制
我有一个带有GridLayout的Java小程序,其中包含我希望是方形的小部件,并且彼此紧密封装(因此它们的大小不受限制)。Java html中嵌入的小程序的强制大小限制,java,html,swing,applet,grid-layout,Java,Html,Swing,Applet,Grid Layout,我有一个带有GridLayout的Java小程序,其中包含我希望是方形的小部件,并且彼此紧密封装(因此它们的大小不受限制)。 但是,我希望GridLayout在屏幕太大或无法保持小部件的“方正性”之前占据尽可能多的空间。 请注意,GridLayout中的行数和列数不一定相等(网格作为一个整体可以是非正方形的) 此小程序通过此html文件显示 <html> <body> <applet code=client.Grid.class archive="
但是,我希望GridLayout在屏幕太大或无法保持小部件的“方正性”之前占据尽可能多的空间。
请注意,GridLayout中的行数和列数不一定相等(网格作为一个整体可以是非正方形的) 此小程序通过此html文件显示
<html>
<body>
<applet code=client.Grid.class
archive="program.jar"
width=100% height=95%>
</applet>
</body>
</html>
我知道这忽略了“最小尺寸”的内容,但目前这并不重要。屏幕位于边框布局的中心,包含其他小部件
getContentPane().add(screen, BorderLayout.CENTER);
getContentPane().add(otherWidgets, BorderLayout.PAGE_END);
我知道这不会使屏幕
集中在它所拥有的空间中,但目前这并不是完全必要的,所以我想让事情尽可能简单
这根本不起作用;除了最小尺寸的东西外,与我之前的(通过Eclipse查看时;我甚至还没有达到html阶段)没有明显的区别。小程序仍在空闲时重新调整屏幕组件的大小,使单元格“不方形”。我做错了什么?将网格布局容器作为唯一没有约束的组件放入网格包布局中,如中所示。这将使它居中 更新 当然,将它放在一个组件中,该组件返回一个首选大小,该大小等于它可以根据父级大小管理的最大正方形大小。例如在
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);
}
}
import java.awt.*;
导入javax.swing.*;
导入javax.swing.border.EmptyBorder;
/**
*用于渲染的方形面板。注意:要正确工作,这必须是唯一
*父级中的组件,其布局允许子级决定大小。
*/
类SquarePanel扩展了JPanel{
@凌驾
公共维度getPreferredSize(){
维度d=super.getPreferredSize();
System.out.println(“首选尺寸:+d”);
int w=(int)d.getWidth();
inth=(int)d.getHeight();
//将s设置为最小组件宽度或高度中的较大值
ints=(w>h?w:h);
容器c=getParent();
如果(c!=null){
维度sz=c.getSize();
如果(d.getWidth(),我不希望网格居中;我希望它尽可能扩展到可用空间,同时保持网格的恒定高宽比。我对SquarePanel如何决定其长度和宽度有点困惑。也就是说,super.getPreferredSize()
给出的可用空间是什么?(注意,我的网格不需要相等的行到列,元素之间没有间距)我已经尝试实现了这一点(没有成功)更新了我的问题。我做错了什么?你能解释一下你计算适当大小的逻辑吗?什么变量代表可用空间?代码很难理解。//使用s作为边长s的平方的基础。
以o结尾的单行注释更有意义吗ne?你试过骑getMinimumSize()吗?我知道大多数人都不喜欢它,但我通常会将LayoutManager设置为null,并使用我自己的Java i计算调用setBounds。也许你也可以这样做。如果小程序没有更改大小的事件,那么它可能不是自动的,尽管我认为应该。你可以在小程序上使用getParent,直到获得windowFor bett的实例er帮助更快,发布一个。无论孩子想要什么大小,BorderLayout
的CENTER
组件都将被拉伸到可用大小。将JPanel
添加到CENTER
并制作布局gridbagloayout
。然后将GridLayout(行、列)
添加到GBL。
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);
}
}