?? converter.java
字號(hào):
import java.awt.*;import java.awt.event.*;import java.util.*;import java.applet.Applet;
class Unit { String description; double multiplier; Unit(String description, double multiplier) { super(); this.description = description; this.multiplier = multiplier; }}
class CPanel extends Panel implements ActionListener, AdjustmentListener, ItemListener{ TextField TEnter; Choice Cchoose; int max = 10000; Converter handle; Unit[] units; CPanel(Converter myController, String myTitle, Unit[] myUnits) { GridBagConstraints c = new GridBagConstraints(); GridBagLayout gridbag = new GridBagLayout(); setLayout(gridbag); handle = myController; units = myUnits; c.fill = GridBagConstraints.HORIZONTAL; Label label = new Label(myTitle, Label.CENTER); c.gridwidth = GridBagConstraints.REMAINDER; gridbag.setConstraints(label, c); add(label); TEnter = new TextField("0", 10); c.weightx = 1.0; c.gridwidth = 1; gridbag.setConstraints(TEnter, c); add(TEnter); TEnter.addActionListener(this); Cchoose = new Choice(); for (int i = 0; i < units.length; i++) { Cchoose.add(units[i].description); } c.weightx = 0.0; c.gridwidth = GridBagConstraints.REMAINDER; gridbag.setConstraints(Cchoose, c); add(Cchoose); Cchoose.addItemListener(this);
c.gridwidth = 1; } double getMultiplier() { int i = Cchoose.getSelectedIndex(); return units[i].multiplier; } public void paint(Graphics g) { Dimension d = getSize(); g.drawRect(0,0, d.width - 1, d.height - 1); } public Insets getInsets() { return new Insets(5,5,5,8); } double getValue() { double f; try { f = (double)Double.valueOf(TEnter.getText()).doubleValue(); } catch (java.lang.NumberFormatException e) { f = 0.0; } return f; } public void actionPerformed(ActionEvent e) { setSliderValue(getValue()); handle.Exchange(this); } public void itemStateChanged(ItemEvent e) { handle.Exchange(this); } public void adjustmentValueChanged(AdjustmentEvent e) { TEnter.setText(String.valueOf(e.getValue())); handle.Exchange(this); } void setValue(double f) { setSliderValue(f); TEnter.setText(String.valueOf((float)f)); } void setSliderValue(double f) { int sliderValue = (int)f; if (sliderValue > max) sliderValue = max; if (sliderValue < 0) sliderValue = 0; }}
public class Converter extends Applet { private CPanel MPanel, UPanel; private Unit[] ChoiceM = new Unit[3]; private Unit[] ChoiceU = new Unit[4]; public void init() { setLayout(new GridLayout(2,0,5,5)); ChoiceM[0] = new Unit("公分", 0.01); ChoiceM[1] = new Unit("公尺", 1.0); ChoiceM[2] = new Unit("公里", 1000.0); MPanel = new CPanel(this, "公制",ChoiceM); MPanel.setBackground(Color.white); ChoiceU[0] = new Unit("英吋", 0.0254); ChoiceU[1] = new Unit("英呎", 0.305); ChoiceU[2] = new Unit("碼", 0.914); ChoiceU[3] = new Unit("英哩", 1613.0); UPanel = new CPanel(this, "英制", ChoiceU); UPanel.setBackground(Color.white); add(MPanel); add(UPanel); } void Exchange(CPanel from) { CPanel to; if (from == MPanel) to = UPanel; else to = MPanel; double multiplier = from.getMultiplier() / to.getMultiplier(); to.setValue(multiplier * from.getValue()); } public void paint(Graphics g) { Dimension d = getSize(); g.drawRect(0,0, d.width - 1, d.height - 1); } public Insets getInsets() { return new Insets(5,5,5,5); } }
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -