2012-04-16 51 views
0

我正在构建一个程序来绘制自1900年以来婴儿名称的流行度。我已经获得了图形部分,但无法调整图形的大小窗户。我添加了一个组件侦听器,用下面的代码扩展GCanvas类:无法通过窗口调整窗口大小(Java)

import acm.graphics.*; 
import java.awt.event.*; 
import java.util.*; 
import java.awt.*; 

public class NameSurferGraph extends GCanvas 
implements NameSurferConstants, ComponentListener { 

private static final long serialVersionUID = 1L; 

//Sets up the graph 
public NameSurferGraph() { 
    addComponentListener(this); 
    nameList = new ArrayList<NameSurferEntry>(); 
} 

//Adds an entry to the array of names being graphed 
public void addEntry(NameSurferEntry entry) { 
    nameList.add(entry); 
} 

//Clears and then redraws the graph 
public void update() { 
    removeAll(); 
    drawBackground(); 
    for (int i=0; i<nameList.size(); i++) { 
      drawLineForOneName(i); 
    } 
} 

//Draws the background lines and labels for the graph 
private void drawBackground() { 
    String decade = ""; 
    for (int i = 0; i<NDECADES; i++) { 
     double x = (APPLICATION_WIDTH/NDECADES)*i; 
     decade = new Integer(STARTING_DECADE+10*i).toString(); 
     GLine line = new GLine(x,0,x,APPLICATION_HEIGHT); 
     GLine hLine = new GLine(0,GRAPH_MARGIN_SIZE,APPLICATION_WIDTH-APPLICATION_WIDTH/NDECADES,GRAPH_MARGIN_SIZE); 
     GLine hLine2 = new GLine(0,APPLICATION_HEIGHT-GRAPH_MARGIN_SIZE,APPLICATION_WIDTH-APPLICATION_WIDTH/NDECADES,APPLICATION_HEIGHT-GRAPH_MARGIN_SIZE); 
     GLabel label = new GLabel(decade); 
     label.setLocation(x+4,APPLICATION_HEIGHT); 
     add(line); 
     add(hLine); 
     add(hLine2); 
     add(label); 
     } 
} 

//More code draws the actual lines. 

//ivars 
private ArrayList<NameSurferEntry> nameList; 
private GLine line; 

//Implementation of the ComponentListener interface 
public void componentHidden(ComponentEvent e) { } 
public void componentMoved(ComponentEvent e) { } 
public void componentResized(ComponentEvent e) { update(); } 
public void componentShown(ComponentEvent e) { } 
} 
} 
} 

但是,当我调用一个方法就可以从另一个类,没有任何反应。

import acm.program.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class NameSurfer extends Program implements NameSurferConstants { 

private static final long serialVersionUID = 1L; 

private NameSurferDataBase database; 
private String name; 
private NameSurferEntry entry = new NameSurferEntry(name); 

//Initializes the program 
public void init() { 
    putInteractors(); 
    database = new NameSurferDataBase(NAMES_DATA_FILE); 
} 

//Adds the interactors 
public void putInteractors() { 
    JButton clear = new JButton("Clear"); 
    JButton clickGraph = new JButton("Graph"); 
    JLabel nameLabel = new JLabel("Name"); 
    add(graph); 
    add(nameLabel,SOUTH); 
    add(textfield,SOUTH); 
    add(clickGraph,SOUTH); 
    add(clear,SOUTH); 
    addActionListeners(); 
    textfield.addActionListener(this); 
    getMouseListeners(); 
} 
//Other code gets user input  

//Graphs the name 
public void graphName(String name) { 
    entry = database.findEntry(name); 
    graph.addEntry(entry); 
    graph.update(); 
} 

//ivars 
private JTextField textfield = new JTextField(20); 
public NameSurferGraph graph = new NameSurferGraph(); 

} 

关于我可能做错什么的想法?

+0

APPLICATION_WIDTH和APPLICATION_HEIGHT是否改变了任何地方?全部大写的名字意味着固定值给我(没有看到实际的定义)。 – Attila 2012-04-16 01:39:01

+0

不,它们是固定的 - 它们是问题的一部分。 – 2012-04-16 02:14:40

+0

既然你正在计算所有基于这些值的东西,我不会被惊异,如果这是什么阻止你正确处理调整大小 – Attila 2012-04-16 02:17:26

回答

0

APPLICATION_WIDTHAPPLICATION_HEIGHT是否改变了?全部大写的名字意味着固定值给我(没有看到实际的定义)。既然你正在计算基于这些值的一切,如果这是什么阻止你正确处理调整大小,我不会感到惊讶

+0

这应该是一条评论,但这是一个很好的问题。 – fireshadow52 2012-04-16 01:32:14