熟練使用各種佈局方式 FlowLayout 流佈局 left center right等 BorderLayout 邊框佈局 east west sorth north center AbsoluteLayout 絕對佈局或空佈局 任意拖拽 可以使用各種佈局的嵌套效果,達到需要的目標 佈局後可以在基 ...
熟練使用各種佈局方式
FlowLayout 流佈局 left center right等
BorderLayout 邊框佈局 east west sorth north center
AbsoluteLayout 絕對佈局或空佈局 任意拖拽
可以使用各種佈局的嵌套效果,達到需要的目標
佈局後可以在基本佈局基礎上添加新的JPanel,然後設置JPanel新的佈局方式
製作好友列表視窗
首先,設置總的佈局方式為 BorderLayout
上邊放用戶的圖標,用戶名和用戶的名言,然後左邊添加JLable
JLable lable=new JLable(new ImageIcon("Images/logo.jpg"));
添加圖片
label.setPreferredSize(new Dimension(74,74));
設置標簽尺寸
先添加一個JPanel,使用BorderLayout
下邊放三個按鈕,
先添加一個JPanel,使用BorderLayout,然後左邊添加JPanel使用FlowLayout佈局,右邊添加JPanel使用BorderLayout佈局
左邊佈局添加兩個按鈕,設置axis為left
右邊佈局添加一個按鈕,放到east
中間放一個選項卡
選項卡 JTabbedPane ,然後在其上添加3個JPanel,
分別命名為好友列表 群聊 會話
好友列表視窗效果圖如下:
為該視窗設置皮膚
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
該視窗全部代碼如下:
package com.swift; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.FlowLayout; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTabbedPane; import javax.swing.SwingConstants; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class FriendsDialog extends JDialog { public static void main(String args[]) { JFrame.setDefaultLookAndFeelDecorated(true); JDialog.setDefaultLookAndFeelDecorated(true); try { UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel"); } catch (ClassNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (InstantiationException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IllegalAccessException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (UnsupportedLookAndFeelException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } EventQueue.invokeLater(new Runnable() { public void run() { try { FriendsDialog dialog = new FriendsDialog(); dialog.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); dialog.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public FriendsDialog() { super(); setTitle("飛燕聊天"); setBounds(100, 100, 247, 581); final JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); getContentPane().add(panel, BorderLayout.NORTH); final JLabel label = new JLabel(new ImageIcon("Images/logo.jpg")); label.setText("New JLabel"); panel.add(label, BorderLayout.WEST); label.setPreferredSize(new Dimension(74,74)); final JPanel panel_1 = new JPanel(); panel_1.setLayout(new BorderLayout()); panel.add(panel_1, BorderLayout.CENTER); final JLabel advancingSwiftLabel = new JLabel(); advancingSwiftLabel.setText("Advancing Swift"); panel_1.add(advancingSwiftLabel, BorderLayout.CENTER); final JLabel neverWasterLabel = new JLabel(); neverWasterLabel.setText("Never waste time any more"); panel_1.add(neverWasterLabel, BorderLayout.SOUTH); final JPanel panel_2 = new JPanel(); panel_2.setLayout(new BorderLayout()); getContentPane().add(panel_2, BorderLayout.SOUTH); final JPanel panel_3 = new JPanel(); final FlowLayout flowLayout = new FlowLayout(); flowLayout.setAlignment(FlowLayout.LEFT); panel_3.setLayout(flowLayout); panel_2.add(panel_3); final JButton button = new JButton(); panel_3.add(button); button.setHorizontalTextPosition(SwingConstants.LEFT); button.setHorizontalAlignment(SwingConstants.LEFT); button.setText("設置"); final JButton button_1 = new JButton(); panel_3.add(button_1); button_1.setText("查找"); final JPanel panel_4 = new JPanel(); panel_2.add(panel_4, BorderLayout.EAST); final JButton button_2 = new JButton(); panel_4.add(button_2); button_2.setText("退出"); final JTabbedPane tabbedPane = new JTabbedPane(); getContentPane().add(tabbedPane, BorderLayout.CENTER); final JPanel panel_5 = new JPanel(); tabbedPane.addTab("好友列表", null, panel_5, null); final JPanel panel_6 = new JPanel(); tabbedPane.addTab("群聊", null, panel_6, null); final JPanel panel_7 = new JPanel(); tabbedPane.addTab("聊天記錄", null, panel_7, null); final FlowLayout flowLayout_1 = new FlowLayout(); flowLayout_1.setAlignment(FlowLayout.RIGHT); } }