選擇整數計算
問題描述
- 使用複選框顯示給定的一組整數,如圖所示,選中一些;
- 單擊“全選”按鈕,選中所有的複選框;
- 單擊“多項選擇”按鈕,將多個選中整數顯示在文本行數組中;
- 在文本行中輸入整數,單擊“求和”按鈕,將各文本行中的非空字符串轉換成整數,再計算和並顯示結果。
- 若字符串不能轉換成整數,採用對話框提示忽略它,並將該文本行清空。
增加要求
- 框架構造方法的參數指定整數個數;
- 使用複選框數組和文本框數組;
- 聲明對文本行數組的計算方法如下
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
}
}
}