我迷失了如何在用户点击计算按钮后使用用户输入来创建图表。我是编程新手,推动正确的方向非常好。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class worksheet5 extends JApplet implements ActionListener {
double E, B, N;
JLabel title, firstBalance, lastBalance, userBalance1, userBalance2,
userBalance3, userBalance4, rateInvested, rateOfReturn;
JTextField begBalance, endBalance, balance1, balance2, balance3, balance4,
userRate, userReturn;
Button b;
public void init() {
setLayout(new BorderLayout());
setUpTitle();
setUpMiddle();
setUpBottom();
}
public void setUpTitle() {
title = new JLabel("Calculate your return rate", JLabel.CENTER);
add(title, BorderLayout.NORTH);
}
public void setUpMiddle() {
firstBalance = new JLabel("What was your starting balance?");
begBalance = new JTextField("", 15);
userBalance1 = new JLabel("What was your balance after year one?");
balance1 = new JTextField("", 15);
userBalance2 = new JLabel("What was your balance after year two?");
balance2 = new JTextField("", 15);
userBalance3 = new JLabel("What was your balance after year three?");
balance3 = new JTextField("", 15);
userBalance4 = new JLabel("What was your balance after year four?");
balance4 = new JTextField("", 15);
lastBalance = new JLabel("What was your ending balance year five?");
endBalance = new JTextField("", 15);
rateInvested = new JLabel("How many years did you invest?");
userRate = new JTextField("", 15);
JPanel pane1 = new JPanel(new FlowLayout());
pane1.add(firstBalance);
pane1.add(begBalance);
JPanel pane2 = new JPanel(new FlowLayout());
pane2.add(userBalance1);
pane2.add(balance1);
pane2.add(userBalance2);
pane2.add(balance2);
pane2.add(userBalance3);
pane2.add(balance3);
pane2.add(userBalance4);
pane2.add(balance4);
pane2.add(lastBalance);
pane2.add(endBalance);
JPanel pane3 = new JPanel(new FlowLayout());
pane3.add(rateInvested);
pane3.add(userRate);
JPanel mainPane = new JPanel(new GridLayout(3, 1));
mainPane.add(pane1);
mainPane.add(pane2);
mainPane.add(pane3);
add(mainPane, BorderLayout.CENTER);
}
public void setUpBottom() {
rateOfReturn = new JLabel("Your rate of return is :");
userReturn = new JTextField("", 15);
b = new Button("calculate");
b.addActionListener(this);
JPanel pane4 = new JPanel(new FlowLayout());
pane4.add(rateOfReturn);
pane4.add(userReturn);
pane4.add(b);
add(pane4, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == b) {
E = Double.parseDouble(endBalance.getText());
B = Double.parseDouble(begBalance.getText());
N = Double.parseDouble(userRate.getText());
double R = Math.pow(E / B, 1.0 / N) - 1;
userReturn.setText("" + R * 100 + "%");
}
}
}
class BarChart extends worksheet5 {
int n;
String year[];
int value[];
public void graph() {
n = 5;
year = new String[n];
value = new int[n];
year[0] = getParameter("year0");
year[1] = getParameter("year1");
year[2] = getParameter("year2");
year[3] = getParameter("year3");
year[4] = getParameter("year4");
value[0] = Integer.parseInt(begBalance.getText());
value[1] = Integer.parseInt(balance1.getText());
value[2] = Integer.parseInt(balance2.getText());
value[3] = Integer.parseInt(balance3.getText());
value[4] = Integer.parseInt(balance4.getText());
}
public void paint(Graphics g) {
Font font = new Font("Arial", Font.BOLD, 15);
g.setFont(font);
for (int i = 0; i < n; i++) {
g.setColor(Color.BLUE);
g.drawString(year[i], 20, i * 50 + 30);
g.setColor(Color.RED);
g.fillRect(70, i * 50 + 10, value[i], 40);
g.drawString(String.valueOf(value[i]) + "%", 180, i * 50 + 35);
}
String msg = "Bar Chart from Year 2001 - 2005";
g.setColor(Color.darkGray);
font = new Font("Arial", Font.BOLD, 20);
g.setFont(font);
g.drawString(msg, 50, 300);
}
}
答案 0 :(得分:3)
建议:
b
或s
,除非它们被用于琐碎的目的,例如for循环的索引。而是使用具有某种含义的名称,以便您的代码成为自我评论。遵循这些建议以及遵循良好的代码格式化实践将允许其他人(例如我们!)更好地理解您的代码,更重要的是,将允许您的未来自我更好地理解您在6个月前撰写代码。@Override
注释,以确保它是有效的覆盖。paintComponent(Graphics g)
方法覆盖的第一行应该是对super的方法的调用,以允许Java清理任何脏像素:super.paintComponent(g);