//2007年04月30日晚用记事本抄写自某java教材。
//考试分数分布统计程序 import java.awt.event.*; import java.awt.*; import javax.swing.*; @SuppressWarnings("serial") public class Score extends JFrame { private JPanel inputPanel = new JPanel(); private PiePanel piePanel = new PiePanel(); private JLabel labelRed = new JLabel("不及格(60以下)"); private JLabel labelYellow = new JLabel("优秀(100-90)"); private JLabel labelGreen = new JLabel("良好(89-75)"); private JLabel labelBlue = new JLabel("中等(74-60)"); private JLabel labelName = new JLabel("学号:"); private JLabel labelScore = new JLabel("分数:"); private JTextField inputName = new JTextField(10); private JTextField inputScore = new JTextField(10); private JTextArea textArea = new JTextArea(20, 16); private JScrollPane scrollPane = new JScrollPane(textArea); private JButton inputButton = new JButton("输入"); private int ia = 0, ib = 0, ic = 0, id = 0; public Score() { initComponents();// 初始化GUI组件 } private void initComponents() { setSize(400, 300);// 设置框架大小 setTitle("分数统计"); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); inputPanel.add(labelName); inputPanel.add(inputName); inputPanel.add(labelScore); inputPanel.add(inputScore); inputPanel.add(inputButton); inputButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { inputData(inputName.getText(), Double.parseDouble(inputScore .getText())); } }); labelYellow.setForeground(Color.YELLOW); labelGreen.setForeground(Color.GREEN); labelBlue.setForeground(Color.BLUE); labelRed.setForeground(Color.RED); piePanel.add(labelYellow); piePanel.add(labelGreen); piePanel.add(labelBlue); piePanel.add(labelRed); piePanel.setBackground(Color.GRAY); getContentPane().add(inputPanel, "North"); getContentPane().add(scrollPane, "West"); getContentPane().add(piePanel, "Center"); textArea.append("姓名/t分数/n"); } private void inputData(String name, double score) { textArea.append(name + ":/t " + score + "/n"); if (score >= 90) { ia++; } else if ((score >= 75)) { ib++; } else if ((score >= 60)) { ic++; } else { id++; } int sum = (ia + ib + ic + id);// 输入的人数汇总 piePanel.a = 360 * ia / sum; piePanel.b = 360 * ib / sum; piePanel.c = 360 * ic / sum; piePanel.d = 360 * id / sum; piePanel.repaint(); } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Score().setVisible(true); } }); } } @SuppressWarnings("serial") class PiePanel extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.YELLOW); g.fillArc(x, y, w, w, 0, a);// 绘制饼图的“优秀”弧段 g.setColor(Color.GREEN); g.fillArc(x, y, w, w, a, b);// 绘制饼图的“良好”弧段 g.setColor(Color.BLUE); g.fillArc(x, y, w, w, a + b, c);// 绘制饼图的“中等”弧段 g.setColor(Color.red); g.fillArc(x, y, w, w, a + b + c, d);// 绘制饼图的“不及格”弧段 } int a = 90, b = 90, c = 90, d = 90;// 初始化均匀分布 int x = 20, y = 50, w = 160; }
posted on 2010-02-18 21:13 台哥编程课堂 阅读(233) 评论(0) 编辑 收藏 举报