主要Swing組件小結(一)

窗口類JFrame:

setTitle();設置窗口標題;

setBounds();位置及大小;

setDefaultCloseOperation();設置窗體關閉按鈕事件:JFrame.[EXIT_ON_CLOSE | DO_NOTHING_ON_CLOSE | DISPOSE_ON_CLOSE | HIDE_ON_CLOSE];

getContentPane();獲得子組件JRootPane對象;

getContentPane().add();添加組件;

getContentPane().setLayout();設置rootpane的佈局管理器,可以爲null;


按鈕類JButton:

setMargin(Insets m);設置上,左,下,右的margin;

setContentAreaFilled(boolean b);設置是否繪製按鈕內容區域;

setBorderPainted(boolean b)設置是否有邊框;

setIcon();設置按鈕顯示圖標;

setRolloverIcon();設置鼠標hover的圖標;

setPressedIcon();設置鼠標按下的圖標;


標籤類JLabel:

setText();顯示文本,也可在構造函數中設置;

setHorizontalAlignment();文本對齊方式;

setHorizontalTextPosition();文字相對於圖片的水平位置;

setVerticalTextPosition();文字相對於圖片的垂直位置;

setIcon();設置顯示圖片;


單選按鈕JRadioButton,ButtonGroup和複選框組件JCheckBox:

JRadioButton類:

setSelected();是否備選;

setText();顯示文字;

ButtonGroup類:

remove(JRadioButton radioButton);刪除單選按鈕;

add(JRadioButton radioButton);添加~;


JCheckBox類:

setText();顯示文字;

setSelected();是否備選;


選擇框組件JComboBox和JList:

JComboBox類:

setEditable(boolean b);是否可編輯

setMaximumRowCount();設置彈出選框是最大顯示行數;

insertItemAt(String string, int index);在index處插入string;

setSelectedItem();設置顯示(被選項)項;


JList類:

setVisibleRowCount();設置顯示行數;

setFixedCellHeight(int height);設置選項高度;

setSelectionMode(ListSelectionMode.[Single_Selection | SINGLE_SELECTION | SINGLE_INTERVAL_SELECTION | MULTIPLE_INTERVAL_SELECTION]);

通過JScrollPane來使用JList對象:

JScrollPane類:

setViewportView(comp | List);添加List進JScrollPane;


輸入框組件JTextField,JTextArea,JPasswordField:

JTextField類:

setHorizontalAlignment();


JTextArea類(也要通過JScrollPane類):

setRows();行數設置;

setColumns();列數設置;

setLineWrap(Boolean b);是否自動換行;


Dimension類(尺寸類):

Dimension dimension = textArea.getPreferredSize();獲得文本域的首選大小;

JScrollPane.setViewportView(textArea);

JScrollPane.setBounds(x, y, dimension.width, dimension.height);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章