import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.util.Arrays;

import javax.swing.JTextField;
import javax.swing.JButton;

public class SwingOk extends JFrame {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JLabel jLabel_input = null;

	private JTextField jTextField_input = null;

	private JButton jButton_sure = null;
	
	private JButton jButton_sur = null;

	private JLabel jLabel_title = null;

	private JButton jButton_cancel = null;
	
	private JTextField str2[] = new JTextField[20];

	private JLabel str[] = new JLabel[20];
	
	private JLabel jLabel_2 = null;

	private JTextField jText = null;
	
	private int n=0;

	public SwingOk() {
		super();
		initialize();
	}

	private void initialize() {
		this.setSize(315, 207);
		this.setContentPane(getJContentPane());
		this.setTitle("评分系统");
		this.setResizable(false);//设置不能更改大小
		((JPanel) this.getContentPane()).setOpaque(false);
		ImageIcon img = new ImageIcon("C:\\Myeclipse2\\编程思想\\BJ0.jpg");
		JLabel background = new JLabel(img);
		background.setSize(new Dimension(1024, 717));
		this.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
		background.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
	}

	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel_title = new JLabel();
			jLabel_title.setBounds(new Rectangle(73, 20, 159, 36));
			jLabel_title.setText("1240131班评委评分系统");
			jLabel_input = new JLabel();
			jLabel_input.setBounds(new Rectangle(40, 81, 112, 28));
			jLabel_input.setText("请输入评委人数:");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(jLabel_input, null);
			jContentPane.add(getJTextField_input(), null);
			jContentPane.add(getJButton_sure(), null);
			jContentPane.add(jLabel_title, null);
			jContentPane.add(getJButton_cancel(), null);

		}
		return jContentPane;
	}

	private JTextField getJTextField_input() {
		if (jTextField_input == null) {
			jTextField_input = new JTextField();
			jTextField_input.setBounds(new Rectangle(182, 81, 75, 27));
			jTextField_input.setToolTipText("请输入4到16的整数");
			/**
			 * 下面创建快捷键enter
			 */
			jTextField_input.addKeyListener(new java.awt.event.KeyListener() {
				public void keyPressed(java.awt.event.KeyEvent e) {
					if (e.getKeyCode() == 10) {
	                    if(jButton_sure.getText().equals("重置")){
	                    	jContentPane.remove(jLabel_2);
	                    	jContentPane.remove(jText);
	                    	jContentPane.remove(jButton_sur);
	                    	jButton_sur=null;
	                    	for(int i=1;i<=n;i++){
	                    		jContentPane.remove(str[i]);
	                    		jContentPane.remove(str2[i]);
	                    	}
	                    	jTextField_input.setEditable(true);
	                    	jButton_sure.setText("确认");
	                    	SwingOk.this.initialize();
	                    }else                   	
	                    {                     	
						String number = jTextField_input.getText();
						int i;
						for (i = 0; i < number.length(); i++) {
							if (number.charAt(i) >= '0' && number.charAt(i) <= '9')
								;
							else {
								JOptionPane.showMessageDialog(null, "输入不合法");
								break;
							}
						}
						if (number.length() == 0) {
							JOptionPane.showMessageDialog(null, "不能为空");
						} else if (i >= number.length()) {
							n = Integer.parseInt(number);
							if (n <= 3 || n >= 16)
								JOptionPane.showMessageDialog(null, "输入不合法");
							else {
								jButton_sure.setText("重置");
		                    	jTextField_input.setEditable(false);
								int y = 220;							
								for (i = 1; i <= n; i++) {
									str[i] = new JLabel();
									str2[i] = new JTextField();
									if (i % 2 == 0) {
										str[i].setBounds(new Rectangle(160, y, 40, 24));
										str2[i].setBounds(new Rectangle(237, y, 43, 27));
										y += 50;
									} else {
										str[i].setBounds(new Rectangle(23, y, 40, 24));
										str2[i].setBounds(new Rectangle(87, y, 43, 27));
									}
									str[i].setText("评委" + i);
									str[i].setForeground(Color.green);
									str2[i].setText(String.valueOf(0));
									str2[i].setForeground(Color.blue);
									str2[i].setBackground(Color.lightGray);
									jContentPane.add(str[i], null);
									jContentPane.add(str2[i], null);
								}
								if (n % 2 != 0)
									y += 50;
								jLabel_2 = new JLabel();
								jLabel_2.setText("最终得分:");
								jLabel_2.setForeground(Color.red);
								jLabel_2.setBounds(new Rectangle(23, y, 159, 36));
								jContentPane.add(jLabel_2, null);
								jText = new JTextField();
								jText.setBounds(new Rectangle(100, y, 43, 27));
								jText.setEditable(false);
								jText.setForeground(Color.red);
								jContentPane.add(jText, null);
								jContentPane.add(getJButton_sur(y), null);
								SwingOk.this.setSize(315, 300 + ((n + 1) / 2) * 50);
								SwingOk.this.setVisible(true);
							}
						}
					}
					}
				}

				public void keyTyped(java.awt.event.KeyEvent e) {
				}

				public void keyReleased(java.awt.event.KeyEvent e) {
				}
			});

		}
		return jTextField_input;
	}

	private JButton getJButton_sure() {
		if (jButton_sure == null) {
			jButton_sure = new JButton();
			jButton_sure.setBounds(new Rectangle(52, 131, 75, 27));
			jButton_sure.setText("确定");
			jButton_sure.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
                    if(jButton_sure.getText().equals("重置")){
                    	jContentPane.remove(jLabel_2);
                    	jContentPane.remove(jText);
                    	jContentPane.remove(jButton_sur);
                    	jButton_sur=null;
                    	for(int i=1;i<=n;i++){
                    		jContentPane.remove(str[i]);
                    		jContentPane.remove(str2[i]);
                    	}
                    	jTextField_input.setEditable(true);
                    	jButton_sure.setText("确认");
                    	SwingOk.this.initialize();
                    }else                   	
                    {                     	
					String number = jTextField_input.getText();
					int i;
					for (i = 0; i < number.length(); i++) {
						if (number.charAt(i) >= '0' && number.charAt(i) <= '9')
							;
						else {
							JOptionPane.showMessageDialog(null, "输入不合法");
							break;
						}
					}
					if (number.length() == 0) {
						JOptionPane.showMessageDialog(null, "不能为空");
					} else if (i >= number.length()) {
						n = Integer.parseInt(number);
						if (n <= 3 || n >= 16)
							JOptionPane.showMessageDialog(null, "输入不合法");
						else {
							jButton_sure.setText("重置");
	                    	jTextField_input.setEditable(false);
							int y = 220;							
							for (i = 1; i <= n; i++) {
								str[i] = new JLabel();
								str2[i] = new JTextField();
								if (i % 2 == 0) {
									str[i].setBounds(new Rectangle(160, y, 40, 24));
									str2[i].setBounds(new Rectangle(237, y, 43, 27));
									y += 50;
								} else {
									str[i].setBounds(new Rectangle(23, y, 40, 24));
									str2[i].setBounds(new Rectangle(87, y, 43, 27));
								}
								str[i].setText("评委" + i);
								str[i].setForeground(Color.green);
								str2[i].setText(String.valueOf(0));
								str2[i].setForeground(Color.blue);
								str2[i].setBackground(Color.lightGray);
								jContentPane.add(str[i], null);
								jContentPane.add(str2[i], null);
							}
							if (n % 2 != 0)
								y += 50;
							jLabel_2 = new JLabel();
							jLabel_2.setText("最终得分:");
							jLabel_2.setForeground(Color.red);
							jLabel_2.setBounds(new Rectangle(23, y, 159, 36));
							jContentPane.add(jLabel_2, null);
							jText = new JTextField();
							jText.setBounds(new Rectangle(100, y, 43, 27));
							jText.setEditable(false);
							jText.setForeground(Color.red);
							jContentPane.add(jText, null);
							jContentPane.add(getJButton_sur(y), null);
							SwingOk.this.setSize(315, 300 + ((n + 1) / 2) * 50);
							SwingOk.this.setVisible(true);
						}
					}
				}
				}
			}

			);
		}
		return jButton_sure;
	}

	private JButton getJButton_cancel() {
		if (jButton_cancel == null) {
			jButton_cancel = new JButton();
			jButton_cancel.setBounds(new Rectangle(179, 131, 75, 27));
			jButton_cancel.setText("取消");
			jButton_cancel
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							System.exit(0);
						}
					});
		}
		return jButton_cancel;
	}

	private JButton getJButton_sur(int y) {
		if (jButton_sur == null) {
			jButton_sur = new JButton();
			jButton_sur.setBounds(new Rectangle(175, y, 75, 27));
			jButton_sur.setText("确定");
			jButton_sur.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					int i=0;
					try {
						double a[] = new double[n];			
						for (i = 0; i < n; i++) {
							a[i] = Double.parseDouble((str2[i + 1].getText()));
						}
						Arrays.sort(a);
						double sum = 0;
						for (i = 1; i < n - 1; i++)
						{
							sum += a[i];
							System.out.println(a[i]);
						}
						sum = sum / (n - 2);
						System.out.println(sum);
						String s=Double.toString(sum);
						int end=s.indexOf('.')+3>s.length()?s.length():s.indexOf('.')+3;					
						jText.setText(s.substring(0, end));
					} catch (NumberFormatException e1) {				
					JOptionPane.showMessageDialog(null, "第"+(i+1)+"位评委输入有误!","警告",2);					
						return ;
					}				
				}
			});
		}
		return jButton_sur;
	}
	
	public static void main(String[] args) {
		SwingOk frame = new SwingOk();
		Center.centerFrame(frame);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
} 

