简体   繁体   English

在 JPanel 上显示网格布局

[英]Make a grid layout displaying on JPanel

My task is to create checked board on JPAnel.我的任务是在 JPAnel 上创建检查板。 For that purpose I'm trying to fill in parent JPanel with JPanels that has borders, but for some reason code doesnt give desired result and no error shown to do investigation why.为此,我试图用有边框的 JPanel 填充父 JPanel,但由于某种原因,代码没有给出预期的结果,也没有显示错误来调查原因。 Here is the code:这是代码:

private static class GlassView extends JFrame {

        private static int width = 600;
        private static int height = 750;

        public GlassView() {
            this.setSize(width, height);
            this.setVisible(true);
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        }

        public static void workingFrame() {
            int cols = 0;
            int rows = 0;
            String frameName = "Bot World";

            WorkFrame workF = new WorkFrame(0, 0, frameName);
            wfFrame = workF.newFrame();
            wfFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
            wfFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            wfFrame.setVisible(true);

            JSplitPane splitPane = new JSplitPane();
            splitPane.setSize(width, height);
            splitPane.setDividerSize(0);
            splitPane.setDividerLocation(150);
            splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);

            JPanel panelLeft = createLftPanel();
            JPanel panelRight = createRightPanel();

            splitPane.setLeftComponent(panelLeft);
            splitPane.setRightComponent(panelRight);
            wfFrame.add(splitPane);
        }
    }

Here is the code for the panelRight which needs to be cheked:这是需要检查的 panelRight 的代码:

public static JPanel createRightPanel() {
        JPanel panel = new JPanel();
        int rows = 100;
        int cols = 100;
        panel.setLayout(new GridLayout(rows, cols));
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                JPanel pane = new JPanel();
                pane.add(new JTextField("both"));
                pane.setBorder(BorderFactory.createLineBorder(Color.black));
                panel.add(new JButton(""));
            }
        }
        return panel;
    }

Any help will be appreciated.任何帮助将不胜感激。 Thank you谢谢

Ok, my (second) guess is that the frame isn't laid out again after the call to好的,我的(第二个)猜测是在调用

wfFrame.setVisible(true);

For me the example below:对我来说,下面的例子:

public class Framed {
    public static void workingFrame() {
        JFrame frame = new JFrame();
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.getContentPane().add(createRightPanel(10, 10));

        frame.revalidate(); // <-- HERE
    }

    public static JPanel createRightPanel(int rows, int cols) {
        JPanel panel = new JPanel(new GridLayout(rows, cols));
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                JPanel pane = new JPanel();
                pane.setBackground((i+j)%2==0?Color.black:Color.white);
                panel.add(pane);
            }
        }
        return panel;
    }

    public static void main(String... none) throws Exception {
        workingFrame();
    }
}

Shows a checker grid, but is you remove the call to显示一个检查网格,但您是否删除了对

        frame.revalidate(); // <-- HERE

Then the gird is not displayed (until you do something with the frame that causes it to lay out again).然后不显示网格(直到您对框架执行某些操作以使其再次布局)。 Better than calling revalidate() though may be to call setVisible only after all the components have been added.比调用 revalidate() 更好的是,只有在添加了所有组件之后才调用 setVisible。

The code编码

            JPanel pane = new JPanel();
            pane.add(new JTextField("both"));
            pane.setBorder(BorderFactory.createLineBorder(Color.black));
            panel.add(new JButton(""));

creates the pane object but doesn't add it to any other UI component.创建窗格object 但不将其添加到任何其他 UI 组件。 Instead just the new JButton("") is added to panel.相反,只是将新的 JButton("")添加到面板中。

Maybe you are intended:也许你打算:

             panel.add(pane);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM