📄 growthframe.java
字号:
//Copyright (c) 1998, Arthur Gittleman
//This example is provided WITHOUT ANY WARRANTY either expressed or implied.
/* Revises Example 4.5, Growth.java, to make it a graphical
* rather than a console program.
*/
import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat;
public class GrowthFrame extends Frame
implements ActionListener {
private TextField getRate = new TextField(5);
private TextField getBalance = new TextField(8);
private TextField getYears = new TextField(3);
private Label rate = new Label("Rate");
private Label balance = new Label("Balance");
private Label years = new Label("Years");
private MyCanvas canvas = new MyCanvas();
private Button button = new Button("Amount");
private String amount = "Press button";
private NumberFormat nf;
public GrowthFrame(String title) {
super(title);
Panel p1 = new Panel();
p1.setLayout(new GridLayout(2,1));
p1.add(rate);
p1.add(getRate);
Panel p2 = new Panel();
p2.setLayout(new GridLayout(2,1));
p2.add(balance);
p2.add(getBalance);
Panel p3 = new Panel();
p3.setLayout(new GridLayout(2,1));
p3.add(years);
p3.add(getYears);
Panel p = new Panel();
p.add(p1);
p.add(p2);
p.add(p3);
Panel p4 = new Panel();
p4.add(button);
add(p,"North");
add(canvas,"Center");
add(p4,"South");
addWindowListener(new CloseWindow());
button.addActionListener(this);
nf = NumberFormat.getCurrencyInstance();
}
public String computeGrowth() {
double rate = new Double(getRate.getText()).doubleValue();
double balance = new Double(getBalance.getText()).doubleValue();
int years = new Integer(getYears.getText()).intValue();
for (int i = 1; i <= years; i++)
balance += balance * rate/100;
return nf.format(balance);
}
public void actionPerformed(ActionEvent event) {
amount = "The amount is " + computeGrowth();
canvas.repaint();
}
public static void main(String [] args) {
GrowthFrame f = new GrowthFrame("Growth");
f.setSize(300,200);
f.show();
}
class CloseWindow extends WindowAdapter {
public void windowClosing(WindowEvent event) {
System.exit(0);
}
}
class MyCanvas extends Canvas {
public MyCanvas() {
Font f = new Font("Serif",Font.BOLD,24);
setFont(f);
}
public void paint(Graphics g) {
g.drawString(amount,10,30);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -