📄 client.java
字号:
package test;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.util.*;
class client extends JFrame implements ActionListener,ListSelectionListener,TableModelListener{
JButton searchB=new JButton("搜索活动");
JButton edit=new JButton("提交编辑");
JTable table;
String []header={"活动名","活动时间","活动组织人员","活动图片","活动内容","活动评论"};
Vector vHead=new Vector();
Vector vContent=new Vector();
JButton serB=new JButton("寻找活动");
JButton subB=new JButton("提交活动");
JButton calB=new JButton("取消修改");
JButton addB=new JButton("添加活动!");
TableModel model;
ListSelectionModel rowSM;
ListSelectionModel colSM;
public client(){
for(int i=0;i<6;i++){
vHead.addElement(header[i]);
}
table=new JTable(vContent,vHead);
Vector vRow=new Vector();
System.out.println("用户可能在添加活动!");
for(int i=0;i<6;i++){
vRow.addElement(new String("gg"));
}
vContent.addElement(vRow);
table.setColumnSelectionAllowed(true);//使选框成为一个单元格
table.setGridColor(Color.blue);
rowSM=table.getSelectionModel();
colSM=table.getColumnModel().getSelectionModel();
rowSM.addListSelectionListener(this);
colSM.addListSelectionListener(this);
addB.addActionListener(this);
calB.addActionListener(this);
serB.addActionListener(this);
subB.addActionListener(this);
model=table.getModel();
model.addTableModelListener(this);
this.setSize(500,500);
this.setLocation(250,250);
JScrollPane jsp=new JScrollPane(table);
this.add(jsp,"Center");
JPanel jp=new JPanel();
jp.add(addB);
jp.add(calB);
jp.add(serB);
jp.add(subB);
this.add(jp,"South");
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
Vector vRow=new Vector();
if(e.getSource()==addB){
System.out.println("用户可能在添加活动!");
for(int i=0;i<6;i++){
vRow.addElement(new String("gg"));
}
vContent.addElement(vRow);
}
if(e.getSource()==serB){
// new ActivePanel(this);
// setVisible(false);
(new DealClient("127.0.0.1",6000)).startGetServer();
}
}
public void tableChanged(TableModelEvent e){
System.out.println("表格改变了!");
int row=e.getFirstRow();
int column=e.getColumn();
Object data=model.getValueAt(row,column);
try{
System.out.println(data);
table.requestDefaultFocus();
}catch(Exception e1){
}
}
public void valueChanged(ListSelectionEvent e){
if(e.getValueIsAdjusting())return;
if(e.getSource()==rowSM){
ListSelectionModel lsm=(ListSelectionModel)e.getSource();
int selectRow=lsm.getMinSelectionIndex();
System.out.println("行:"+selectRow);
}
if(e.getSource()==colSM){
ListSelectionModel lsm=(ListSelectionModel)e.getSource();
int selectCol=lsm.getMinSelectionIndex();
System.out.println("列:"+selectCol);
}
}
public static void main(String args[]){
new client();
}
}
class ActivePanel extends JDialog implements ActionListener{
String []list={"活动名","活动时间","活动组织人员","活动图片","活动内容","活动评论"};
client c;
JButton cb=new JButton("取消");
JButton sb=new JButton("确定");
JPanel jp[]=new JPanel[6];
JLabel jl[]=new JLabel[6];
JTextField jtf[]=new JTextField[4];
JTextArea jta[]=new JTextArea[2];
JButton jb[]=new JButton[4];
Vector vr[]=new Vector[2];
JList jlist[]=new JList[2];
public ActivePanel(client c){
super(c,true);
this.c=c;
cb.addActionListener(this);
JPanel jp1=new JPanel();
JPanel jp3=new JPanel();
jp1.setLayout(new GridLayout(6,1));
jp3.setLayout(new GridLayout(2,1));
for(int i=0;i<6;i++){
jp[i]=new JPanel();
jl[i]=new JLabel(list[i]);
jp[i].add(jl[i]);
if(i==0||i==1){
jtf[i]=new JTextField(10);
jp[i].add(jtf[i]);
}
if(i==4||i==5){
/* jta[i-4]=new JTextArea(5,10);
JScrollPane jsp=new JScrollPane(jta[i-4]);
jp[i].add(jsp);*/
jtf[i-2]=new JTextField(10);
jb[i-2]=new JButton("添加");
jp[i].add(jtf[i-2]);
jp[i].add(jb[i-2]);
}
if(i==3||i==2){
jb[i-2]=new JButton("添加");
jp[i].add(jb[i-2]);
vr[i-2]=new Vector();
jlist[i-2]=new JList(vr[i-2]);
JScrollPane jsp=new JScrollPane(jlist[i-2],JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
if(i==2)vr[i-2].addElement("活动的组织人员");
if(i==3)vr[i-2].addElement("活动的相关图片");
vr[i-2].addElement("test............");
vr[i-2].addElement("test.............");
vr[i-2].addElement("test...............");
vr[i-2].addElement("test");
vr[i-2].addElement("test");
vr[i-2].addElement("test");
vr[i-2].addElement("test");
vr[i-2].addElement("test");
vr[i-2].addElement("test");
vr[i-2].addElement("test");
vr[i-2].addElement("test");
jp3.add(jsp);
}
jp1.add(jp[i]);
}
JPanel jp2=new JPanel();
jp2.add(cb);
jp2.add(sb);
this.add(jp3,"East");
this.add(jp1,"Center");
this.add(jp2,"South");
this.setSize(500,500);
this.setLocation(250,250);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==cb){
c.setVisible(true);
dispose();
}
}
}
class DealClient extends Thread{
Socket sock;
DataOutputStream out;
DataInputStream in;
boolean running=false;
Vector vContent=new Vector();
public DealClient(String ip,int port){
try{
sock=new Socket(ip,port);
OutputStream os=sock.getOutputStream();
InputStream is=sock.getInputStream();
out=new DataOutputStream(os);
in=new DataInputStream(is);
}catch(Exception e){
System.out.println("创建客户端失败!");
}
}
public void startGetServer(){
running=true;
this.start();
}
public void run(){
while(running){
try{
vContent.addElement(in.readUTF());
System.out.println(in.readUTF());
}catch(Exception e){
System.out.println("接受服务器的内容!");
System.out.println(e);
}
}
}
public void destory(){
running=false;
}
}
/*addWindowListener(new WindowAdapter(){
/*public void winodwClosing(WindowEvent e){
*
*dispose();
*System.exit(0);
*}
*}
**/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -