在eclipse中更改我的小程序的大小,“以applet方式运行” [英] Changing the size for my applet also in eclipse "run as applet"

查看:183
本文介绍了在eclipse中更改我的小程序的大小,“以applet方式运行”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做我的第一个applet。在测试结果时,我希望能够在预览窗口中的eclipse中运行它,而不是总是将applet部署到一个jar中,并在浏览器中打开页面(浏览器缓存会杀死我!我总是需要重新启动浏览器..无论如何,当我尝试使用运行为 - > Java Applet运行应用程序时,我得到了预览,但它总是很小(猜测在200x200以下)。当我改变每只手的大小时,窗口会增长,但内容保持不变。当我调用 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屋!

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