package four;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

import lombok.Data;

/**
 * 投票的主页面
 * 
 * @author czx
 *
 */
@Data
public class VoteClient {

	private JFrame frame;
	static String host;
	static int port;
	Socket socket;
	PrintWriter pw;
	BufferedReader in;
	public static JCheckBox checkBox1;
	private JCheckBox checkBox2;
	private JCheckBox checkBox3;
	private JCheckBox checkBox4;
	private JCheckBox checkBox5;
	public static JLabel label1;
	private JLabel label2;
	private JLabel label3;
	private JLabel label4;
	private JLabel label5;
	private JProgressBar progressBar1;
	private JProgressBar progressBar2;
	private JProgressBar progressBar3;
	private JProgressBar progressBar4;
	private JProgressBar progressBar5;
	private JPanel panel_6;
	private JButton btnNewButton_1;
	private JButton btnNewButton_2;
	public static String str11;
	String str12;
	String str13;
	String str14;
	String str15;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					VoteClient window = new VoteClient(host, port);
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	@SuppressWarnings("static-access")
	public VoteClient(String host, int port) {
		this.host = host;
		this.port = port;
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		try {
			// 创建一个套接字,host port是从login中获取的
			socket = new Socket(host, port);
			// 创建一个往套接字中写数据的管道,即输出流,给服务器发送信息
			in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
			pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"), true);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		frame = new JFrame();
		frame.setBounds(100, 100, 473, 298);
		frame.setTitle("");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JPanel panel = new JPanel();
		frame.getContentPane().add(panel, BorderLayout.CENTER);
		frame.setVisible(true);
		panel.setLayout(new GridLayout(5, 1, 15, 15));

		// 一号选手
		JPanel panel_1 = new JPanel();
		panel.add(panel_1);
		panel_1.setLayout(new BorderLayout(0, 0));

		checkBox1 = new JCheckBox("一号选手");
		checkBox1.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_1.add(checkBox1, BorderLayout.WEST);

		label1 = new JLabel("0票");
		progressBar1 = new JProgressBar();
		progressBar1.setStringPainted(true);
		panel_1.add(label1, BorderLayout.EAST);

		progressBar1.setStringPainted(true);
		panel_1.add(progressBar1, BorderLayout.CENTER);
		// 二号选手
		JPanel panel_2 = new JPanel();
		panel.add(panel_2);
		panel_2.setLayout(new BorderLayout(0, 0));

		checkBox2 = new JCheckBox("二号选手");
		checkBox2.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_2.add(checkBox2, BorderLayout.WEST);

		progressBar2 = new JProgressBar();
		progressBar2.setStringPainted(true);
		panel_2.add(progressBar2, BorderLayout.CENTER);

		label2 = new JLabel("0\u7968");
		panel_2.add(label2, BorderLayout.EAST);

		// 三号选手
		JPanel panel_3 = new JPanel();
		panel.add(panel_3);
		panel_3.setLayout(new BorderLayout(0, 0));

		checkBox3 = new JCheckBox("三号选手");
		checkBox3.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_3.add(checkBox3, BorderLayout.WEST);

		progressBar3 = new JProgressBar();
		progressBar3.setStringPainted(true);
		panel_3.add(progressBar3, BorderLayout.CENTER);

		label3 = new JLabel("0票");
		panel_3.add(label3, BorderLayout.EAST);

		// 四号选手
		JPanel panel_4 = new JPanel();
		panel.add(panel_4);
		panel_4.setLayout(new BorderLayout(0, 0));

		checkBox4 = new JCheckBox("四号选手");
		checkBox4.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_4.add(checkBox4, BorderLayout.WEST);

		progressBar4 = new JProgressBar();
		progressBar4.setStringPainted(true);
		panel_4.add(progressBar4, BorderLayout.CENTER);

		label4 = new JLabel("0票");
		panel_4.add(label4, BorderLayout.EAST);

		// 五号选手
		JPanel panel_5 = new JPanel();
		panel.add(panel_5);
		panel_5.setLayout(new BorderLayout(0, 0));

		checkBox5 = new JCheckBox("五号选手");
		checkBox5.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_5.add(checkBox5, BorderLayout.WEST);

		progressBar5 = new JProgressBar();
		progressBar5.setStringPainted(true);
		panel_5.add(progressBar5, BorderLayout.CENTER);

		label5 = new JLabel("0票");
		panel_5.add(label5, BorderLayout.EAST);

		panel_6 = new JPanel();
		frame.getContentPane().add(panel_6, BorderLayout.SOUTH);

		btnNewButton_1 = new JButton("\u63D0\u4EA4");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnNewButton_1.setEnabled(false);
				pw.println("submit");
				String text1 = label1.getText();
				int number1 = Integer.parseInt(text1.substring(0, text1.length() - 1));
				String text2 = label2.getText();
				int number2 = Integer.parseInt(text2.substring(0, text2.length() - 1));
				String text3 = label3.getText();
				int number3 = Integer.parseInt(text3.substring(0, text3.length() - 1));
				String text4 = label4.getText();
				int number4 = Integer.parseInt(text4.substring(0, text4.length() - 1));
				String text5 = label5.getText();
				int number5 = Integer.parseInt(text5.substring(0, text5.length() - 1));
				if (checkBox1.isSelected()) {
					number1++;
					label1.setText(number1 + "票");
				}
				if (checkBox2.isSelected()) {
					number2++;
					label2.setText(number2 + "票");
				}
				if (checkBox3.isSelected()) {
					number3++;
					label3.setText(number3 + "票");
				}
				if (checkBox4.isSelected()) {
					number4++;
					label4.setText(number4 + "票");
				}
				if (checkBox5.isSelected()) {
					number5++;
					label5.setText(number5 + "票");
				}
				double total = number1 + number2 + number3 + number4 + number5;
				progressBar1.setString(number1 * 100 / total + "%");
				progressBar1.setValue(number1);
				progressBar2.setString(number2 * 100 / total + "%");
				progressBar2.setValue(number2);
				progressBar3.setString(number3 * 100 / total + "%");
				progressBar3.setValue(number3);
				progressBar4.setString(number4 * 100 / total + "%");
				progressBar4.setValue(number4);
				progressBar5.setString(number5 * 100 / total + "%");
				progressBar5.setValue(number5);
				System.out.println(number1 + "" + number2 + "" + number3 + "" + number4 + "" + number5);
				pw.println(number1);
				pw.println(number2);
				pw.println(number3);
				pw.println(number4);
				pw.println(number5);
			}
		});
		btnNewButton_1.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_6.add(btnNewButton_1);

		btnNewButton_2 = new JButton("\u83B7\u53D6\u5019\u9009\u4EBA\u4FE1\u606F");
		btnNewButton_2.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnNewButton_2.setEnabled(false);
				if (pw != null) {
					pw.println("Info");
					if (in != null) {
						try {
							//接收候选人信息
							String tr11 = in.readLine();
							String tr21 = in.readLine();
							String tr31 = in.readLine();
							String tr41 = in.readLine();
							String tr51 = in.readLine();

							//接收票数信息
							String tr1 = in.readLine();
							String tr2 = in.readLine();
							String tr3 = in.readLine();
							String tr4 = in.readLine();
							String tr5 = in.readLine();

							checkBox1.setText(tr11);
							checkBox2.setText(tr21);
							checkBox3.setText(tr31);
							if (tr41.equals("null")) {
								checkBox4.setText("");
							}
							checkBox5.setText(tr51);

							if (tr1.equals("")) {
								label1.setText(0 + "票");
							} else if (!tr1.equals("")) {
								label1.setText(tr1 + "票");
							}
							if (tr2.equals("")) {
								label2.setText(0 + "票");
							} else if (!tr2.equals("")) {
								label2.setText(tr2 + "票");
							}
							if (tr3.equals("")) {
								label3.setText(0 + "票");
							} else if (!tr3.equals("")) {
								label3.setText(tr3 + "票");
							}
							if (tr4.equals("")) {
								label4.setText(0 + "票");
							} else if (!tr4.equals("")) {
								label4.setText(tr4 + "票");
							}
							if (tr5.equals("")) {
								label5.setText(0 + "票");
							} else if (!tr5.equals("")) {
								label5.setText(tr5 + "票");
							}
							int number1 = Integer
									.parseInt(label1.getText().substring(0, label1.getText().length() - 1));
							int number2 = Integer
									.parseInt(label2.getText().substring(0, label2.getText().length() - 1));
							int number3 = Integer
									.parseInt(label3.getText().substring(0, label3.getText().length() - 1));
							int number4 = Integer
									.parseInt(label4.getText().substring(0, label4.getText().length() - 1));
							int number5 = Integer
									.parseInt(label5.getText().substring(0, label5.getText().length() - 1));
							double total = number1 + number2 + number3 + number4 + number5;
							progressBar1.setString(number1 * 100 / total + "%");
							progressBar1.setValue(number1);
							progressBar2.setString(number2 * 100 / total + "%");
							progressBar2.setValue(number2);
							progressBar3.setString(number3 * 100 / total + "%");
							progressBar3.setValue(number3);
							progressBar4.setString(number4 * 100 / total + "%");
							progressBar4.setValue(number4);
							progressBar5.setString(number5 * 100 / total + "%");
							progressBar5.setValue(number5);
						} catch (IOException e1) {
							e1.printStackTrace();
						}
					}

				}
			}
		});
		panel_6.add(btnNewButton_2);
	}

}
最近下载更多
求学的熊猫  LV11 2023年12月26日
微信网友_5973767083364352  LV1 2022年5月25日
微信网友_5927360927125504  LV1 2022年4月22日
543666826  LV33 2022年3月12日
William2342  LV1 2022年2月8日
lllayc  LV1 2022年1月3日
Thynpa  LV1 2021年12月23日
1095402829  LV18 2021年12月15日
Derrick  LV1 2021年12月15日
Undead  LV2 2021年12月14日
最近浏览更多
y_x_happy  LV4 11月1日
hfffff  LV1 6月3日
akittyboy  LV9 3月22日
wananall  LV13 3月13日
laishaofe  LV7 1月16日
求学的熊猫  LV11 2023年12月25日
lshlsh 2023年12月25日
暂无贡献等级
shiaomon 2023年12月14日
暂无贡献等级
嗯嗯嗯嗯嗯嗯2528 2023年11月29日
暂无贡献等级
ftc1314  LV12 2023年11月2日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友