Java TableLayout

前端 未结 1 887
故里飘歌
故里飘歌 2021-01-12 01:06

Im currently wanting to construct a table type of layout for JPanels. I found out there is a TableLayout for Java but I don\'t how to import it. On the other hand i found ou

1条回答
  •  既然无缘
    2021-01-12 01:20

    Here is an SSCCE of using a TableLayout, (Introduction to TableLayout)

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import layout.TableLayout;
    
    public class TestTableLayout {
    
        public static void main(String args[]) {
    
            JFrame frame = new JFrame("Example of TableLayout");
            frame.setSize(450, 450);
    
            double size[][] = {{10, 75, 75, 75, 75, 75, 10}, // Columns
                {10, 75, 75, 75, 75, 75, 10}}; // Rows
    
            frame.setLayout(new TableLayout(size));
    
    
            String label[] = {"(1,1)", "(1,5)", "(1,3)", "(5,3)", "(3,3)"};
            JButton button[] = new JButton[label.length];
    
            for (int i = 0; i < label.length; i++) {
                button[i] = new JButton(label[i]);
            }
    
    
            frame.add(button[0], "1, 1");
            frame.add(button[1], "1, 5");
            frame.add(button[2], "1, 3");
            frame.add(button[3], "5, 3");
            frame.add(button[4], "3, 3");
    
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    }
    

    The required jar for the TableLayout can be downloaded from here


    Also have a look at : A Visual Guide to Layout Managers ,In case.


    In case you go for GridBagLayout, have a look at : How to Use GridBagLayout

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