所以即使开始创建一个接受用户给出的值的折线图,我也遇到了问题。我有到目前为止的代码(目前只计算增长率,这是分配的一部分)。所以我需要做的是获取用户输入的所有值(bv1,ev_1,ev_2,ev_3,ev_4和ev_5 ...),并使用它们绘制线图上具有金额的点在Y轴上,以及在X轴上的年份。对于图形的标签,我也应该在Java中使用HTML格式作为轴的标签。我知道我应该使用像superpaint g和@Override这样的东西,但我不知道从哪里开始(一些示例代码会有所帮助)。任何帮助都表示赞赏,因为这项任务明天到期!
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.util.ArrayList;
import java.util.List;
public class Rate_Graph1 extends JApplet implements ActionListener
{
double E, B, N;
JLabel rateTitle, firstBv, ev1, ev2, ev3, ev4, ev5, returnRate;
JTextField bv1, ev_1, ev_2, ev_3, ev_4, ev_5, usersRate;
JButton calc;
public void setTop()
{
rateTitle = new JLabel("Calculate your return rate!", JLabel.CENTER);
add(rateTitle, BorderLayout.NORTH);
}
public void setMiddle()
{
firstBv = new JLabel("Enter the beginning value:");
bv1 = new JTextField("", 15);
ev1 = new JLabel("Enter the value after the first year:");
ev_1 = new JTextField("", 15);
ev2 = new JLabel("Enter the value after the second year:");
ev_2 = new JTextField("", 15);
ev3 = new JLabel("Enter the value after the third year:");
ev_3 = new JTextField("", 15);
ev4 = new JLabel("Enter the value after the fourth year:");
ev_4 = new JTextField("", 15);
ev5 = new JLabel("Enter the value after the fifth, and final year:");
ev_5 = new JTextField("", 15);
JPanel pane1 = new JPanel(new FlowLayout());
pane1.add(firstBv);
pane1.add(bv1);
pane1.add(ev1);
pane1.add(ev_1);
pane1.add(ev2);
pane1.add(ev_2);
pane1.add(ev3);
pane1.add(ev_3);
pane1.add(ev4);
pane1.add(ev_4);
pane1.add(ev5);
pane1.add(ev_5);
JPanel mainPane = new JPanel(new GridLayout(3, 1));
mainPane.add(pane1);
add(mainPane, BorderLayout.CENTER);
}
public void setBottom()
{
returnRate = new JLabel("Your return rate is:");
usersRate = new JTextField("", 15);
calc = new JButton("Calculate Rate!");
calc.addActionListener(this);
JPanel pane4 = new JPanel(new FlowLayout());
pane4.add(returnRate);
pane4.add(usersRate);
pane4.add(calc);
add(pane4, BorderLayout.SOUTH);
}
public void init()
{
setLayout(new BorderLayout());
setTop();
setMiddle();
setBottom();
}
public void actionPerformed(ActionEvent event)
{
if (event.getSource() == calc){
E = Double.parseDouble(ev_5.getText());
B = Double.parseDouble(bv1.getText());
double rate = Math.pow(E / B, 1.0 / 5.0) - 1;
usersRate.setText(+ rate * 100 + "%");
}
}
}