Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从中的不同类到GUI类的条形图_Java_Paint - Fatal编程技术网

Java 从中的不同类到GUI类的条形图

Java 从中的不同类到GUI类的条形图,java,paint,Java,Paint,我有一个带有JPanel和JButton的Gui类。单击按钮时,我希望在我的JPanel中显示图形。图在不同的类中。有人能帮我做这个吗 GUI类: public class Gui extends JFrame implements ActionListener{ JButton showGraph; public Gui() { super("GUI"); setSize(1200,600); setDefaultCloseOperat

我有一个带有JPanel和JButton的Gui类。单击按钮时,我希望在我的JPanel中显示图形。图在不同的类中。有人能帮我做这个吗

GUI类:

 public class Gui  extends JFrame implements ActionListener{  

     JButton showGraph;

     public Gui() {

    super("GUI");
    setSize(1200,600);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    showGraph = new JButton("SHOW GRAPH");
    JPanel mainPanel = new JPanel();
    add(mainPanel);
    mainPanel.setLayout(new GridLayout(2,0,10,10));
    mainPanel.setBorder(new EmptyBorder(10,10,10,10));
    mainPanel.add(showGraph);
    JPanel graphPanel = new JPanel();
    graphPanel.setBackground(Color.yellow);
    mainPanel.add(graphPanel);

    showGraph.addActionListener(this);

  }



 public static void main (String[] args){
    new Gui().setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == showGraph) {
        SimpleBarChart b =  new SimpleBarChart();
        b.getGraph();
    }
}
}
将getGraph()方法更改为获取JFrame并传入
this

public void getgraph(JFrame f) {

//JFrame f = new JFrame();
f.setSize(400, 300);
... as before ...
}
然后打电话进来

if (e.getSource() == showGraph) {
    SimpleBarChart b =  new SimpleBarChart();
    b.getGraph(this);
}

框架内不能有框架。另一种选择是让getGraph()返回一个JPanel,然后你可以将面板放在你现有的框架中,而不是更新整个框架。

ye这是在框架内创建新框架,而不是最好的解决方案,将Jpanel传入getGraph方法无效。请将
Jpanel graphPanel
作为类成员变量,以便保留对它的引用。然后让getGraph()返回一个JPanel并执行
graphPanel=b.getGraph()在您的操作中执行。然后打包你的相框。