在 JFrame 中看不到一个组件
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您正在使用
JFrame
的默认布局管理器,它是BorderLayout
。当您添加
第二个组件时,它将替换第一个组件。 (两者都添加到CENTER
单元格中。)如果您想使用组件来显示框,我建议您将它们放置在不重叠的位置。
否则我建议您在同一组件上绘制所有框。
The problem is that you're using the default layout manager of a
JFrame
which is aBorderLayout
. When youadd
your second component, it will replace the first. (Both are added to theCENTER
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.