/**8
* 利用文本框,標籤,按鈕等完成一個界面
* 輸入一個平時成績,一個實驗成績,一個期末成績
* 按照一定的比例自動出現一個綜合成績
**/
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.*; public class Test{ public static void main(String[] args){ JFrame exit = new JFrame("大一下學期Java課成績計算"); exit.setLayout(null); exit.setSize(300, 200); exit.setLocation(0, 0); exit.setVisible(true); JLabel lab1 = new JLabel("平時成績:"); //標籤 lab1.setBounds(0,0,60,30); exit.add(lab1); final JTextField text1 = new JTextField(); //“平時成績”文本框 text1.setEditable(true); text1.setBounds(60,0,60,30); exit.add(text1); JLabel lab11 = new JLabel("所佔比例為30%"); lab11.setBounds(130,0,100,30); exit.add(lab11); JLabel lab2 = new JLabel("實驗成績:"); //標籤 lab2.setBounds(0,40,60,30); exit.add(lab2); final JTextField text2 = new JTextField(); //“實驗成績”文本框 text2.setEditable(true); text2.setBounds(60,40,60,30); exit.add(text2); JLabel lab22 = new JLabel("所佔比例為20%"); lab22.setBounds(130,40,100,30); exit.add(lab22); JLabel lab3 = new JLabel("期末成績:"); //標籤 lab3.setBounds(0,80,60,30); exit.add(lab3); final JTextField text3 = new JTextField(); //“期末成績”文本框 text3.setEditable(true); text3.setBounds(60,80,60,30); exit.add(text3); JLabel lab33 = new JLabel("所佔比例為50%"); lab33.setBounds(130,80,100,30); exit.add(lab33); final JTextField text4 = new JTextField(); text4.setEditable(true); text4.setBounds(150,120,60,30); exit.add(text4); final JButton but1 = new JButton("總成績:"); //標籤 but1.setBounds(60, 120, 80, 30); exit.add(but1); but1.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if (e.getSource() == but1){ double a = Double.parseDouble(text1.getText()); double b = Double.parseDouble(text2.getText()); double c = Double.parseDouble(text3.getText()); double d = a*0.2 + b*0.3 + c*0.5; text4.setText(String.valueOf(d)); } } }); exit.addWindowListener(new WindowAdapter(){ //退出事件監聽 public void windowClosing(WindowEvent arg0){ System.exit(1); } }); } }
时间: 2024-10-10 15:00:33