📄 serverinit.java
字号:
import javax.swing.*;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
public class ServerInit extends JFrame implements ActionListener
{
/*定义一些JPanel、JLabel、JTextField、JButton名*/
JPanel jpa;
JPanel jpa1;
JPanel jpa2;
JPanel jpa3;
JPanel jpa4;
JPanel jpa5;
JPanel jpa6;
JPanel jpa7;
JPanel jpa8;
JPanel jpa9;
JLabel jla1;
JLabel jla2;
JLabel jla3;
JLabel jla4;
JLabel jla5;
JLabel jla6;
JLabel jla7;
JLabel jla8;
JLabel jla9;
JLabel jla10;
JLabel jla11;
JLabel jla12;
JLabel jla13;
JTextField title;
JTextField rightAddScore;
JTextField wrongSubScore;
JTextField faultSubScore;
JTextField initScore;
JTextField questionNumber;
JTextField answerTime;
JButton button;
ServerInfo serverInfo = new ServerInfo();
public ServerInit()
{
/*初始化窗口布局*/
this.setTitle("初始化菜单");//窗口标题
//this.setBounds(280,280,280,280);//固定窗口大小
jpa = new JPanel();
this.add(jpa);
jpa.setLayout(new BorderLayout());
jpa1 = new JPanel();
jpa2 = new JPanel();
jpa3 = new JPanel();
jpa1.setBackground(new Color(85,141,142));
jpa3.setBackground(new Color(85,141,142));
jpa.add(jpa1,BorderLayout.NORTH);
jpa.add(jpa2,BorderLayout.CENTER);
jpa.add(jpa3,BorderLayout.SOUTH);
jla1 = new JLabel("竞赛标题");
jla1.setFont(new Font("宋体",Font.BOLD,14));
title = new JTextField("知识竞赛",10);
title.setHorizontalAlignment(JTextField.CENTER);//设置文本水平对齐方式
jpa1.add(jla1);
jpa1.add(title);
jpa2.setLayout(new GridLayout(3,2));
jpa4 = new JPanel();
jpa5 = new JPanel();
jpa6 = new JPanel();
jpa7 = new JPanel();
jpa8 = new JPanel();
jpa9 = new JPanel();
jpa2.add(jpa4);
jpa2.add(jpa5);
jpa2.add(jpa6);
jpa2.add(jpa7);
jpa2.add(jpa8);
jpa2.add(jpa9);
jla2 = new JLabel("答对每题加");
jla2.setFont(new Font("宋体",Font.BOLD,14));
jla2.setForeground(Color.RED);
rightAddScore =new JTextField("10",5);
rightAddScore.setHorizontalAlignment(JTextField.CENTER);//设置文本水平对齐方式
jla3 = new JLabel("分");
jla3.setFont(new Font("宋体",Font.BOLD,14));
jla3.setForeground(Color.RED);
jla4 = new JLabel("违规抢答扣");
jla4.setFont(new Font("宋体",Font.BOLD,14));
jla4.setForeground(Color.RED);
faultSubScore =new JTextField("5",5);
faultSubScore.setHorizontalAlignment(JTextField.CENTER);//设置文本水平对齐方式
jla5 = new JLabel("分");
jla5.setFont(new Font("宋体",Font.BOLD,14));
jla5.setForeground(Color.RED);
jla6 = new JLabel("答错每题扣");
jla6.setFont(new Font("宋体",Font.BOLD,14));
jla6.setForeground(Color.RED);
wrongSubScore =new JTextField("10",5);
wrongSubScore.setHorizontalAlignment(JTextField.CENTER);//设置文本水平对齐方式
jla7 = new JLabel("分");
jla7.setFont(new Font("宋体",Font.BOLD,14));
jla7.setForeground(Color.RED);
jla8 = new JLabel("原始总分数");
jla8.setFont(new Font("宋体",Font.BOLD,14));
jla8.setForeground(Color.RED);
initScore = new JTextField("200",5);
initScore.setHorizontalAlignment(JTextField.CENTER);//设置文本水平对齐方式
jla9 = new JLabel("分");
jla9.setFont(new Font("宋体",Font.BOLD,14));
jla9.setForeground(Color.RED);
jla10 = new JLabel("答题总数目");
jla10.setFont(new Font("宋体",Font.BOLD,14));
jla10.setForeground(Color.RED);
questionNumber = new JTextField("100",5);
questionNumber.setHorizontalAlignment(JTextField.CENTER);//设置文本水平对齐方式
jla11 = new JLabel("道");
jla11.setFont(new Font("宋体",Font.BOLD,14));
jla11.setForeground(Color.RED);
jla12 = new JLabel("答题时限为");
jla12.setFont(new Font("宋体",Font.BOLD,14));
jla12.setForeground(Color.RED);
answerTime = new JTextField("60",5);
answerTime.setHorizontalAlignment(JTextField.CENTER);//设置文本水平对齐方式
jla13 = new JLabel("秒");
jla13.setFont(new Font("宋体",Font.BOLD,14));
jla13.setForeground(Color.RED);
jpa4.add(jla2);
jpa4.add(rightAddScore);
jpa4.add(jla3);
jpa5.add(jla4);
jpa5.add(faultSubScore);
jpa5.add(jla5);
jpa6.add(jla6);
jpa6.add(wrongSubScore);
jpa6.add(jla7);
jpa7.add(jla8);
jpa7.add(initScore);
jpa7.add(jla9);
jpa8.add(jla10);
jpa8.add(questionNumber);
jpa8.add(jla11);
jpa9.add(jla12);
jpa9.add(answerTime);
jpa9.add(jla13);
button = new JButton("确定");
button.setBackground(new Color(123,175,176));
button.setFont(new Font("宋体",Font.BOLD,30));
button.setForeground(Color.RED);
jpa3.add(button);
button.addActionListener(this);
this.pack();//使布局紧凑
this.setVisible(true);
//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
/*将打包到的ServerInfo类的数据传出*/
serverInfo.setTitle(title.getText());
try{
serverInfo.setRightAddScore(rightAddScore.getText());
serverInfo.setWrongSubScore(wrongSubScore.getText());
serverInfo.setFaultSubScore(faultSubScore.getText());
serverInfo.setInitScore(initScore.getText());
serverInfo.setQuestionNumber(questionNumber.getText());
serverInfo.setAnswerTime(answerTime.getText());
this.dispose();
}
catch(Exception ex)
{
ServerError servererror = new ServerError();
servererror.setVisible(true);
//System.out.println("输入有误!");
}
//new一个ServerMain(服务器端)对像,将初始化数据做为参数传出去。
/* ServerMain servermain = new ServerMain(ServerInfo);
servermain.setVisible(true);
this.setVisible(false);*/
/*System.out.println(serverInfo.title);
System.out.println(serverInfo.rightAddScore);
System.out.println(serverInfo.wrongSubScore);
System.out.println(serverInfo.faultSubScore);
System.out.println(serverInfo.initScore);
System.out.println(serverInfo.questionNumber);
System.out.println(serverInfo.answerTime);
*/
//String [] args =null;
//serverMain.main(args);
new Thread(new Runnable(){
public void run() {
// TODO Auto-generated method stub
ServerMain serverMain = new ServerMain(ServerInit.this.serverInfo);
try
{
ServerSocket ss = new ServerSocket(9999);
while(true)
{
Socket currentSocket = ss.accept();
serverMain.setSocket(currentSocket);
new Thread(serverMain).start();
}
}
catch(Exception e)
{
System.out.println("服务器套接字创建失败");
}
}
}).start();
}
public static void main(String[] args)
{
new ServerInit();
}
}
class ServerError extends JFrame implements ActionListener
{
ServerInit serverinit = new ServerInit();
//输入错误时弹出对话框
public ServerError()
{
try
{
//JFrame jframe = new JFrame();
this.setBounds(280, 280, 280, 280);
JPanel jpanel = new JPanel();
jpanel.setLayout(new BorderLayout());
jpanel.setLayout(new GridLayout(2,2));
this.add(jpanel);
jpanel.setBackground(Color.getHSBColor(78, 215, 222));
JButton jbutton = new JButton("关闭");
JLabel jlabel = new JLabel("输入错误,请重新输入!");
jbutton.addActionListener(this);
jpanel.setLayout(null);
//jpanel.add(jlable,BorderLayout.CENTER);
jpanel.add(jlabel,BorderLayout.CENTER);
jpanel.add(jbutton,BorderLayout.SOUTH);
jlabel.setBounds(59, 39, 195, 35);
jbutton.setBounds(69, 69, 135, 135);
jlabel.setFont(new Font("宋体",Font.BOLD,14));
jlabel.setForeground(Color.RED);
jbutton.setFont(new Font("宋体",Font.BOLD,40));
jbutton.setForeground(Color.RED);
//this.pack();
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
catch(Exception ex1)
{
System.exit(0);
}
}
public void actionPerformed(ActionEvent e1)
{
this.setVisible(false);
serverinit.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -