admin管理员组文章数量:1122853
飞鸽传书
- 简介:飞鸽传书是王大大在给我们上网络编程的时候让我们做的一个例子。具体界面如下:
- 功能:在局域网内能够发现使用飞鸽传书的电脑,并可以与之聊天。实时监听在线用户,并反馈到表格中。
- 代码: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 blocke1.printStackTrace();}//监听mainFramemainFrame.addWindowListener(new WindowListener() {@Overridepublic void windowOpened(WindowEvent e) {// TODO Auto-generated method stub}@Overridepublic void windowIconified(WindowEvent e) {// TODO Auto-generated method stub}@Overridepublic void windowDeiconified(WindowEvent e) {// TODO Auto-generated method stub}@Overridepublic void windowDeactivated(WindowEvent e) {// TODO Auto-generated method stub}@Overridepublic void windowClosing(WindowEvent e) {// TODO Auto-generated method stub}@Overridepublic void windowClosed(WindowEvent e) {// TODO Auto-generated method stubSystem.exit(0);}@Overridepublic 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() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubJFileChooser 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() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubSystem.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);
}
} `
本文标签: 飞鸽传书
版权声明:本文标题:飞鸽传书 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/biancheng/1701513446a423484.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论