标签:java
import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; public class Test { public static void main (String[] args){ JFrame test= new JFrame(); test.setLayout(new GridLayout(2,5)); final JTextField[] text = new JTextField[10]; for (int i = 0; i < text.length; i++) { text[i] = new JTextField(i+1+""); test.add(text[i]); } JPanel pan1 = new JPanel(new GridLayout(1,2)); final JButton but1 = new JButton("平均分"); final JTextField t11 = new JTextField("5.50"); pan1.add(but1); pan1.add(t11); but1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if (e.getSource() == but1) { int n = 0; for (int i = 1; i <= 10; i++) n = n + Integer.parseInt(text[i].getText()); //在这附近存在bug,有待调试 int result = (int)n/10; t11.setText(Integer.toString(result)); } } }); test.add(pan1); test.setVisible(true); test.pack(); } }
标签:java
原文地址:http://anglecode.blog.51cto.com/5628271/1619879