Drawing a gridline in the same part where I created the JPanel?

后端 未结 2 953
广开言路
广开言路 2021-01-15 11:43

I need to build a gridline for a crossword puzzle. I wanted to know if I could do it in the same part where I created the JPanel and it\'s properties instead of doing it in

相关标签:
2条回答
  • 2021-01-15 12:01

    First off, never do this: setLayout(null);. While null layouts and setBounds() might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one.

    But more to your original point, my suggestion is not to draw gridlines. Instead consider using a JPanel that holds a GridLayout, you can give it a horizontal and vertical gap of 1, and set is background to black if you want to show gridlines, and then fill it with either JLabels or JTextFields that accept one single char.

    0 讨论(0)
  • 2021-01-15 12:02

    There is a multitide of ways that this might be done, one of the simplest I can think of would be to use a component of some kind for each cell and a series of MatterBorders as the "grid lines"

    I also agree with HovercraftFullOfEels, avoid using null layouts, pixel perfect layouts are an illusion within modern UI design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify

    enter image description here

    import java.awt.Color;
    import java.awt.EventQueue;
    import java.awt.GridLayout;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    import javax.swing.border.Border;
    import javax.swing.border.MatteBorder;
    
    public class Test {
    
        public static void main(String[] args) {
            new Test();
        }
    
        public Test() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public static class TestPane extends JPanel {
    
            protected static final Border TOP_LEFT = new MatteBorder(1, 1, 1, 0, Color.DARK_GRAY);
            protected static final Border TOP_RIGHT = new MatteBorder(1, 1, 1, 1, Color.DARK_GRAY);
            protected static final Border BOTTOM_LEFT = new MatteBorder(0, 1, 1, 0, Color.DARK_GRAY);
            protected static final Border BOTTOM_RIGHT = new MatteBorder(0, 1, 1, 1, Color.DARK_GRAY);
    
            public TestPane() {
                setLayout(new GridLayout(10, 10));
                for (int row = 0; row < 10; row++) {
                    for (int col = 0; col < 10; col++) {
                        Border border = null;
                        int index = (row * 10) + col;
                        if (row == 0) {
                            if (col == 9) {
                                border = TOP_RIGHT;
                            } else {
                                border = TOP_LEFT;
                            }
                        } else if (row == 9) {
                            if (col == 9) {
                                border = BOTTOM_RIGHT;
                            } else {
                                border = BOTTOM_LEFT;
                            }
                        } else if (col == 9) {
                            border = BOTTOM_RIGHT;
                        } else {
                            border = BOTTOM_LEFT;
                        }
                        JLabel cell = new JLabel(" ");
                        cell.setBorder(border);
                        add(cell);
                    }
                }
            }
    
        }
    
    }
    

    Take a look at How to Use Borders, Laying Out Components Within a Container and How to Use GridLayout for more details

    0 讨论(0)
提交回复
热议问题