學習SWING頭一遭!
發現 swing 中對控件的布局很麻煩,要想整個好看的界面確實是件煩人的事情,我做了一個簡單的界面,代碼如下:
?
package
?gui;
import ?java.awt.BorderLayout;
import ?java.awt.GridLayout;
import ?javax.swing.JButton;
import ?javax.swing.JFrame;
import ?javax.swing.JLabel;
import ?javax.swing.JPanel;
import ?javax.swing.JTextField;
import ?javax.swing.JToolBar;
public ? class ?UserGui? extends ?JFrame?{?
???? static ? final ? long ?serialVersionUID? = ? 1 ;
????JPanel?infBar;
????JButton[]?button? = ? new ?JButton[ 6 ];
????JToolBar?tb;
???? // ?----------
????JLabel?idL? = ? new ?JLabel( " 工號: " );
????JLabel?nameL? = ? new ?JLabel( " 姓名: " );
????JLabel?moneyL? = ? new ?JLabel( " 學歷: " );
????JLabel?workL? = ? new ?JLabel( " 職稱: " );
????JTextField?idT? = ? new ?JTextField( 22 );
????JTextField?nameT? = ? new ?JTextField( 22 );
????JTextField?moneyT? = ? new ?JTextField( 22 );
????JTextField?workT? = ? new ?JTextField( 22 );
???? // ?--------------------------------
???? public ?UserGui(String?name)?{
???????? super (name);
????????setLayout( new ?BorderLayout());
????????setDefaultCloseOperation(EXIT_ON_CLOSE);
????????setSize( 290 ,? 200 );
????????setResizable( false );
????????setToolBar();
????????setInfBar();
????????add(infBar,?BorderLayout.CENTER);
????????add(tb,?BorderLayout.NORTH);
????????setVisible( true );
????}
???? // ?--------------------------------
???? public ? void ?setToolBar()?{
????????tb? = ? new ?JToolBar();
????????tb.setFloatable( false );
????????String[]?t1? = ?{? " 查詢 " ,? " 添加 " ,? " 修改 " ,? " 刪除 " ,? " 應用更改 " ,? " 退出系統 " ?};
???????? for ?( int ?i? = ? 0 ;?i? < ? 6 ;?i ++ )?{
????????????button[i]? = ? new ?JButton(t1[i]);
????????????tb.add(button[i]);
????????}
????}
???? public ? void ?setInfBar()?{
????????infBar? = ? new ?JPanel();
????????infBar.setLayout( new ?BorderLayout());
????????JPanel?left? = ? new ?JPanel();
????????left.setLayout( new ?GridLayout( 4 ,? 1 ));
????????JPanel?right? = ? new ?JPanel();
????????right.setLayout( new ?GridLayout( 4 ,? 1 ));
????????infBar.add(left,?BorderLayout.WEST);
????????infBar.add(right,?BorderLayout.EAST);
????????left.add(idL);
????????left.add(nameL);
????????left.add(moneyL);
????????left.add(workL);
????????right.add(idT);
????????right.add(nameT);
????????right.add(moneyT);
????????right.add(workT);
????}
???? // ?--------------------------------
???? public ? static ? void ?main(String[]?args)?{
????????UserGui?ug? = ? new ?UserGui( " 人事檔案管理系統界面 " );
????}????
}
import ?java.awt.BorderLayout;
import ?java.awt.GridLayout;
import ?javax.swing.JButton;
import ?javax.swing.JFrame;
import ?javax.swing.JLabel;
import ?javax.swing.JPanel;
import ?javax.swing.JTextField;
import ?javax.swing.JToolBar;
public ? class ?UserGui? extends ?JFrame?{?
???? static ? final ? long ?serialVersionUID? = ? 1 ;
????JPanel?infBar;
????JButton[]?button? = ? new ?JButton[ 6 ];
????JToolBar?tb;
???? // ?----------
????JLabel?idL? = ? new ?JLabel( " 工號: " );
????JLabel?nameL? = ? new ?JLabel( " 姓名: " );
????JLabel?moneyL? = ? new ?JLabel( " 學歷: " );
????JLabel?workL? = ? new ?JLabel( " 職稱: " );
????JTextField?idT? = ? new ?JTextField( 22 );
????JTextField?nameT? = ? new ?JTextField( 22 );
????JTextField?moneyT? = ? new ?JTextField( 22 );
????JTextField?workT? = ? new ?JTextField( 22 );
???? // ?--------------------------------
???? public ?UserGui(String?name)?{
???????? super (name);
????????setLayout( new ?BorderLayout());
????????setDefaultCloseOperation(EXIT_ON_CLOSE);
????????setSize( 290 ,? 200 );
????????setResizable( false );
????????setToolBar();
????????setInfBar();
????????add(infBar,?BorderLayout.CENTER);
????????add(tb,?BorderLayout.NORTH);
????????setVisible( true );
????}
???? // ?--------------------------------
???? public ? void ?setToolBar()?{
????????tb? = ? new ?JToolBar();
????????tb.setFloatable( false );
????????String[]?t1? = ?{? " 查詢 " ,? " 添加 " ,? " 修改 " ,? " 刪除 " ,? " 應用更改 " ,? " 退出系統 " ?};
???????? for ?( int ?i? = ? 0 ;?i? < ? 6 ;?i ++ )?{
????????????button[i]? = ? new ?JButton(t1[i]);
????????????tb.add(button[i]);
????????}
????}
???? public ? void ?setInfBar()?{
????????infBar? = ? new ?JPanel();
????????infBar.setLayout( new ?BorderLayout());
????????JPanel?left? = ? new ?JPanel();
????????left.setLayout( new ?GridLayout( 4 ,? 1 ));
????????JPanel?right? = ? new ?JPanel();
????????right.setLayout( new ?GridLayout( 4 ,? 1 ));
????????infBar.add(left,?BorderLayout.WEST);
????????infBar.add(right,?BorderLayout.EAST);
????????left.add(idL);
????????left.add(nameL);
????????left.add(moneyL);
????????left.add(workL);
????????right.add(idT);
????????right.add(nameT);
????????right.add(moneyT);
????????right.add(workT);
????}
???? // ?--------------------------------
???? public ? static ? void ?main(String[]?args)?{
????????UserGui?ug? = ? new ?UserGui( " 人事檔案管理系統界面 " );
????}????
}
posted on 2006-07-08 00:37 銀河海盜 閱讀(323) 評論(0) 編輯 收藏 所屬分類: JAVA