如何获得JPanel宽度和高度相等 [英] How to get JPanel equal width and height

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

问题描述

我从我不明白为什么w = getWidth()和h = getHeight()不相等.以及如何使它们彼此相等?

I don't understand why w = getWidth() and h = getHeight() are not equal. And how to make them equal to each other?

谢谢

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

public class GraphingData extends JPanel {
 int[] data = {
     21, 14, 18, 03, 86, 88, 74, 87, 54, 77,
     61, 55, 48, 60, 49, 36, 38, 27, 20, 18
 };
 final int PAD = 20;

protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
    int w = getWidth();
    int h = getHeight();
    // Draw ordinate.
    g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD));
    // Draw abcissa.
    g2.draw(new Line2D.Double(PAD, h-PAD, w-PAD, h-PAD));
    double xInc = (double)(w - 2*PAD)/(data.length-1);
    double scale = (double)(h - 2*PAD)/getMax();
    // Mark data points.
    g2.setPaint(Color.red);
    for(int i = 0; i < data.length; i++) {
        double x = PAD + i*xInc;
        double y = h - PAD - scale*data[i];
        g2.fill(new Ellipse2D.Double(x-2, y-2, 4, 4));
    }
}

private int getMax() {
    int max = -Integer.MAX_VALUE;
    for(int i = 0; i < data.length; i++) {
        if(data[i] > max)
            max = data[i];
    }
    return max;
}

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(new GraphingData());
    f.setSize(400,400);
    f.setLocation(200,200);
    f.setVisible(true);
}
}

推荐答案

您似乎在假设框架的大小就是可见内容区域的大小.

You seem to be under the assumption that the size of frame is the size of the viewable content area.

框架由内容和框架装饰组成.因此,在我的系统上,如果帧为400x400,则内容可视区域为384x362

A frame is made up of the content AND frame decorations. So on my system, a frame of 400x400, results in a content viewable area of 384x362

您应该做的第一件事就是摆脱f.setSize(),它是不可靠的,因为它创建的可见内容区域对于程序所运行的每个系统都是不同的.相反,您应该使用f.pack(),它使用框架内容来确定窗口的大小(以便可查看的内容区域具有优先级).

The first thing you should do is get rid of f.setSize(), it is unreliable as the viewable content area it creates will be different for each system your program runs on. Instead, you should be using f.pack(), which uses the frame contents to determine the size of the window (so that the viewable content area is given precedence).

接下来,在您的GraphingData类中,您将需要覆盖getPreferredSize并返回您要使用的面板的首选尺寸.

Next, in you GraphingData class, you will need to override getPreferredSize and return the preferred size of the panel you would like to use.

@Override
public Dimension getPreferredSize() {
    return new Dimension(400, 400);
}

这将使(一些)布局管理者可以更好地决定如何最好地展示您的组件.

This will allow (some) layout managers to make better decisions about how best to present your component.

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

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