zoukankan      html  css  js  c++  java
  • JAVA实验六——图形用户界面设计——6-47选择整数计算

    选择整数计算

    问题描述

    • 使用复选框显示给定的一组整数,如图所示,选中一些;
    • 单击“全选”按钮,选中所有的复选框;
    • 单击“多项选择”按钮,将多个选中整数显示在文本行数组中;
    • 在文本行中输入整数,单击“求和”按钮,将各文本行中的非空字符串转换成整数,再计算和并显示结果。
    • 若字符串不能转换成整数,采用对话框提示忽略它,并将该文本行清空。

    增加要求

    1. 框架构造方法的参数指定整数个数;
    2. 使用复选框数组和文本框数组;
    3. 声明对文本行数组的计算方法如下
    public int sum(JTextField[] texts) //计算文本行数组之和,忽略其中""和不能转换成整数的字符串
    

    代码如下

    package book;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class AWT extends Frame implements ActionListener{
    	JTextField [] textbox = new JTextField[6];
    	JCheckBox [] checkboxs = new JCheckBox[6];
    	TextField sumField = new TextField();
    	Button button1,button2,button3;
    	
    	public AWT() {
    		super("选择整数计算");
    		this.setBounds(0, 0, 400, 360);
    		this.setLayout(null);
    		String [] str = {"57","15","78","23","51","59"};
    		//JPanel panel = new JPanel();
    		for(int i=0;i<str.length;i++) {
    			checkboxs[i] = new JCheckBox(str[i]);
    			textbox[i] = new JTextField();
    			this.add(checkboxs[i]);
    			this.add(textbox[i]);
    			checkboxs[i].setBounds(20 , 30+40*i , 60, 40);
    			//checkboxs[i].setBounds(x, y, width, height);
    			textbox[i].setBounds(100 , 40+40*i, 100, 20);
    			//textbox[i].setText(str[i]);
    			//checkboxs[i].setFont(new Font("Arial",1,20));
    			//checkboxs[i].setSize(width, height);
    			//checkboxs[i].setSize(30, 30);
    		}
    		//this.add(panel);
    		this.button1 = new Button("全选");
    		button1.setBounds(20, 300, 70, 30);
    		this.add(button1);
    		button1.addActionListener(this);
    		this.button2 = new Button("多项选择");
    		button2.setBounds(100, 300, 70, 30);
    		this.add(button2);
    		button2.addActionListener(this);
    		this.button3 = new Button("求和");
    		button3.setBounds(180, 300, 70, 30);
    		this.add(button3);
    		button3.addActionListener(this);
    		this.sumField.setBounds(260,300 , 70, 30);
    		this.sumField.setFont(new Font("TimesRoman",0,18));
    		this.add(sumField);
    		this.setVisible(true);
    		this.addWindowListener(new WinClose());
    	}
    	public void Function() {
    		for(int i=0;i<checkboxs.length;i++) {
    			if(!checkboxs[i].isSelected())
    				checkboxs[i].doClick();
    		}
    		for(int i=0;i<checkboxs.length;i++)
    			if(checkboxs[i].isSelected()) {
    				textbox[i].setText(checkboxs[i].getText());
    			}
    	}
    	public void Function2() {
    		for(int i=0;i<checkboxs.length;i++)
    			if(checkboxs[i].isSelected()) {
    				textbox[i].setText(checkboxs[i].getText());
    			}
    	}
    	public int sum(JTextField[] texts) {
    		int s=0;
    		for(int i=0;i<texts.length;i++) {
    			try {
    				if(checkboxs[i].isSelected()&&!(texts[i].getText()==""))
    				s+=Integer.parseInt(texts[i].getText());
    			}
    			catch (Exception e) {
    				// TODO: handle exception
    				JOptionPane.showMessageDialog(null,"""+texts[i].getText()+"" 不能转换成整数,忽略","消息",JOptionPane.INFORMATION_MESSAGE);
    				textbox[i].setText("");
    			}
    		}
    		//sumField.setText(Integer.toString(s));
    		return s;
    	}
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		// TODO Auto-generated method stub
    		if(e.getSource() == this.button1) {
    			Function();
    		}
    		if(e.getSource() == this.button2) {
    			Function2();
    		}
    		if(e.getSource() == this.button3) {
    			sumField.setText(Integer.toString(sum(textbox)));;
    		}
    		for(int i=0;i<textbox.length;i++) {
    			if(checkboxs[i].isSelected())
    				textbox[i].setText(checkboxs[i].getText());
    			else
    				textbox[i].setText("");
    		}
    	}
    	
    	public static void main(String[] args) {	new AWT();	}
    	public class WinClose implements WindowListener{
    
    		@Override
    		public void windowOpened(WindowEvent e) {
    			// TODO Auto-generated method stub
    			
    		}
    
    		@Override
    		public void windowClosing(WindowEvent e) {
    			// TODO Auto-generated method stub
    			System.exit(0);
    		}
    
    		@Override
    		public void windowClosed(WindowEvent e) {
    			// TODO Auto-generated method stub
    			
    		}
    
    		@Override
    		public void windowIconified(WindowEvent e) {
    			// TODO Auto-generated method stub
    			
    		}
    
    		@Override
    		public void windowDeiconified(WindowEvent e) {
    			// TODO Auto-generated method stub
    			
    		}
    
    		@Override
    		public void windowActivated(WindowEvent e) {
    			// TODO Auto-generated method stub
    			
    		}
    
    		@Override
    		public void windowDeactivated(WindowEvent e) {
    			// TODO Auto-generated method stub
    			
    		}		
    	}	
    }
    
  • 相关阅读:
    ElasticSearch(5.5.2)在java中的使用
    ElasticSearch基础
    Linux中profile、bashrc、bash_profile之间的区别和联系
    linux上安装启动elasticsearch-5.5.1完整步骤
    linux 解压zip文件
    Ubuntu Linux 环境变量PATH设置
    sudo 用户添加
    SecureCRT5 中文乱码
    韩美林的养生秘诀——“懒人操”
    实验二:线性表的实验【物联网1132-11】
  • 原文地址:https://www.cnblogs.com/Andre/p/14061642.html
Copyright © 2011-2022 走看看