熟练使用各种布局方式
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); } }