簡體   English   中英

重新驗證並重新繪制-Java 6

[英]Revalidate and repaint - 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版本無法識別重新驗證。

我嘗試使用了invalidate,因為環顧四周invalidate用於Java 6。

我的問題(很抱歉在開始時未添加); 我將如何使用Java 6進行編譯?

謝謝

作為解決方法,您可以執行

invalidate();
validate();

這近似於在沒有RootPane遞歸的情況下在revalidate中發生的情況。

另一個選擇是如果窗口大小未更改,則調用pack

盡管升級到Java 7會更簡單

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM