【问题标题】:repaint method not working java重绘方法不起作用java
【发布时间】:2014-03-01 21:59:38
【问题描述】:

我有这段代码,我希望它重新绘制,这样当用户输入详细信息时,它会打印出加速度和油耗(在另一个类中计算),我可以看到它有效,因为我有 system.out.println's显示值,但它们没有更新到我的 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();
    }
}

【问题讨论】:

  • 你提到的window()在哪里调用,System.out在哪里调用
  • window 在另一个类中被调用,在 public static void main 部分中
  • 这还不够代码给你答案。请提供SSCCE,准确显示您在这两个课程中所做的事情。请注意,这不应该是您的整个程序,只要我们能得到一个想法就足够了。

标签: java repaint


【解决方案1】:

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

但是,在您的情况下,您的 JLabel 对象在 window() 方法中被创建为局部范围变量,因此它们不再易于访问(从技术上讲,自从您将它们添加到你的JPanel,但这太麻烦了)。

由于TestConsumption.printVan()显然是用来计算加速和油耗的,为了方便起见,我建议将JLabel accelJLabel fuel提升为实例变量,然后在@987654330中单独计算这两个值@。所以你的动作事件可能看起来像这样:

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();
}});

或者,您不必将 JLabels 提升为实例变量;只要您在window() 方法的顶部声明accelfuel为这两个按钮设置ActionListener,它们就可以在事件操作中通过actionPerformed 方法的作用域外壳。

一些旁注:

请记住,变量名称的 Java 命名约定是以小写字母开头。所以你的VanSportCar 变量应该是vansportCar。我已经在我的例子中这样写了它们。虽然它不会导致任何问题,但从语法上讲,它让人很难一眼就知道是在查看类名还是变量名。

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

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

【讨论】:

  • 非常感谢,这帮助我加载并解决了我的问题!
【解决方案2】:

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

【讨论】:

  • window 在另一个类的构造函数中被调用
猜你喜欢
  • 2018-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-27
  • 2018-08-25
  • 1970-01-01
相关资源
最近更新 更多