如何设置JPanel的宽度和高度? [英] How to Set JPanel's Width and Height?

查看:146
本文介绍了如何设置JPanel的宽度和高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Java 开发 Snake 游戏.电路板(所有操作发生的地方)的宽度和高度应该是固定的(640 像素 * 480 像素).

I'm developing Snake game using Java. Board's (where all action takes it place) width and height should be fixed (640 pixels * 480 pixels).

结构:

  • 主类,继承自JFrame类,
  • Board 类,继承自 JPanel 类;
  • Main class that extends from JFrame class,
  • Board class that extends from JPanel class;

Main 类中,我有类似的东西...

In Main class I have something like...

setSize( 1024, 768 );

...在董事会课上我有...

...and in Board class I have...

setSize( BOARDS_WIDTH, BOARDS_HEIGHT );

问题是主窗口和唯一窗口似乎是 1024 * 768,但里面的面板 - 不像 640 * 480.我也有 setBackground() 并且背景填充超过 640* 480.

Problem is that main and only windows seems to be like 1024 * 768, but panel inside it - not like 640 * 480. I have setBackground() as well and background is filled more than 640 * 480.

有什么办法可以让 Board 的宽度和高度像 640 * 480,但主窗口的宽度和高度 - 与 Board 的宽度和高度一样多吗?

Is there any way to make Board's width and height like 640 * 480, but main windows width and height - as much as Board's width and height are correct?

现在看起来像这样......

Right now it looks like this...

一切正常......几乎.

All works... almost.

类:

add( new Board(), BorderLayout.CENTER );
pack();
setResizable( false );
setLocationRelativeTo( null );
setVisible( true );
setDefaultCloseOperation( EXIT_ON_CLOSE );
setTitle( "Snake (by daGrevis)" );

董事会类:

setPreferredSize( new Dimension( 640, 480 ) );

灰色背景为 642 * 482!就像会有 1 个像素的边框一样!

Gray background is 642 * 482! Like there would be border that wraps all by 1 pixel!

推荐答案

拜托,事情发生了 xxx*x,这根本不是真的,检查一下

please, something went xxx*x, and that's not true at all, check that

JButton Size - java.awt.Dimension[width=400,height=40]
JPanel Size - java.awt.Dimension[width=640,height=480]
JFrame Size - java.awt.Dimension[width=646,height=505]

代码(来自 Trail:使用 JFC/Swing 创建 GUI 的基本内容 ,但我仍然满意那会过时 )

code (basic stuff from Trail: Creating a GUI With JFC/Swing , and yet I still satisfied that that would be outdated )

忘记setDefaultCloseOperation()

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class FrameSize {

    private JFrame frm = new JFrame();
    private JPanel pnl = new JPanel();
    private JButton btn = new JButton("Get ScreenSize for JComponents");

    public FrameSize() {
        btn.setPreferredSize(new Dimension(400, 40));
        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("JButton Size - " + btn.getSize());
                System.out.println("JPanel Size - " + pnl.getSize());
                System.out.println("JFrame Size - " + frm.getSize());
            }
        });
        pnl.setPreferredSize(new Dimension(640, 480));
        pnl.add(btn, BorderLayout.SOUTH);
        frm.add(pnl, BorderLayout.CENTER);
        frm.setLocation(150, 100);
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // EDIT
        frm.setResizable(false);
        frm.pack();
        frm.setVisible(true);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                FrameSize fS = new FrameSize();
            }
        });
    }
}

这篇关于如何设置JPanel的宽度和高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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