在 JFrame 中看不到一个组件

发布于 2024-12-09 11:51:41 字数 1432 浏览 0 评论 0原文

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

public class Box {
    public static void main(String[] args){
        BoxFrame frame = new BoxFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

class BoxFrame extends JFrame{
    public BoxFrame(){
        setTitle("BoxGame");
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        DrawComponent[] component = new DrawComponent[4];
        component[0] = new DrawComponent(0, 0, 20, 20);
        component[1] = new DrawComponent(400, 0, 20, 20);

        add(component[0]);
        add(component[1]);//here the problem is
    }
    public static final int DEFAULT_WIDTH = 600;
    public static final int DEFAULT_HEIGHT = 400;
}

class DrawComponent extends JComponent{
    private double left;
    private double top;
    private double width;
    private double height;
    public DrawComponent(double l, double t, double w, double h){
        left = l;
        top = t;
        width = w;
        height = h;
    }
    public void paintComponent(Graphics g){
        Graphics2D g2 = (Graphics2D) g;
        Rectangle2D rect = new Rectangle2D.Double(left, top, width, height);
        g2.draw(rect);
        g2.setPaint(Color.BLUE);
        g2.fill(rect);
    }
}

这是我的代码,并不复杂。但是当我尝试绘制两个组件时,窗口只绘制一个。这段代码,当我去掉第一个组件时,窗口将绘制第二个组件。我在javadocs中查找了JFrame.add方法,但没有找到错误是什么,请帮助我

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

public class Box {
    public static void main(String[] args){
        BoxFrame frame = new BoxFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

class BoxFrame extends JFrame{
    public BoxFrame(){
        setTitle("BoxGame");
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        DrawComponent[] component = new DrawComponent[4];
        component[0] = new DrawComponent(0, 0, 20, 20);
        component[1] = new DrawComponent(400, 0, 20, 20);

        add(component[0]);
        add(component[1]);//here the problem is
    }
    public static final int DEFAULT_WIDTH = 600;
    public static final int DEFAULT_HEIGHT = 400;
}

class DrawComponent extends JComponent{
    private double left;
    private double top;
    private double width;
    private double height;
    public DrawComponent(double l, double t, double w, double h){
        left = l;
        top = t;
        width = w;
        height = h;
    }
    public void paintComponent(Graphics g){
        Graphics2D g2 = (Graphics2D) g;
        Rectangle2D rect = new Rectangle2D.Double(left, top, width, height);
        g2.draw(rect);
        g2.setPaint(Color.BLUE);
        g2.fill(rect);
    }
}

here is my code, it is not complicated. but when i try to draw two component, the window only draw one. this piece of code,when i get rid of the first component, the window will draw the second one. and i have looked up JFrame.add method in javadocs, but did not find what the error is,plz help me

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

书间行客 2024-12-16 11:51:41

问题是您正在使用 JFrame 的默认布局管理器,它是 BorderLayout。当您添加第二个组件时,它将替换第一个组件。 (两者都添加到 CENTER 单元格中。)

如果您想使用组件来显示框,我建议您将它们放置在不重叠的位置。

否则我建议您在同一组件上绘制所有框。

The problem is that you're using the default layout manager of a JFrame which is a BorderLayout. When you add your second component, it will replace the first. (Both are added to the CENTER cell.)

If you want to use components to show the boxes I suggest you lay them out without overlap.

Otherwise I'd suggest you draw all boxes on the same component.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文