如何重绘BorderLayout中的JPanels

时间:2017-04-07 20:02:15

标签: java swing grid jpanel repaint

在我的borderlayout中,我有JPanels,由数组theGrid引用。在一个单独的函数中,我想改变JPanel的外观,所以我改变了应该如何绘制特定的JPanel。我遇到的问题是,我不知道我现在如何绘制这个新版本的JPanel?

我已经查看了其他问题,并尝试在mainPanel或contentPane上使用.revalidate,.validate,.repaint()等,或两者都没有,但是没有人会获得我创建的新版JPanel

在下面的构造函数中,我设置了网格以及它如何适应JFrame的其余部分

 public class GraphicDisplay extends JFrame {

        private static int ROWS = 6;
        private static int COLS = 7;
        private JPanel[][] theGrid = new JPanel[ROWS][COLS];
        private JPanel mainPanel = new JPanel(new GridLayout(ROWS, COLS));
        private Container contentPane;

        public GraphicDisplay() {
            for (int i = 0; i < theGrid.length; i++) { //Initialize theGrid (with blanks)
                for (int j = 0; j < theGrid[i].length; j++) {
                    theGrid[i][j] = new JPanel();
                }
            }

            //add them to the JFrame
            contentPane = getContentPane();
            contentPane.setLayout(new BorderLayout());

            JPanel boardElements = new JPanel();
            boardElements.setLayout(new BoxLayout(boardElements, BoxLayout.Y_AXIS)); //vertical layout for the two parts, theGrid itself and then the
            // button which goes underneath, 

            final int SPACE = 3;
            final Color COLORCHOICE = Color.BLACK;


            mainPanel.setBorder(BorderFactory.createEmptyBorder(SPACE, SPACE, SPACE, SPACE));
            mainPanel.setBackground(COLORCHOICE);
            JPanel[][] panels = new JPanel[ROWS][COLS];
            for (int i = 0; i < panels.length; i++) {
                for (int j = 0; j < panels[i].length; j++) {
                    panels[i][j] = new JPanel(new GridLayout(1, 1, 1, 1));
                    panels[i][j].setBackground(COLORCHOICE);
                    panels[i][j].setBorder(BorderFactory.createEmptyBorder(SPACE, SPACE, SPACE, SPACE));

                    mainPanel.add(panels[i][j]);


                    panels[i][j].add(theGrid[i][j]);


                }
            }
            //adding the grid to the vertical layout
            boardElements.add(mainPanel);
            //adding the button which will go directly under the grid
            boardElements.add(new JButton("Button"));

            contentPane.add(boardElements, BorderLayout.CENTER);

        }

下面的函数属于同一类,假设通过在点3,3处添加一个点来更新网格(但问题是我没有看到任何明显的变化)

    public void addDotToGrid() {
                   //theGrid[3][3] reference was added to the panels array which is part of the layout, so I would of thought by changing the value of it here would then change this JPanel on the UI
                    theGrid[3][3] = new JPanel() {
                        public void paintComponent( Graphics g) {
                            //g.setColor(Color.RED);
                            int y = 0;
                            int diameter = getWidth() -10;
                            int x = (getWidth()/2) - (diameter/2);
                            super.paintComponent(g);
                            Graphics2D g2d = (Graphics2D)g;
                            // Assume x, y, and diameter are instance variables.
                            Ellipse2D.Double circle = new Ellipse2D.Double(x, y, diameter, diameter);
                            g2d.fill(circle);

                        }
                    };


}

另一个创建GraphicDisplay对象的类的主方法

public static void main(String[] args) {

            GraphicDisplay display2 = new GraphicDisplay();
            display2.setSize(600, 600);
            display2.setVisible(true);
            display2.addDotToGrid();
        }

问题是网格显示但是addDotToGrid()没有改变任何东西,并且没有添加到网格的点

1 个答案:

答案 0 :(得分:4)

您正在更改由theGrid数组保存的JPanel,但这对GUI中显示的JPanel没有任何影响,这会影响变量与引用或对象之间的关键区别 - 更改对象变量引用对它先前引用的对象没有影响。您的解决方案是更改网格所持有的JPanel。

这样做的一种方法是给所有绘制你想要的paintComponent方法,但让它们用布尔值控制,然后改变感兴趣的面板的布尔变量。

如果这是我的GUI,我会创建一个JLabel网格,只需在我想要的地方交换ImageIcons - 尽可能保持简单!

例如

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

import javax.swing.*;

@SuppressWarnings("serial")
public class MyGridEg extends JPanel {
    private static final int SPACE = 6;
    private static final int ROWS = 6;
    private static final int COLS = 7;
    private static final int IMG_W = 80;
    private static final int SML_GAP = 3;
    private static final Color IMG_BACKG = new Color(240, 240, 240);
    private static final String TITLE = "Click on a Cell";

    private JLabel[][] labelGrid = new JLabel[ROWS][COLS];
    private Icon blankIcon = createIconDisk(new Color(0, 0, 0, 0));
    private Icon redIcon = createIconDisk(Color.RED);

    public MyGridEg() {
        MyMouse myMouse = new MyMouse();
        JPanel gridHolder = new JPanel(new GridLayout(ROWS, COLS, SPACE, SPACE));
        gridHolder.setBackground(Color.BLACK);
        for (int i = 0; i < labelGrid.length; i++) {
            for (int j = 0; j < labelGrid[i].length; j++) {
                JLabel label = new JLabel(blankIcon);
                label.addMouseListener(myMouse);
                labelGrid[i][j] = label;
                gridHolder.add(label);
            }
        }
        gridHolder.setBorder(BorderFactory.createLineBorder(Color.black, SPACE));

        JLabel titleLabel = new JLabel(TITLE, SwingConstants.CENTER);
        titleLabel.setFont(titleLabel.getFont().deriveFont(Font.BOLD, 20));

        JButton clearButton = new JButton(new ClearAction("Clear"));
        JPanel btnPanel = new JPanel();
        btnPanel.add(clearButton);

        setLayout(new BorderLayout());
        add(gridHolder, BorderLayout.CENTER);
        add(btnPanel, BorderLayout.PAGE_END);
        add(titleLabel, BorderLayout.PAGE_START);
    }

    private Icon createIconDisk(Color color) {
        BufferedImage img = new BufferedImage(IMG_W, IMG_W, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = img.createGraphics();
        g2.setBackground(IMG_BACKG);
        g2.clearRect(0, 0, IMG_W, IMG_W);
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(color);
        int x = SML_GAP;
        int y = x;
        int width = IMG_W - 2 * x;
        int height = IMG_W - 2 * y;
        g2.fillOval(x, y, width, height);
        g2.dispose();
        return new ImageIcon(img);
    }

    private class MyMouse extends MouseAdapter {
        @Override
            public void mousePressed(MouseEvent e) {
                JLabel selected = (JLabel) e.getSource();
                Icon icon = selected.getIcon() == blankIcon ? redIcon : blankIcon;
                selected.setIcon(icon);
            }
    }

    private class ClearAction extends AbstractAction {
        public ClearAction(String name) {
            super(name);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            for (JLabel[] labelRow : labelGrid) {
                for (JLabel cell : labelRow) {
                    cell.setIcon(blankIcon);
                }
            }
        }
    }

    private static void createAndShowGui() {
        MyGridEg mainPanel = new MyGridEg();

        JFrame frame = new JFrame("MyGridEg");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

这段代码的关键是我没有交换JLabel或JPanels,而是让我保持JLabel不变,但我正在改变它们的状态。这样,如果我改变2D数组labelGrid所持有的JLabel的状态,这可以通过视图中的更改来反映,就像我在clearButton JButton调用的ClearAction类中所做的那样:

private class ClearAction extends AbstractAction {
    public ClearAction(String name) {
        super(name);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        for (JLabel[] labelRow : labelGrid) {
            for (JLabel cell : labelRow) {
                cell.setIcon(blankIcon);
            }
        }
    }
}
相关问题