我有一个名为DrawRectangles
的类,它接受一个整数数组。
我要做的是遍历数组中的数字,并为每个数字创建一个新面板,使用数组中的数字作为面板的宽度和X位置。
让我说我传入[2,4,6,8]。我想创建一个新面板,用这些数字添加到JFrame
。
所以第一个面板应该从位置2开始并且宽度为2.我还有一个随机颜色生成器,它应该为每个面板创建一个新颜色。这就是我所拥有的:
public class DrawRectangles {
JFrame frame;
DrawPanel panel;
Random randomGenerator = new Random();
int red = randomGenerator.nextInt(255);
int green = randomGenerator.nextInt(255);
int blue = randomGenerator.nextInt(255);
Color randomColor;
int[] newWidth;
DrawRectangles(int[] width){
this.newWidth = width;
}
public void setUpFrame(){
frame = new JFrame();
frame.setSize(500,100);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
for(int x = 0; x < newWidth.length; x++){
panel = new DrawPanel(newWidth[x]);
frame.add(panel);
}
}
class DrawPanel extends JPanel{
int newWidth;
DrawPanel(int width){
this.newWidth = width;
System.out.println(newWidth);
}
public void paint(Graphics g) {
super.paint(g);
randomColor = new Color(red,green,blue);
g.setColor(randomColor);
g.fillRect(newWidth, 10, newWidth, 30);
}
}
}
答案 0 :(得分:3)
你有很多问题......
BorderLayout
。这将只允许单个组件占用其可用的任何一个预定义位置。这意味着只有最后一个窗格可见。randomColor
更改值时,所有组件都会引用它
也将在下一个油漆循环中重新粉刷它。paintComponent
代替paint
这是使用单个DrawRectangle
组件绘制所有矩形的基本示例。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class DrawRectangles {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
DrawRectangles dr = new DrawRectangles(new int[]{2, 4, 6, 8});
dr.setUpFrame();
}
});
}
JFrame frame;
DrawPanel panel;
Random randomGenerator = new Random();
int[] newWidth;
DrawRectangles(int[] width) {
this.newWidth = width;
}
public void setUpFrame() {
frame = new JFrame();
frame.setSize(500, 100);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DrawPanel panel = new DrawPanel();
for (int x = 0; x < newWidth.length; x++) {
int red = randomGenerator.nextInt(255);
int green = randomGenerator.nextInt(255);
int blue = randomGenerator.nextInt(255);
panel.addRectangle(new Color(red, green, blue), newWidth[x]);
}
frame.setSize(200, 200);
frame.add(panel);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public class MyRectangle {
private Color color;
private Rectangle rectangle;
public MyRectangle(Color color, int width) {
this.color = color;
rectangle = new Rectangle(width, 10, width, 30);
}
public Color getColor() {
return color;
}
public Rectangle getRectangle() {
return rectangle;
}
public void draw(Graphics2D g2d) {
g2d.setColor(color);
g2d.fill(rectangle);
}
}
public class DrawPanel extends JPanel {
private List<MyRectangle> rectangles;
public DrawPanel() {
rectangles = new ArrayList<>();
}
public void addRectangle(Color color, int width) {
rectangles.add(new MyRectangle(color, width));
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g.create();
for (MyRectangle rect : rectangles) {
rect.draw(g2d);
}
g2d.dispose();
}
}
}
您可以使用其他布局管理器,但是您的DrawRectangle
组件也必须返回preferredSize
,以便布局管理器不会布局大小为0x0
的组件