class Center {
	public static void centerFrame(JFrame frame) {
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = frame.getSize();
		if (frameSize.height > screenSize.height) {
			frameSize.height = screenSize.height;
		}
		if (frameSize.width > screenSize.width) {
			frameSize.width = screenSize.width;
		}
		frame.setLocation((screenSize.width - frameSize.width) / 2,
				(screenSize.height - frameSize.height) / 2);
	}
}
最近下载更多
DEFNXKJ  LV2 4月18日
fesfefe  LV13 2023年10月6日
Demo1111  LV30 2021年12月12日
小林哥哥爱吃肉  LV1 2021年5月31日
陈皮阿五  LV1 2019年12月8日
1403225930  LV1 2019年1月5日
Shiroha  LV2 2018年12月26日
songyy  LV2 2018年12月17日
ssy961021  LV7 2018年12月7日
qubaba  LV1 2018年8月7日
最近浏览更多
hfffff  LV1 6月3日
huAres  LV2 4月18日
DEFNXKJ  LV2 4月18日
程哥哥 4月11日
暂无贡献等级
tangyongheng08  LV6 1月4日
lshlsh 2023年12月25日
暂无贡献等级
shiaomon 2023年12月14日
暂无贡献等级
四十四十  LV3 2023年10月18日
fesfefe  LV13 2023年9月30日
微信网友_6533174388953088  LV1 2023年6月24日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友