繁体   English   中英

为什么我的JFrame出现变量声明错误?

[英]Why am I getting Variable Declaration error with JFrame?

我的最终目标是让用户输入三个跑步者的姓名以及他们完成跑步的时间。它将根据不佳的时间来组织跑步者,我希望将结果打印到表格中,但是当我在最后我得到以下错误;

error: variable declaration not allowed here
       JFrame frame = new JFrame();

当我分别运行JFrame代码时,它工作正常,但是当我一起编译所有代码时,我遇到了以上错误。

import java.awt.BorderLayout;
import javax.swing.*;

public class JTableCreatingDemo {
  public static void main(String args[]) {

    String runner1, runner2, runner3;
    double runTime1, runTime2, runTime3;

    //Get the names and run time of each runner 
    runner1 = JOptionPane.showInputDialog("Enter name of runner 1"); //runner1
    runTime1 = Double.parseDouble(JOptionPane.showInputDialog("Enter runtime of runner 1 in minutes")); //runTime1
    while (runTime1<0)
        {
            JOptionPane.showMessageDialog(null, "Invalid Entry");
            runtTime1 = Double.parseDouble(JOptionPane.showInputDialog("Enter runtime of runner 1 in minutes \nEntery format should be MM.SS"));
        }

    runner2 = JOptionPane.showInputDialog("Enter name of runner 2"); //runner2
    runTime2 = Double.parseDouble(JOptionPane.showInputDialog("Enter runtime of runner 2 in minutes")); //runTime2
    while (runTime2<0)
        {
            JOptionPane.showMessageDialog(null, "Invalid Entry");
            runtTime2 = Double.parseDouble(JOptionPane.showInputDialog("Enter runtime of runner 2 in minutes \nEntery format should be MM.SS"));
        }

    runner3 = JOptionPane.showInputDialog("Enter name of runner 3"); //runner3
    runtime3 = Double.parseDouble(JOptionPane.showInputDialog("Enter runtime of runner 3 in minutes")); //runTime3
    while (runTime3<0)
        {
            JOptionPane.showMessageDialog(null, "Invalid Entry");
            runtTime3 = Double.parseDouble(JOptionPane.showInputDialog("Enter runtime of runner 3 in minutes \nEntery format should be MM.SS"));
        }

    //Sort the runners by their respective run times
    if(runtime1<=runtime2 && runtime1<=runtime3) //runner1 is the fastest
    {
        if(runtime2<=runtime3)
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            Object rowData[][] = { { runner1, runtime1 + " minutes", "-" },
                { runner2, runtime2 + " minutes", (runtime2-runtime1) },
                {runner3, runtime3 + " minutes", (runtime3-runTime1)} };
            Object columnNames[] = { "Runners Name", "Finish Time", "Differnce" };
            JTable table = new JTable(rowData, columnNames);

            JScrollPane scrollPane = new JScrollPane(table);
            frame.add(scrollPane, BorderLayout.CENTER);
            frame.setSize(300, 150);
            frame.setVisible(true);
    }

  }
}

试试这个代码,它应该可以工作。 我只是修复了括号错误:)

import java.awt.BorderLayout;
import javax.swing.*;

public class JTableCreatingDemo {
  public static void main(String args[]) {

    String runner1, runner2, runner3;
    double runTime1, runTime2, runTime3;

    //Get the names and run time of each runner 
    runner1 = JOptionPane.showInputDialog("Enter name of runner 1"); //runner1
    runTime1 = Double.parseDouble(JOptionPane.showInputDialog("Enter runtime of runner 1 in minutes")); //runTime1
    while (runTime1<0)
        {
            JOptionPane.showMessageDialog(null, "Invalid Entry");
            runtTime1 = Double.parseDouble(JOptionPane.showInputDialog("Enter runtime of runner 1 in minutes \nEntery format should be MM.SS"));
        }

    runner2 = JOptionPane.showInputDialog("Enter name of runner 2"); //runner2
    runTime2 = Double.parseDouble(JOptionPane.showInputDialog("Enter runtime of runner 2 in minutes")); //runTime2
    while (runTime2<0)
        {
            JOptionPane.showMessageDialog(null, "Invalid Entry");
            runtTime2 = Double.parseDouble(JOptionPane.showInputDialog("Enter runtime of runner 2 in minutes \nEntery format should be MM.SS"));
        }

    runner3 = JOptionPane.showInputDialog("Enter name of runner 3"); //runner3
    runtime3 = Double.parseDouble(JOptionPane.showInputDialog("Enter runtime of runner 3 in minutes")); //runTime3
    while (runTime3<0)
        {
            JOptionPane.showMessageDialog(null, "Invalid Entry");
            runtTime3 = Double.parseDouble(JOptionPane.showInputDialog("Enter runtime of runner 3 in minutes \nEntery format should be MM.SS"));
        }

    //Sort the runners by their respective run times
    if(runtime1<=runtime2 && runtime1<=runtime3) //runner1 is the fastest
    {
        if(runtime2<=runtime3) 
           {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            Object rowData[][] = { { runner1, runtime1 + " minutes", "-" },
                { runner2, runtime2 + " minutes", (runtime2-runtime1) },
                {runner3, runtime3 + " minutes", (runtime3-runTime1)} };
            Object columnNames[] = { "Runners Name", "Finish Time", "Differnce"
           }
 };
            JTable table = new JTable(rowData, columnNames);

            JScrollPane scrollPane = new JScrollPane(table);
            frame.add(scrollPane, BorderLayout.CENTER);
            frame.setSize(300, 150);
            frame.setVisible(true);
    }

    }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM