⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kuframe.java

📁 这是java写的一个抢答器
💻 JAVA
字号:
package Frame;

import javax.swing.*;
import java.awt.Toolkit;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.Socket;
import java.io.OutputStream;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class KuFrame extends JFrame {
    public KuFrame() {
        super();
        this.getContentPane().setLayout(null);
        setSize(_width,_height);
        Toolkit kit=Toolkit.getDefaultToolkit();
        Dimension dim=kit.getScreenSize();
        setLocation(dim.width/2-_width/2,dim.height/2-_height/2);
        setVisible(true);
        try {
            jbInit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private int _width=350;
    private int _height=150;
    private String IP=null;
    private Socket s;
    private JLabel IPLab=new JLabel("输入服务器IP:");
    private JLabel UserNameLab=new JLabel("输入您的名字:");
    private JTextArea IPText=new javax.swing.JTextArea("");
    private JTextArea UserNameText=new JTextArea("");
    private JButton TestButton=new JButton("测  试");
    private JButton OkButton=new JButton("确  定");
    private JButton ExitButton=new JButton("退  出");

    public static void main(String[] args) {
        KuFrame kuframe = new KuFrame();
    }

    private void jbInit() throws Exception {
        IPLab.setBounds(3,10,100,20);
         IPLab.setVisible(true);
        TestButton.addActionListener(new KuFrame_TestButton_actionAdapter(this));
        OkButton.addActionListener(new KuFrame_OkButton_actionAdapter(this));
        ExitButton.addActionListener(new KuFrame_ExitButton_actionAdapter(this));
        this.getContentPane().add(IPLab);
         IPText.setBounds(103,10,120,20);
         IPText.setVisible(true);
         this.getContentPane().add(IPText);
         UserNameLab.setBounds(3,35,100,20);
         UserNameLab.setVisible(true);
         this.getContentPane().add(UserNameLab);
         UserNameText.setBounds(103,35,120,20);
         UserNameText.setVisible(true);
         UserNameText.setEnabled(false);
         this.getContentPane().add(UserNameText);
         TestButton.setBounds(240,10,70,20);
         TestButton.setVisible(true);
         this.getContentPane().add(TestButton);
         OkButton.setBounds(40,70,70,20);
         OkButton.setVisible(true);
         this.getContentPane().add(OkButton);
         ExitButton.setBounds(180,70,70,20);
         ExitButton.setVisible(true);
         this.getContentPane().add(ExitButton);
     }

    public void TestButton_actionPerformed(ActionEvent e) {
        this.UserNameText.setEnabled(true);
        IP=this.IPText.getText().trim();
        System.out.println(IP);
        try
        {
            s = new Socket(IP, 8189);
        }catch(Exception ee)
        {
            ee.printStackTrace();
            System.out.println("在客户端的连接程序端!");
        }
    }

    public void OkButton_actionPerformed(ActionEvent e) {
        try
        {
            OutputStream outStream = s.getOutputStream();
            System.out.println(this.UserNameText.getText().trim());
            byte[] bye=this.UserNameText.getText().trim().getBytes();
            outStream.write(bye);
            outStream.flush();
        }
        catch(Exception ee)
        {
            ee.printStackTrace();
            System.out.println("错误在OkButton!");
        }
    }

    public void ExitButton_actionPerformed(ActionEvent e) {
        try
        {
            s.close();
            System.exit(1);
        }catch(Exception ee)
        {
            ee.printStackTrace();
            System.out.println("错误在ExitButton");
        }
    }
}


class KuFrame_ExitButton_actionAdapter implements ActionListener {
    private KuFrame adaptee;
    KuFrame_ExitButton_actionAdapter(KuFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.ExitButton_actionPerformed(e);
    }
}


class KuFrame_OkButton_actionAdapter implements ActionListener {
    private KuFrame adaptee;
    KuFrame_OkButton_actionAdapter(KuFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.OkButton_actionPerformed(e);
    }
}


class KuFrame_TestButton_actionAdapter implements ActionListener {
    private KuFrame adaptee;
    KuFrame_TestButton_actionAdapter(KuFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.TestButton_actionPerformed(e);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -