📄 clientmain.java
字号:
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;
import java.net.*;
public class ClientMain extends JFrame implements ActionListener,KeyListener
{
JPanel jpamain;
JPanel jpaup;
JPanel jpamid;
JPanel jpadon;
JLabel jlbname;
JLabel jtfname;
JLabel jlbid;
JLabel jtfid;
JButton jbtanswer;
JLabel jlbinfo;
JPanel jpaeast;
JPanel jpawest;
OutputStream ops;
Socket socket;
public ClientMain(String name,String ID,Socket socket)
{
this.setTitle("网络抢答器客户端");
this.setBounds(100, 100, 350, 350);
jpamain=new JPanel();
this.add(jpamain);
jpamain.setBackground(Color.LIGHT_GRAY);
jpamain.setLayout(new BorderLayout(3,1));
jpaup=new JPanel();
jpamid=new JPanel();
jpadon=new JPanel();
jpamain.add(jpaup,BorderLayout.NORTH);
jpamain.add(jpamid,BorderLayout.CENTER);
jpamain.add(jpadon,BorderLayout.SOUTH);
jpaup.setBackground(new Color(85,141,142));
jpamid.setBackground(new Color(85,141,142));
jpadon.setBackground(new Color(85,141,142));
jlbname=new JLabel("姓名");
jtfname=new JLabel();
jlbid=new JLabel("序号");
jtfid=new JLabel();
jpaup.add(jlbname);
jpaup.add(jtfname);
jpaup.add(jlbid);
jpaup.add(jtfid);
jlbname.setFont(new Font("宋体",Font.BOLD,15));
jtfname.setFont(new Font("宋体",Font.BOLD,15));
jlbid.setFont(new Font("宋体",Font.BOLD,15));
jtfid.setFont(new Font("宋体",Font.BOLD,15));
jbtanswer=new JButton("抢答按钮");
jpamid.setLayout(null);
jbtanswer.setBounds( 80, 30,200, 200);
jbtanswer.setBackground(new Color(123,175,176));
jbtanswer.setForeground(Color.red);
jbtanswer.setFont(new Font("宋体",Font.BOLD,35));
jpamid.add(jbtanswer);
jlbinfo=new JLabel();
jlbinfo.setText(" 按抢答按钮或回车键抢答,加油哦!");
jlbinfo.setFont(new Font("宋体",Font.BOLD,15));
jlbinfo.setForeground(Color.blue);
jpadon.add(jlbinfo);
jbtanswer.addActionListener(this);
jbtanswer.addKeyListener(this);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.socket=socket;
jtfname.setText(name);
jtfid.setText(ID);
//jtfname.setEditable(false);
//jtfid.setEditable(false);
}
public void actionPerformed(ActionEvent e)
{
try
{
ops=socket.getOutputStream();
ops.write(jtfid.getText().getBytes());
}
catch(Exception e1)
{
e1.printStackTrace();
}
}
public void keyPressed(KeyEvent e)
{
Object obj=e.getSource();
if(e.getKeyCode()==KeyEvent.VK_ENTER )
{
try
{
System.out.println("aaaaaaa");
ops=socket.getOutputStream();
ops.write(jtfid.getText().getBytes());
}
catch(Exception e1)
{
e1.printStackTrace();
}
}
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -