重新验证并重新绘制 - Java 6

时间:2013-05-20 17:55:42

标签: java swing compatibility java-7 java-6

我正在尝试编译我的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版无法识别revalidate。

我尝试使用invalidate,因为查找invalidate用于java 6。

我的问题(对于在开始时不添加它而道歉);我将如何用java 6编译它?

由于

1 个答案:

答案 0 :(得分:2)

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

invalidate();
validate();

这近似于没有RootPane递归的revalidate中发生的情况。

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

虽然升级到Java 7会更简单