繁体   English   中英

无法从另一个面板更新JPanel

[英]Can't update JPanel from another panel

我正在尝试从另一个面板更新一个面板。 一个显示图形和路径,另一个运行算法并更改路径。 当路径被更新时,我希望图形面板被更新,但是这没有发生。

这些都是面板类:(一个接收另一个作为参数。这是个坏主意吗?)

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.io.IOException;

import javax.swing.JPanel;

import algcomp.util.Graph;

// main canvas class where points of the graph are drawn
public class GraphPanel extends JPanel {

    private Graph graph;
    private int[] path;
    private boolean hasPath;


    public GraphPanel(String gf) throws IOException{
        super();
        graph = new Graph(gf);
        hasPath = false;
        repaint();

    }

    //preferred size of panel
    public Dimension getPreferredSize(){

        return (new Dimension(500, 400));
    }

    public void setGraph(Graph gr){
        graph = gr;
        repaint();
    }

    public Graph getGraph() {
        return graph;
    }

    public void setPath(int[] _path){
        path = _path;
        hasPath = true;
        //printArray(path);
        removeAll();
        revalidate();
        repaint();

    }

    //for testing
    private void printArray(int[] arr){
        for(int i = 0;i<arr.length;i++){
            System.out.print(arr[i]+",");
        }
    }

    public void paintComponent(Graphics g)
    {

        //System.out.println("amieventrying");
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D)g;
        for(int i=0;i<graph.points.size();i++){
            g2d.fillOval(graph.points.get(i).getX(), graph.points.get(i).getY(),5, 5);
        }
        g2d.setColor(Color.RED);
        if(hasPath == true){
            for(int i=0; i<path.length-1;i++){
                g2d.drawLine(graph.getPoint(i).getX(), graph.getPoint(i).getY(), graph.getPoint(i+1).getX(), graph.getPoint(i+1).getY());
            }
        }
    }
}

另一类:

package algcomp.gui.main;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import algcomp.alg.Genetic;
import algcomp.alg.PathChromosome;


public class OptionPanel extends JPanel{
    String type;
    GraphPanel gp;


    Genetic genalg;
    boolean alg_initialized;

    //genetic
    JLabel gensizeL;
    JLabel mutprobL;
    JLabel timerL;
    JTextField gensizeTF;
    JTextField mutprobTF;
    JTextField timerTF;

    JButton runstepB;
    JButton runallB; 

    public OptionPanel(String _type, GraphPanel _gp){
        type = _type;
        gp = _gp;

        runstepB = new JButton("Run Step");
        runallB = new JButton("Full Run"); 


        if(type.equals("Genetic")){
            genPan();
            alg_initialized = false;
        }

    }

    //preferred size of panel
    public Dimension getPreferredSize(){

        return (new Dimension(250,150));
    }

    //
    private void genPan(){
        gensizeL = new JLabel("Generation size: ", SwingConstants.RIGHT);
        mutprobL = new JLabel("Mutation probability: ", SwingConstants.RIGHT);
        timerL = new JLabel("Timer for full run (ms): ", SwingConstants.RIGHT);

        gensizeTF = new JTextField("20");
        mutprobTF = new JTextField("0.1");
        timerTF = new JTextField("60000");



        this.setLayout(new GridLayout(4,2));

        this.add(gensizeL);
        this.add(gensizeTF);
        this.add(mutprobL);
        this.add(mutprobTF);
        this.add(timerL);
        this.add(timerTF);
        this.add(runstepB);
        this.add(runallB);

        //This function runs when runstep button is clicked
        runstepB.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(!alg_initialized){
                    if(type.equals("Genetic")){
                        genalg = new Genetic(gp.getGraph(),Integer.parseInt(gensizeTF.getText()),Double.parseDouble(mutprobTF.getText()),Integer.parseInt(timerTF.getText()));
                        alg_initialized = true;
                    }
                }

            if(type.equals("Genetic")){
                gp.setPath(((PathChromosome) genalg.step()).getPath()); 
            }

            }
        });

        //This function runs when runall button is clicked
        runallB.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Will launch a full run someday :D ");
            }
        });
    }

}

这就是他们的称呼:

public void displayGUI() throws IOException{
        JFrame frame = new JFrame("Algorithm Comparison");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        fixedop = new FixedOptionPanel();
        allop = new JPanel();

        JPanel mainpanel = new JPanel();

        mainpanel.setLayout(new GridLayout(1,2));

        canvas = new GraphPanel("/u1/cmperezgavilantorres/workspacejava/graphs/g1");
        options = new OptionPanel("Genetic",canvas);


        allop.setLayout(new GridLayout(2,1));
        allop.add(fixedop);
        allop.add(options);
        mainpanel.add(canvas);
        mainpanel.add(allop);
        mainpanel.setBackground(Color.WHITE); 
        frame.setContentPane(mainpanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

您能看到GraphPanel类中的setPath方法似乎无效的任何原因吗?

请帮忙 :(

谢谢@kiheru,这实际上与面板无关。

这幅画很好,但是我正在调用graph.getPoint(i)而不是graph.getPoint(path[i])

非常感谢您的帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM