设计的聊天窗口如下:
制作过程:
首先,在默认的BorderLayout视图下,
上边也就是North处添加一个JPanel,将Layout调整为BorderLayout,West放一个JLabel用来放照片,
Center再放一个JPanel,布局调整为BorderLayout,Center和South各方一个JLabel,分别放用户昵称和名言。
下边模仿QQ是一个上下可以自行拆分大小的JSplitPane,调整为垂直的上下模式。
拆分框的下边放一个JPanel,设布局为BorderLayout,
再在上中下分别放JPanel,JScrollPane,JPanel,
上边放一个按钮调整为左对齐,下边放两个按钮调整为右对齐,中间放一个JTextArea
最后,拆分框上边放一个JScrollPane,里边放一个JTextArea。
全部完整代码如下:
package com.swift; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.FlowLayout; 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.JScrollPane; import javax.swing.JSplitPane; import javax.swing.JTextArea; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class ChatFrame extends JFrame { private JTextArea textArea_1; private JTextArea textArea; 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 { ChatFrame frame = new ChatFrame(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public ChatFrame() { super(); setTitle("飞燕—聊天窗"); setBounds(100, 100, 558, 576); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JPanel panel = new JPanel(); panel.setLayout(new BorderLayout(5,5)); getContentPane().add(panel, BorderLayout.NORTH); final JLabel label = new JLabel(new ImageIcon("Images/logo.jpg")); panel.add(label, BorderLayout.WEST); label.setText("New JLabel"); 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 neverWasteTimeLabel = new JLabel(); neverWasteTimeLabel.setText("Never waste time any more"); panel_1.add(neverWasteTimeLabel, BorderLayout.SOUTH); final JSplitPane splitPane = new JSplitPane(); splitPane.setDividerLocation(300); splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT); getContentPane().add(splitPane, BorderLayout.CENTER); final JPanel panel_2 = new JPanel(); panel_2.setLayout(new BorderLayout()); splitPane.setRightComponent(panel_2); final JPanel panel_3 = new JPanel(); final FlowLayout flowLayout = new FlowLayout(); flowLayout.setAlignment(FlowLayout.LEFT); panel_3.setLayout(flowLayout); panel_2.add(panel_3, BorderLayout.NORTH); final JButton button = new JButton(); button.setText("字体"); panel_3.add(button); final JPanel panel_4 = new JPanel(); final FlowLayout flowLayout_1 = new FlowLayout(); flowLayout_1.setAlignment(FlowLayout.RIGHT); panel_4.setLayout(flowLayout_1); panel_2.add(panel_4, BorderLayout.SOUTH); final JButton button_1 = new JButton(); button_1.setText("关闭"); panel_4.add(button_1); final JButton button_2 = new JButton(); button_2.setText("发送"); panel_4.add(button_2); final JScrollPane scrollPane = new JScrollPane(); panel_2.add(scrollPane, BorderLayout.CENTER); textArea_1 = new JTextArea(); scrollPane.setViewportView(textArea_1); final JScrollPane scrollPane_1 = new JScrollPane(); splitPane.setLeftComponent(scrollPane_1); textArea = new JTextArea(); scrollPane_1.setViewportView(textArea); } }
时间: 2024-10-10 04:06:11