在java中的Jframe上绘制简单的矩形
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9713432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
drawing simple rectangles on a Jframe in java
提问by wfbarksdale
I am extending JFrame like this:
我正在像这样扩展 JFrame:
public GameFrame() {
this.setBounds(30, 30, 500, 500);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
initializeSquares();
}
private void initializeSquares(){
for(int i = 0; i < 5; i++){
this.getContentPane().add(new Square(i*10, i*10, 100, 100));
}
this.setVisible(true);
}
However, only one square is being drawn on the screen, does anybody know why?
但是,屏幕上只绘制了一个正方形,有人知道为什么吗?
also My Square class looks like this:
我的 Square 类也如下所示:
private int x;
private int y;
private int width;
private int height;
public Square(int x, int y, int width, int height){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawRect(x, y, width, height);
}
采纳答案by Hovercraft Full Of Eels
The JFrame's contentPane uses BorderLayout by default. When you add a Square to it, it gets added by default BorderLayout.CENTER and covers up any previously added Squares. You will want to read up on all the layout managers available to Swing GUI's.
JFrame 的 contentPane 默认使用 BorderLayout。当你向它添加一个 Square 时,它会被默认添加 BorderLayout.CENTER 并覆盖任何以前添加的 Squares。您将需要阅读所有可用于 Swing GUI 的布局管理器。
For e.g., start here: Laying Out Components within a Container
例如,从这里开始:在容器中布置组件
But having said this, I would do things differently. I would create just one JPanel and make it able to paint multiple squares. For example something like so:
但话虽如此,我会做不同的事情。我只会创建一个 JPanel 并使其能够绘制多个正方形。例如像这样:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
public class GameFrame extends JFrame {
public GameFrame() {
super("Game Frame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Squares squares = new Squares();
getContentPane().add(squares);
for (int i = 0; i < 15; i++) {
squares.addSquare(i * 10, i * 10, 100, 100);
}
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new GameFrame();
}
}
class Squares extends JPanel {
private static final int PREF_W = 500;
private static final int PREF_H = PREF_W;
private List<Rectangle> squares = new ArrayList<Rectangle>();
public void addSquare(int x, int y, int width, int height) {
Rectangle rect = new Rectangle(x, y, width, height);
squares.add(rect);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
for (Rectangle rect : squares) {
g2.draw(rect);
}
}
}
回答by Jakub Zaverka
Fo absolute positioning, call setLayout(null). Then the icons will be drawed at the position returned by their getLocation() method, so you might want to call the setLocation() first.
对于绝对定位,调用 setLayout(null)。然后图标将被绘制在它们的 getLocation() 方法返回的位置,所以你可能想先调用 setLocation()。
回答by crackerplace
Even I was having an issue while displaying multiple rectangles on a jframe with absolute layout i.e layout set to null.
甚至我在使用绝对布局(即布局设置为空)在 jframe 上显示多个矩形时也遇到问题。
JFrame frame = new JFrame("hello");
frame.setLayout(null);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.getContentPane().setBackground(Color.red);
frame.getContentPane().add(new Square(10,10,100,100));//My rectangle class is similar to the Square mentioned in the question and it extends JComponent.
Still I was getting issues as the rectangle was not displayed.Unless you explicitly set the bounds on the Square(JComponent) ,it will not work.Even though the Square has the location passed in the constructor ,setbounds only fixed the issue.So the below fixed the issue and this issue is for an absolute layout jframe.
由于未显示矩形,我仍然遇到问题。除非您在 Square(JComponent) 上明确设置边界,否则它将不起作用。即使 Square 在构造函数中传递了位置,setbounds 也仅解决了问题。所以下面修复了这个问题,这个问题是针对绝对布局 jframe 的。
Square square = new Square(10,10,100,100);
square.setBounds(10,10,100,100);
frame.getContentPane().add(square);