⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tableexp.java

📁 Java的swing的课堂练习。。 。
💻 JAVA
字号:
/**
 * @(#)TableExp.java
 *
 *
 * @author 
 * @version 1.00 2007/11/11
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Win extends JFrame implements ActionListener {
 JTable  table;
 Object a[][];
 Object name[]={"姓名","英语","数学","总成绩"};
 JButton  confirm,total;
 JTextField  rowNum;
 JPanel p;
 int rows=1;
 Win(String s) {
  super(s);
  confirm=new JButton("确定");
  confirm.addActionListener(this);
  total=new JButton("总成绩");
  total.addActionListener(this);
  
  rowNum=new JTextField(10);
  a=new Object[rows][4];
  table=new JTable(a,name);
  
  p=new JPanel();
  p.add(new JLabel("输入表格行数"));
  p.add(rowNum);
  p.add(confirm);
  p.add(total);
  
  add(p,BorderLayout.SOUTH);
  add(new JScrollPane(table),BorderLayout.CENTER);
  
  setSize(500,200);
  setVisible(true);
  validate();
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
 public void actionPerformed(ActionEvent e) {
  if (e.getSource()==confirm)
  { 
  	try {
  	rows=Integer.parseInt(rowNum.getText());
  	}
  	catch (NumberFormatException ee) {
  	 System.out.println("the input is not number!");
  	}
  	a=new Object[rows][4];
  	table=new JTable(a,name);
  	getContentPane().removeAll();
  	add(new JScrollPane(table),BorderLayout.CENTER);
  	add(p,BorderLayout.SOUTH);
  	validate();  		
  }
  else if (e.getSource()==total) {
  	for (int i=0; i<rows; i++) {
  	 double sum=0;
  	 for (int j=1; j<=2; j++)
  	 {
  	   try {
  	   	sum+=Double.parseDouble(a[i][j].toString());
  	   }
  	   catch (Exception ee) {
  	   	System.out.println("row"+i+"column"+j+"is not a number");
  	   }
  	 }
  	 a[i][3]=sum;
  	 table.repaint();
  	}
  }
 } 
  	   
  	
}
public class TableExp {
        
   
    public static void main(String[] args) {
        // TODO code application logic here
        new Win("表格示例");
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -