也在 Eclipse 中更改我的小程序的大小“作为小程序运行”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4759499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing the size for my applet also in eclipse "run as applet"
提问by WarrenFaith
I am currently doing my first applet. While testing the results I want to be able to run it in eclipse in the preview windows instead of always deploying the applet into a jar and opening the page in the browser (the browser cache kills me! I always need to restart the browser...)
我目前正在做我的第一个小程序。在测试结果时,我希望能够在预览窗口中的 eclipse 中运行它,而不是总是将小程序部署到 jar 中并在浏览器中打开页面(浏览器缓存杀了我!我总是需要重新启动浏览器.. .)
Anyway, when I try to run the app with "run as -> Java Applet" I got the preview but its always very small (guess below 200x200). When I change the size per hand, the window grows but the content stay that small. When I call setSize(width, height)
the window starts bigger, the content stays small. Small doesn't mean its scaled down, it means that I only see the black panel, the white one (which is visible in the browser) is not visible so its not seemed to be scaled...
无论如何,当我尝试使用“run as -> Java Applet”运行应用程序时,我得到了预览,但它总是很小(猜测低于 200x200)。当我改变每只手的大小时,窗口会变大但内容保持那么小。当我调用setSize(width, height)
窗口开始变大时,内容保持小。小并不意味着它缩小了,这意味着我只看到黑色面板,白色面板(在浏览器中可见)不可见,所以它似乎没有被缩放......
What am I missing?
我错过了什么?
My code so far (which works like expected in the broswer with width of 560 and height of 500)
到目前为止,我的代码(在宽度为 560 和高度为 500 的浏览器中按预期工作)
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Benchmark extends JApplet {
private static final long serialVersionUID = -8767182603875435760L;
GridLayout gridLayout = new GridLayout(7, 1);
JButton startTests = new JButton("Start");
JPanel testPanel = new JPanel();
JPanel topPanel = new JPanel();
@Override
public void init() {
super.init();
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
invalidate();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void initComponents() {
setSize(660, 500);
topPanel.setBackground(Color.BLACK);
topPanel.setSize(500, 500);
testPanel.setBackground(Color.WHITE);
testPanel.setSize(160, 500);
getContentPane().add(topPanel, BorderLayout.WEST);
getContentPane().add(testPanel, BorderLayout.EAST);
testPanel.setLayout(gridLayout);
testPanel.add(new JLabel("Triangles"));
testPanel.add(new JLabel("Pyramids"));
testPanel.add(new JLabel("Cubes"));
testPanel.add(new JLabel("Blending"));
testPanel.add(new JLabel("Spheres"));
testPanel.add(new JLabel("Lights"));
testPanel.add(new JLabel("Mass"));
}
}
The screenshot should show the problem. If the window has the size of 660x500 (set with setSize()
the visible area stays small:
屏幕截图应显示问题。如果窗口的大小为 660x500(设置setSize()
为可见区域保持较小:
采纳答案by jzd
Your size of the window is set to 500,500 and so it this size of your black box. The panel on the right is visible if you widen your screen.
您的窗口大小设置为 500,500,因此它是您的黑匣子的大小。如果您加宽屏幕,则可以看到右侧的面板。
Remove the code for setting a size and a min,max, and preferred size for the topPanel
. Then instead of adding it to BorderLayout.WEST
, use BorderLayout.CENTER
instead. This will allow the test panel to stay on the left and will resize your black box as the window resizes.
删除用于设置大小以及最小、最大和首选大小的代码topPanel
。然后不是将其添加到BorderLayout.WEST
,BorderLayout.CENTER
而是使用。这将允许测试面板保持在左侧,并会随着窗口大小的调整而调整黑框的大小。