在eclipse中更改我的小程序的大小,“以applet方式运行” [英] Changing the size for my applet also in eclipse "run as applet"
问题描述
setSize(width,height)
时,窗口开始变大,内容保持不变。小的并不意味着它的缩小,这意味着我只看到黑色面板,白色(在浏览器中可见)是不可见的,所以它似乎没有被缩放... 我缺少什么?
我的代码到目前为止(这样做在预期的宽度为560,高度为500的broswer中) / p>
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));
}
}
截图应显示问题。如果窗口的大小为660x500(设置为 setSize()
,可见区域保持较小:
你的窗口大小设置为500,500,所以这个大小您的黑盒子,如果您的屏幕变宽,右侧的面板是可见的。
删除用于设置大小和最小,最大和首选大小的代码 topPanel
然后将其添加到 BorderLayout.WEST
中,使用 BorderLayout.CENTER
,这样可以让测试面板留在左边,随着窗口调整大小,将调整黑匣子的大小。
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...)
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...
What am I missing?
My code so far (which works like expected in the broswer with width of 560 and height of 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:
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.
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.
这篇关于在eclipse中更改我的小程序的大小,“以applet方式运行”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!