split paintComponent方法

时间:2018-04-20 01:21:25

标签: java swing paintcomponent

我是GUI新手,我正在编写一个程序,在图表上显示NameRecord的几个条目。当我只需要输入一个条目时,我在paintComponent中启动了整个框架。

但是,现在我需要修改它,以便存储NameRecords的多个条目,我决定使用ArrayList来存储。然后我希望能够搜索,清除这些条目。但是我在paintComponent中所拥有的东西太乱了。

如何修改此方法以便我有单独的类来存储条目并执行其他方法?因为我不能在本课程之外引用g。

@Override
public void paintComponent(Graphics g) {

    super.paintComponent(g);

    w = getWidth();
    h = getHeight();

    //draw horizontal lines
    g.drawLine(0, 20, w, 20);
    g.drawLine(0, h - 20, w, h - 20);

    //draw vertical lines
    for (int i = 1; i < 12; i++) {
        g.drawLine((w / 12) * i, 0, (w / 12) * i, h);
    }
    //draw lable of years
    g.drawString("   1900", 0, 13);
    g.drawString("   1900", 0, h - 7);

    int year = 1900;
    for (int i = 1; i < 12; i++) {
        g.drawString("   " + Integer.toString(year + (i * 10)), (w / 12 * i), 13);
        g.drawString("   " + Integer.toString(year + (i * 10)), (w / 12 * i), h - 7);
    }

    // draw line of rank
    if (nameData != null) {
        nameList.add(nameData);
        for (int i = 0; i < 11; i++) {
            g.drawLine((w / 12 * i), convertValue(nameData.getRank(i)),
                    (w / 12) * (i + 1), convertValue(nameData.getRank(i + 1)));
        }

        for (int i = 0; i < 12; i++) {
            String label = " ";
            label = (nameData.getName() + " " + nameData.getRank(i));
            g.drawString(label, (w / 12 * i), convertValue(nameData.getRank(i)));
        }
    }
}

我还使用了另一个类来进行所有GUI显示(我不确定这是否是说出来的方式,但是它调用了actionPerformed方法)。他们的关系让我很困惑。我将把它作为参考。

 @Override
public void actionPerformed(ActionEvent e) {
    String s = e.getActionCommand();

    if (s.equalsIgnoreCase("graph")) {
        doGraph();
    } else if (s.equalsIgnoreCase("search")) {
        findName(inputField.getText());
    } else if (s.equalsIgnoreCase("clear all")) {
        clear();
        //– remove all names from the NameGraph

    } else if (s.equalsIgnoreCase("clear one")) {

        //remove the name that was added to the NameGraph earliest.
    } else if (s.equalsIgnoreCase("quit")) {
        setVisible(false);
        dispose();
        System.exit(0);
    } else {
        // This is the response to the Enter Key pressed in inputField.
        doGraph();
    }
}

1 个答案:

答案 0 :(得分:0)

  

如何修改此方法以便我有单独的类来存储条目并执行其他方法?因为我不能在本课程之外引用g。

您可以通过 switch (...) { case 1: foo = <some function with a FooFunc signature>; break case 2: foo = <some other function with a FooFunc signature>; break; // ...etc. } 传递给方法 或其他类的方法来访问Graphics object g

//for example
class MyGraph{
    public void draw(Graphics g){
        //implements how the graph is drawn, for e.g:
        //g.drawLine(x, y);
    }
}

在显示图纸的面板类中:

class DisplayPanel extends JPanel{

    private MyGraph;

    public DisplayPanel(){
        MyGraph graph = new MyGraph();
    }

    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        graph.draw(g);            //let the objects draw themselves
    }
}