標籤:java
/**9
* 利用文字框,標籤,按鈕等完成一個介面
* 選擇輸入的是“人民幣”、“美元”
* 轉換成另一種幣種的貨幣轉換介面
**/
import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;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("貨幣轉換匯率計算");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,120,30);exit.add(text1);final JTextField text4 = new JTextField();text4.setEditable(true);text4.setBounds(130,120,120,30);exit.add(text4);JLabel lab2 = new JLabel("選擇幣種:");//標籤lab2.setBounds(0,30,60,20);exit.add(lab2);final JRadioButton jrb1 = new JRadioButton("人民幣");final JRadioButton jrb2 = new JRadioButton("美元");jrb1.addItemListener(new ItemListener() {@Overridepublic void itemStateChanged(ItemEvent e) {// TODO Auto-generated method stubif (e.getSource() == jrb1){jrb2.setSelected(false);double c = Double.parseDouble(text1.getText());c *= 0.1618;text4.setText(String.valueOf(c));}}});jrb1.setBounds(0,55,80,30);exit.add(jrb1);jrb2.addItemListener(new ItemListener() {@Overridepublic void itemStateChanged(ItemEvent e) {// TODO Auto-generated method stubif (e.getSource() == jrb2){jrb1.setSelected(false);double c = Double.parseDouble(text1.getText());c *= 6.228;text4.setText(String.valueOf(c));}}});jrb2.setBounds(85,55,80,30);exit.add(jrb2);exit.addWindowListener(new WindowAdapter(){//退出事件監聽public void windowClosing(WindowEvent arg0){System.exit(1);}});}}
本文出自 “hacker” 部落格,請務必保留此出處http://anglecode.blog.51cto.com/5628271/1619893
java期末考試 9