并排显示多个矩形

时间:2017-03-19 09:41:26

标签: java swing

现在我已经创建了一个名为ColorRectPanel(JPanel)的类,并覆盖了方法(paintComponent)以绘制矩形(具有x,y,width和height)。当我在JFrame中添加一个ColorRectPanel时,它可以工作,但是当我尝试添加多个ColorRectPanel时失败。我试过使用BoxLayout。但是6个ColorRectPanels 不是并排的。我是说如何放置它们使彼此之间的距离为0。 这是ColorRectPanel:

public class ColorRectPanel extends JPanel 
{   
    private ArrayList<ColorRect> colorRects = new ArrayList<ColorRect>();    //store Rectangles
    private int firstLocation;
    public ColorRectPanel(int x){
        firstLocation = x;
    }
    public void addRect(String color)
    {
        ColorRect[] colorRect = new ColorRect[7];
        char[] b = color.toCharArray();
        for (int i=0;i<7;i++){
            colorRect[i] = new ColorRect();
            colorRect[i].x = firstLocation + i * 5;
            colorRect[i].y = 250;
            colorRect[i].width = 5;
            colorRect[i].height = 500;
            if (b[i] == '0')
                colorRect[i].color = Color.white;
            else
                colorRect[i].color = Color.black;
             colorRects.add(colorRect[i]);
        }
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        for (ColorRect colorRect : colorRects)    //画矩形
        {
            g.setColor(colorRect.color);
            g.fillRect(colorRect.x, colorRect.y, colorRect.width, colorRect.height);
        }
    }

    //data struct
    private class ColorRect
    {
        public int x;
        public int y;
        public int width;
        public int height;
        public Color color;
    }
}

1 个答案:

答案 0 :(得分:0)

我认为你用更简单的方法得到了理想的结果:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class ColorRectPanel extends JPanel
{
   //extends JPanel means it IS a JPanel. It is best to remove
   //none relevant functionality from it

    public ColorRectPanel(int numberOfRectangles){

        setLayout(new GridLayout(0, numberOfRectangles));
    }

    public static void main(String[] args){

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        int numberOfRectangles =7;
        JPanel cr = new ColorRectPanel(numberOfRectangles);
        for (int i=0;i<numberOfRectangles;i++){

            JPanel panel = new JPanel(); //create a panel
            panel.setPreferredSize(new Dimension(5,500)); //size it 
            if ((i%2) ==0) { //color it 
                panel.setBackground(Color.white);
            } else {
                panel.setBackground(Color.BLACK);
            }

            cr.add(panel);//add it
        }

        frame.getContentPane().add(cr);
        frame.pack();
        frame.setVisible(true);
    }
}

您也可以将其更改为逐步添加面板:

public class ColorRectPanel extends JPanel
{

    List<Component> components ;
    public ColorRectPanel(){

        components = new ArrayList<>();
    }

    @Override
    public Component add(Component comp) {

        if(comp != null) {
            refresh(comp);
        }
        return comp;
    }

    private void refresh(Component comp) {

        components.add(comp);
        setLayout(new GridLayout(0,components.size()));
        removeAll();
        for(Component c : components) {
            super.add(c);
        }
    }

    public static void main(String[] args){

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        int numberOfRectangles =7;
        JPanel cr = new ColorRectPanel();
        for (int i=0;i<numberOfRectangles;i++){

            JPanel panel = new JPanel(); //create a panel
            panel.setPreferredSize(new Dimension(5,500)); //size it
            if ((i%2) ==0) { //color it
                panel.setBackground(Color.white);
            } else {
                panel.setBackground(Color.BLACK);
            }

            cr.add(panel);//add it
        }

        frame.getContentPane().add(cr);
        frame.pack();
        frame.setVisible(true);
    }
}