Created
June 5, 2010 08:05
-
-
Save pcyu16/426430 to your computer and use it in GitHub Desktop.
java GUI layout test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.awt.BorderLayout; | |
| import javax.swing.JButton; | |
| import javax.swing.JFrame; | |
| import javax.swing.JPanel; | |
| import javax.swing.JTable; | |
| public class LayoutTest extends JFrame{ | |
| public static final int x_start = 150, y_start = 100; | |
| public static final int WIDTH = 500, HEIGHT = 150; | |
| public LayoutTest(){ | |
| super("My title"); | |
| setBounds(x_start, y_start, WIDTH, HEIGHT); | |
| setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
| setLayout(new BorderLayout()); | |
| int button_num = 5; | |
| // ==== set button ==== | |
| JButton [] headButton = new JButton[button_num]; | |
| for(int i=1;i<=button_num;i++) | |
| headButton[i-1] = new JButton("button" + i); | |
| JPanel headPanel = new JPanel(); | |
| for(int i=0;i<button_num;i++) | |
| headPanel.add(headButton[i]); | |
| add(headPanel,BorderLayout.NORTH); | |
| // ==== set table ==== | |
| int row = 3, col = 4; | |
| String [][] data = new String[row][col]; | |
| for(int i=0;i<row;i++) | |
| for(int j=0;j<col;j++) | |
| data[i][j] = "(" + i + ", " + j + ")" ; | |
| String [] head = new String[col]; | |
| for(int i=0;i<col;i++) | |
| head[i] = "head" + i; | |
| JTable table=new JTable(data,head); | |
| // ==== set layout for table ==== | |
| JPanel tabPanel = new JPanel(new BorderLayout()); | |
| tabPanel.add(table.getTableHeader(),BorderLayout.NORTH); | |
| tabPanel.add(table,BorderLayout.CENTER); | |
| JPanel test = new JPanel(); | |
| test.add(tabPanel); | |
| add(test,BorderLayout.CENTER); | |
| } | |
| public static void main(String[] args) { | |
| LayoutTest window = new LayoutTest(); | |
| window.setVisible(true); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment