Guest User

Untitled

a guest
May 27th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import javax.swing.JButton;
  3. import javax.swing.JFrame;
  4. import javax.swing.JPanel;
  5. import javax.swing.JTable;
  6.  
  7. public class LayoutTest extends JFrame{
  8. public static final int x_start = 150, y_start = 100;
  9. public static final int WIDTH = 500, HEIGHT = 150;
  10.  
  11. public LayoutTest(){
  12. super("My title");
  13. setBounds(x_start, y_start, WIDTH, HEIGHT);
  14. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15.  
  16. setLayout(new BorderLayout());
  17.  
  18. int button_num = 5;
  19.  
  20. // ==== set button ====
  21. JButton [] headButton = new JButton[button_num];
  22. for(int i=1;i<=button_num;i++)
  23. headButton[i-1] = new JButton("button" + i);
  24.  
  25. JPanel headPanel = new JPanel();
  26.  
  27. for(int i=0;i<button_num;i++)
  28. headPanel.add(headButton[i]);
  29.  
  30. add(headPanel,BorderLayout.NORTH);
  31.  
  32. // ==== set table ====
  33. int row = 3, col = 4;
  34. String [][] data = new String[row][col];
  35. for(int i=0;i<row;i++)
  36. for(int j=0;j<col;j++)
  37. data[i][j] = "(" + i + ", " + j + ")" ;
  38.  
  39. String [] head = new String[col];
  40. for(int i=0;i<col;i++)
  41. head[i] = "head" + i;
  42.  
  43. JTable table=new JTable(data,head);
  44.  
  45. // ==== set layout for table ====
  46. JPanel tabPanel = new JPanel(new BorderLayout());
  47. tabPanel.add(table.getTableHeader(),BorderLayout.NORTH);
  48. tabPanel.add(table,BorderLayout.CENTER);
  49.  
  50. JPanel test = new JPanel();
  51. test.add(tabPanel);
  52.  
  53. add(test,BorderLayout.CENTER);
  54. }
  55.  
  56. public static void main(String[] args) {
  57. LayoutTest window = new LayoutTest();
  58. window.setVisible(true);
  59. }
  60. }
Add Comment
Please, Sign In to add comment