/**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() { @Override public void itemStateChanged(ItemEvent e) { // TODO Auto-generated method stub if (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() { @Override public void itemStateChanged(ItemEvent e) { // TODO Auto-generated method stub if (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); } }); } }
时间: 2024-10-15 21:14:50