[英]Revalidate and repaint - Java 6
I am trying to compile my java class files however I am unable to do so due to the java on my school computer being old (java 6) whereas on my own laptop it works (java 7) 我正在尝试编译我的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);
}
});
}
}
Error received when compiling: 编译时收到错误:
GUI.java:90: cannot find symbol
symbol : method revalidate()
location: class java.awt.Container
getContentPane().revalidate();
^
java version is: Version 6 Update 45 java版本是:版本6更新45
Whereas I have been working on Version 7 when coding the project. 鉴于我在编写项目时一直在研究版本7。
It seems that version 6 of java does not recognize revalidate. 看来Java的6版本无法识别重新验证。
I have tried to use invalidate, since looking around invalidate is used for java 6. 我尝试使用了invalidate,因为环顾四周invalidate用于Java 6。
My question (apologies for not adding it at the start); 我的问题(很抱歉在开始时未添加); how would I compile it with java 6?
我将如何使用Java 6进行编译?
Thanks 谢谢
As a workaround you could do 作为解决方法,您可以执行
invalidate();
validate();
This approximates to what is occurring in revalidate without RootPane
recursion. 这近似于在没有
RootPane
递归的情况下在revalidate中发生的情况。
Another option is to invoke pack if the window size has not changed. 另一个选择是如果窗口大小未更改,则调用pack 。
Although upgrading to Java 7 would be simpler 尽管升级到Java 7会更简单
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.