- 简介:飞鸽传书是王大大在给我们上网络编程的时候让我们做的一个例子。具体界面如下:
- 功能:在局域网内能够发现使用飞鸽传书的电脑,并可以与之聊天。实时监听在线用户,并反馈到表格中。
- 代码:import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.TextArea;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.util.MissingFormatArgumentException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.RepaintManager;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
public class MainWnd{
JFrame mainFrame=new JFrame(“飞鸽传书”);;
JButton btnSend;
JPanel jpanelScrolPane=new JPanel(new BorderLayout());
JSplitPane splitPane;
public UserList userList=new UserList();
JScrollPane scrolPane=new JScrollPane(userList.tblUserListInit());
//整体初始化
public void init(){
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
| UnsupportedLookAndFeelException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//监听mainFrame
mainFrame.addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
});
//设置mainFrame的一些基础属性。
mainFrame.setSize(640, 480);
initStartPos();
initSplitPanel();
mainFrame.setMinimumSize(new Dimension(300, 200));
mainFrame.setVisible(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//将mainFrame设置在屏幕中心。
private void initStartPos(){
Dimension srcDim = Toolkit.getDefaultToolkit().getScreenSize();
Dimension mainwndDim=mainFrame.getSize();
mainFrame.setLocation((srcDim.width-mainwndDim.width)/2, (srcDim.height-mainwndDim.height)/2);
}
//初始化布局
private void initSplitPanel(){
splitPane=new JSplitPane();
splitPane.setDividerSize(10);
splitPane.setDividerLocation(150);
splitPane.setContinuousLayout(true);//操作箭头,重绘图形
splitPane.setOneTouchExpandable(true);//让分割线显示出箭头
splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
initBottomArea(splitPane);
initTopArea(splitPane);
splitPane.setVisible(true);
mainFrame.setContentPane(splitPane);
}
//初始化按钮‘发送(S)’,并给按钮添加打开文件获得文件大小的功能。
private void initBtnSend(JSplitPane splitPane,JPanel jpanel){
JPanel jlabelButton=new JPanel();
btnSend=new JButton("发送(S)");
btnSend.setMnemonic(83);
btnSend.setSize(60, 16);
btnSend.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JFileChooser jFileChooser=new JFileChooser();
jFileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
jFileChooser.setDialogTitle("请选择文件:");
jFileChooser.showOpenDialog(null);
if(jFileChooser.getSelectedFile() != null){
File file=jFileChooser.getSelectedFile();
JOptionPane.showMessageDialog( null, file.getName()+" "+file.length()/1024+"kb");
}
}
});
jlabelButton.add(btnSend);
jpanel.add(jlabelButton,BorderLayout.SOUTH);
}
//初始化顶部区域
private void initTopArea(JSplitPane splitPane){
JButton refresh=new JButton("刷新(R)");
JLabel textTab=new JLabel("在线人数:");
JLabel textNum=new JLabel("0");
JPanel rigthArea=new JPanel(new FlowLayout());
rigthArea.setPreferredSize(new Dimension(70,150));
rigthArea.setMinimumSize(new Dimension(70, 100));
textTab.setPreferredSize(new Dimension(70, 40));
rigthArea.add(textTab);
rigthArea.add(textNum);
rigthArea.add(refresh);
jpanelScrolPane.add(scrolPane,BorderLayout.CENTER);
jpanelScrolPane.add(rigthArea,BorderLayout.EAST);
splitPane.setTopComponent(jpanelScrolPane);
refresh.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("我被电击了,系一个");
}
});
}
//初始化底部区域
private void initBottomArea(JSplitPane splitPane){
TextArea textArea=new TextArea();
JPanel bottomandTopArea=new JPanel(new BorderLayout());
bottomandTopArea.add(textArea,BorderLayout.CENTER);
initBtnSend(splitPane,bottomandTopArea);
splitPane.setBottomComponent(bottomandTopArea);
}
} `