也在 Eclipse 中更改我的小程序的大小“作为小程序运行" [英] Changing the size for my applet also in eclipse "run as applet"

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

问题描述

我目前正在做我的第一个小程序.在测试结果时,我希望能够在预览窗口中的 eclipse 中运行它,而不是总是将小程序部署到 jar 中并在浏览器中打开页面(浏览器缓存杀了我!我总是需要重新启动浏览器...)

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...)

无论如何,当我尝试使用run as -> Java Applet"运行应用程序时,我得到了预览,但它总是很小(猜测低于 200x200).当我改变每只手的大小时,窗口会变大但内容保持那么小.当我调用 setSize(width, height) 时,窗口开始变大,内容保持小.小并不意味着它缩小了,这意味着我只看到黑色面板,白色面板(在浏览器中可见)不可见,所以它似乎没有被缩放...

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...

我错过了什么?

到目前为止我的代码(在浏览器中按预期工作,宽度为 560,高度为 500)

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

屏幕截图应显示问题.如果窗口大小为 660x500(使用 setSize() 设置,则可见区域保持较小:

The screenshot should show the problem. If the window has the size of 660x500 (set with setSize() the visible area stays small:

推荐答案

您的窗口大小设置为 500,500,因此它是您的黑匣子的大小.如果您加宽屏幕,则可以看到右侧的面板.

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.

删除用于设置 topPanel 的大小和最小、最大和首选大小的代码.然后,不要将其添加到 BorderLayout.WEST,而是使用 BorderLayout.CENTER.这将允许测试面板保持在左侧,并会随着窗口大小的调整而调整黑框的大小.

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 中更改我的小程序的大小“作为小程序运行"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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