저는 1900 년 이래로 아기 이름의 인기를 그래프로 나타 내기위한 프로그램을 만들고 있습니다. 그래프 부분을 얻었으나 그래프 크기를 조정할 수 없습니다 창.그래프로 창 크기를 조정할 수 없습니다. (자바)
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) { }
}
}
}
을하지만 다른 클래스에서의 메소드를 호출 할 때 아무 반응이 없습니다 : 나는 다음과 같은 코드로 GCanvas를 확장하는 클래스에 구성 요소 리스너를 추가했습니다.
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();
}
내가 잘못하고있는 것에 대한 아이디어가 있으십니까?
APPLICATION_WIDTH 및 APPLICATION_HEIGHT는 어디에서 변경 되었습니까? 모든 대문자 이름은 고정 값을 내게 의미합니다 (실제 정의를 보지 않고). – Attila
아니요, 문제가 수정되었습니다. 문제의 일부로 제공되었습니다. –
그 값을 기반으로 모든 것을 계산 했으므로 크기 조정을 제대로 처리하지 못하는 경우 나는 놀라지 않을 것입니다. – Attila