Java图形用户界面编程

2024-12-20 01:04:49
推荐回答(2个)
回答1:

我给你写,已经测试,你看合不合你的意思
package dongzi.pattern;

import java.awt.FlowLayout;
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 SumTest extends JPanel{
JTextField mem[] = new JTextField[10] ;
JButton sumButton = new JButton("SUM") ;
JTextField sumText = new JTextField(4) ;

public SumTest() {
setLayout(new FlowLayout()) ;
for (int i = 0; i < 10; i++) {
mem[i] = new JTextField(4) ;
add(mem[i]) ;
}
add(sumButton) ;
add(sumText) ;

sumButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int sum = 0 ;
for (int i = 0 ;i < 10; i++) {
sum += Integer.parseInt(mem[i].getText().trim()) ;
}
sumText.setText(String.valueOf(sum)) ;
}

}) ;
}

public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new JFrame() ;
SumTest test = new SumTest() ;
frame.add(test) ;

frame.setSize(800, 100) ;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
frame.setVisible(true) ;

}

}

回答2:

去开源社区上收!里面很多好代码