簡體   English   中英

無法使用窗口調整圖形大小(Java)

[英]can't get graph to resize with window (Java)

自1900年以來,我正在構建一個程序來繪制嬰兒名字的流行程度。我已經獲得了圖表部分,但是無法使用窗口調整圖表的大小。 我已使用以下代碼將組件偵聽器添加到擴展GCanvas的類中:

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();

}

關於我可能做錯了什么的任何想法?

APPLICATION_WIDTHAPPLICATION_HEIGHT是否已更改? 全部大寫的名稱對我來說意味着固定的值(沒有看到實際的定義)。 由於您是根據這些值計算所有內容,因此如果您無法正確處理調整大小,我不會感到驚訝

暫無
暫無

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

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