1

我正在尝试编译我的 java 类文件但是我无法这样做,因为我学校计算机上的 java 是旧的(java 6),而在我自己的笔记本电脑上它可以工作(java 7)

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

public class GUI extends JFrame {

    private Rainfall rainfall;

    /**
     * Creates new form GUI
     */
    public GUI(String fileName) {
        rainfall = new Rainfall(fileName);  //Initialise rainfall data
        this.setTitle("Rainfall Analysis");  //Define the title
        this.setSize(750, 650); //Define the size of the window
        this.setResizable(false); //Disable resizability
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //Definition of the menu
        JMenuBar menuBar = new JMenuBar(); // Initialisation of the first menu bar
        JMenu menuA = new JMenu("Rainfall Information"); // Build first menu
        // A group of JMenuItems
        JMenuItem menuItemA1 = new JMenuItem("Global Information"); // Create a menu item containing "Global Information" as text
        JMenuItem menuItemA2 = new JMenuItem("Month Calendar Based Information");
        JMenuItem menuItemA3 = new JMenuItem("Month Graphical Based Information");
        menuA.add(menuItemA1); 
        menuA.addSeparator(); // We add a seperator to seperate the menu items
        menuA.add(menuItemA2);
        menuA.addSeparator();
        menuA.add(menuItemA3);
        JMenu menuB = new JMenu("Rainfall Analysis"); // Initialisation of the second menu bar
        JMenuItem menuItemB1 = new JMenuItem("Yearly statistics"); // Create a menu item containing "Yearly Statistics" as text
        JMenuItem menuItemB2 = new JMenuItem("Comparison between two months' rainfall"); // Create a menu item 
        menuB.add(menuItemB1); // Add menuItem to the menu(menuB)
        menuB.addSeparator(); // We add a seperator to seperate the menu items
        menuB.add(menuItemB2);

        //Associate events to menu items
        menuItemA1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                setContentPane(new RecordsView(rainfall));   //Make RecordsView as the content panel
                getContentPane().revalidate();
                getContentPane().repaint();
            }
        });

        menuItemA2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                setContentPane(new MonthCalendarView(rainfall));//Make MonthCalendarView as the content panel
                getContentPane().revalidate();
                getContentPane().repaint();
            }
        });

        menuItemA3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                setContentPane(new MonthGraphicalView(rainfall));//Make MonthGraphicalView as the content panel
                getContentPane().revalidate();
                getContentPane().repaint();
            }
        });

        menuItemB1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                JPanel p = new JPanel();
                p.setSize(750, 650);
                setContentPane(p);
                getContentPane().add(rainfall.getGlobalChartByYears()); //Make Chart that show statistics over years as the content panel
                getContentPane().revalidate();
                getContentPane().repaint();
            }
        });
        menuItemB2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                setContentPane(new ComparisonView(rainfall)); // Set the comparisonView as the content of content pane so that it will be displayed in the GUI
                getContentPane().revalidate();
                getContentPane().repaint();
            }
        });
        menuBar.add(menuA); // Add menuA to menuBar
        menuBar.add(menuB); // Add menuB to menuBar
        //End menu

        this.setJMenuBar(menuBar); // Set the menu bar (menuBar) as the menu bar of the container
        this.setContentPane(new RecordsView(rainfall));

    }

    /**
     * @param args the command line arguments
     */
    public static void main(final String args[]) {

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUI(args.length==0?"2RainfallDataLanc.txt":args[0]).setVisible(true);
            }
        });
    }
}

编译时收到错误:

GUI.java:90: cannot find symbol
symbol  : method revalidate()
location: class java.awt.Container
getContentPane().revalidate();
                            ^

java版本是:版本6更新45

而我在编写项目时一直在使用第 7 版。

似乎 java 版本 6 不承认重新验证。

我尝试使用 invalidate,因为 java 6 使用了 invalidate 环顾四周。

我的问题(很抱歉没有在开始时添加它);我将如何用 java 6 编译它?

谢谢

4

1 回答 1

2

作为一种解决方法,你可以做

invalidate();
validate();

这近似于没有递归的重新验证中发生的情况。RootPane

如果窗口大小没有改变,另一个选项是调用pack 。

虽然升级到 Java 7 会更简单

于 2013-05-20T18:03:45.700 回答