📄 converterswing.java
字号:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import javagently.*;
class ConverterSwing extends JFrame {
/* The Converter Program by J M Bishop Dec 1998
* --------------------- Display version July 1999
* GUI version July 1999
* Swing version B Worrall
* and J Bishop Aug 2000
* Keeps the exchange rates from one currency into
* many others and enables currency exchanges to be
* estimated.
*
* Illustrates the use of a customised Swing GUI.
*/
public static void main(String[] args) throws IOException {
int look = 0;
if (args.length > 0)
look = Integer.parseInt(args[0]);
new ConverterSwing(look);
}
ConverterSwing (int look) throws IOException{
try {
switch (look) {
case 1: UIManager.setLookAndFeel
(UIManager.getCrossPlatformLookAndFeelClassName()); break;
case 2: UIManager.setLookAndFeel
(UIManager.getSystemLookAndFeelClassName()); break;
case 3: UIManager.setLookAndFeel
("javax.swing.plaf.metal.MetalLookAndFeel"); break;
case 4: UIManager.setLookAndFeel
("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
break;
case 5: UIManager.setLookAndFeel
("com.sun.java.swing.plaf.motif.MotifLookAndFeel"); break;
case 6: UIManager.setLookAndFeel
("javax.swing.plaf.mac.MacLookAndFeel"); break;
}
}
catch (Exception c) {
System.out.println("Could not use the specified look and feel"
+ "- defaulting to cross-platform");
}
DataHandler data = new DataHandler ();
data.initialise();
data.readIn();
// now control transfers to the user and events are handled
// via actionPerformed and the transaction() method
}
class DataHandler extends JFrame
implements ActionListener, SwingConstants {
JComboBox fromChoice, toChoice;
JTextField amountField;
JTextArea resultField;
JButton goButton;
void initialise () {
JPanel p = new JPanel (new BorderLayout());
// Heading
JLabel label = new JLabel ("Savannah Exchange",
new ImageIcon("Elephant.gif"),CENTER);
p.add("North", label);
// left hand side panel
JPanel q = new JPanel();
q.add ("North",new JLabel ("From"));
fromChoice = new JComboBox();
q.add("Center",fromChoice);
p.add ("West", q);
// right hand side panel
q = new JPanel();
q.add ("North",new JLabel ("To"));
toChoice = new JComboBox();
q.add("Center",toChoice);
p.add ("East",q);
// Centre panel
q = new JPanel(new BorderLayout());
JPanel r = new JPanel();
r.add(new JLabel("Amount"));
amountField = new JTextField("1000 ");
amountField.addActionListener(this);
r.add(amountField);
q.add("North",r);
resultField = new JTextArea(8,20);
q.add ("Center",resultField);
goButton = new JButton ("Convert");
goButton.addActionListener(this);
q.add("South",goButton);
p.add("Center",q);
getContentPane().add(p);
setTitle("Currency Converter");
setSize(610,300);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void actionPerformed (ActionEvent e) {
if (e.getSource() == goButton)
transaction();
}
void transaction () {
// First get the latest amount
int amount = (int)
Integer.parseInt(amountField.getText().trim());
// The to and from rates are loaded directly from the
// JComboBox and used to create the output
Rates fromRate = (Rates) fromChoice.getSelectedItem();
Rates toRate = (Rates) toChoice.getSelectedItem();
resultField.append(amount+" "+fromRate.country+" "+
fromRate.currency+
"\n in "+toRate.country+" "+
toRate.currency+"\n was "+
Stream.format(amount/fromRate.conversion*
toRate.conversion,10,3)+"\n\n");
}
void readIn() throws IOException {
Stream fin = new Stream ("Rates.data", Stream.READ);
Rates rate;
try {
for (int i = 0; ; i++) {
rate = new Rates();
rate.setRate(fin);
// Here we store the rate elements in the JComboBoxes.
// Note that the whole rate is stored, but only the
// elements specified in Rates' toString() method will
// show up in the list.
toChoice.addItem(rate);
fromChoice.addItem(rate);
}
}
catch (EOFException e) {}
setVisible(true);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -