重绘方法不工作java

时间:2014-02-05 12:49:55

标签: java repaint

我有这个代码,我希望它重新绘制,以便当用户输入详细信息时会打印出加速度和油耗(在另一个类中计算),我可以看到它有效,因为我有system.out.println的显示值但它们没有更新到我的JFrame。

window()在另一个类的另一个构造函数中调用,JFrame打开正常但不更新

有什么想法吗?

由于

public class Vehicle extends JFrame {

    protected static double horsepower;
    protected static double aerodynamics;
    protected static double weight;
    protected static double acceleration;
    protected static double topspeed;
    protected double fuelconsumption;
    protected String userHorsepower;
    protected String userWeight;
    protected String userTopspeed;
    protected String userInput = "No Current Selection";

    JPanel panel = new JPanel();
    JButton Van = new JButton("Add Van");



    public Vehicle(double horsepower, double weight, double aerodynamics, double topspeed){
        super();
    }

    public void window(){

        JButton Van = new JButton("Add Van Car");
        Van.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {

                userHorsepower = JOptionPane.showInputDialog("Enter Horsepower");
                horsepower = Double.parseDouble(userHorsepower);
                userWeight = JOptionPane.showInputDialog("Enter Weight");
                weight = Double.parseDouble(userWeight);
                userTopspeed = JOptionPane.showInputDialog("Enter Topspeed");
                topspeed = Double.parseDouble(userTopspeed);
                aerodynamics = 0.9;
                userInput = "Van";
                TestConsumption.printVan();
                repaint();
                return;

            }});

        JButton SportCar = new JButton("Add Sports Car");
        SportCar.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                        userHorsepower = JOptionPane.showInputDialog("Enter Horsepower");
                        horsepower = Double.parseDouble(userHorsepower);
                        userWeight = JOptionPane.showInputDialog("Enter Weight");
                        weight = Double.parseDouble(userWeight);
                        userTopspeed = JOptionPane.showInputDialog("Enter Topspeed");
                        topspeed = Double.parseDouble(userTopspeed);
                        aerodynamics = 0.5;
                        userInput = "Sports Car";
                        TestConsumption.printCar();
                        panel.repaint();
            }});

        JLabel userChoice = new JLabel(userInput);
        JLabel accel = new JLabel("Acceleration: " + acceleration);
        JLabel fuel = new JLabel("Fuel Consumption: " + fuelconsumption);

        panel.setLayout(new GridLayout(5,5,0,0));
        panel.add(Van);
        panel.add(SportCar);
        panel.add(userChoice);
        panel.add(accel);
        panel.add(fuel);
        add(panel);
        pack();
        setTitle("Title Here");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(true);
        setSize(300,200);
        setLocationRelativeTo(null);
        setVisible(true);
        repaint();
    }

在此课程中调用窗口

public class TestConsumption extends Vehicle {

    public TestConsumption(double horsepower, double weight, double aerodynamics, double topspeed) {
        super(horsepower, weight, aerodynamics, topspeed);
    }

    public static void main(String [] args){

        Vehicle vh = new Vehicle(500, 500, 500, 500);
        vh.window();
    }


    public static void printCar(){
        Vehicle Car = new SportCar(horsepower,weight,aerodynamics,topspeed);

        Car.acceleration();
        Car.showFuelConsumption();
    }

    public static void printVan(){

        Vehicle FirstVan = new Van(horsepower,weight,aerodynamics,topspeed);

        FirstVan.acceleration();
        FirstVan.showFuelConsumption();
    }
}

2 个答案:

答案 0 :(得分:0)

window()

中致电constructor Vehicle(double horsepower, double weight, double aerodynamics, double topspeed)

答案 1 :(得分:0)

据我们所知,您永远不会更新JLabel的文本。在计算该标签的新对应值后,您需要在每个JLabel上致电setText

但是,在您的情况下,您的JLabel对象在window()方法中被创建为本地范围变量,因此它们不再容易访问(从技术上讲,有一种方法可以访问它们你把它们添加到你的JPanel,但那是不必要的麻烦)。

由于TestConsumption.printVan()显然用于计算加速度和油耗,为了让生活更轻松,我建议将JLabel accelJLabel fuel提升为实例变量,然后制作单独的方法来计算TestConsumption中的两个值。因此,您的行动事件可能如下所示:

sportCar.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
        userHorsepower = JOptionPane.showInputDialog("Enter Horsepower");
        horsepower = Double.parseDouble(userHorsepower);

        userWeight = JOptionPane.showInputDialog("Enter Weight");
        weight = Double.parseDouble(userWeight);

        userTopspeed = JOptionPane.showInputDialog("Enter Topspeed");
        topspeed = Double.parseDouble(userTopspeed);

        aerodynamics = 0.5;
        userInput = "Sports Car";

        // These static methods would be added to your TestConsumption class
        acceleration = TestConsumption.calculateAcceleration(...whatever params required for this calculation...);
        fuelConsumption = TestConsumption.calculateFuelConsumption(...whatever params required for this calculation...);

        accel.setText("Acceleration: " + acceleration);
        fuel.setText("Fuel Consumption: " + fuelConsumption);

        panel.repaint();
}});

或者,您不必将JLabel提升为实例变量;只要您在accel方法之前的<{1}}方法的顶部声明fuelwindow(),您为这两个按钮设置了ActionListener,它们就会可以通过actionPerformed方法的范围内容在事件操作中访问。

一些附注:

请记住,变量名称的Java命名约定是以小写字母开头。因此,您的VanSportCar变量应为vansportCar。我在我的例子中写过它们。虽然它在语法上没有引起任何问题,但一眼就能看出是否正在查看类或变量名称。

据我所知,您的TestConsumption课程不需要延长Vehicle。它不是车辆;它似乎既是应用程序的起点,也是辅助类(静态方法)。

这似乎是一项学校作业,所以我不确定你是否已被明确告知以这种方式设计你的程序,但从设计的角度来看,你将你的概念“视图”和“模型”结合起来一起。 JFrame不是车辆;它是一个窗口,一个用于帮助表示数据的视图元素。您可能会发现它有助于清理代码,方法是将特定于车辆的字段和方法(例如accelerationtopspeed等)提取到一个名为Vehicle的单独的类中,然后可以使用SportCarVan进行子类化(正如您在TestConsumption中所指示的那样)。您的JFrame子类(让我们称之为MainWindow或类似的东西)将仅负责更新其数据表示(意味着我们的accelfuel JLabels,在此情况)。