我想从按钮单击中调用一个方法,然后从该方法中将变量传递给DrawPanel类,该类扩展了Jpanel来画一条线。我有4个类,MainFem,DrawPanel,DrawLine和Callmethod1。

问题在于新值不会添加到LinkedList中。

主分类:MainFem

package paintpack;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;



public class MainFem extends JFrame {

JPanel buttonsPanel = null;
JButton dcomp = null;
DrawPanel drawPanel = null;

  public MainFem(){

  add(getDrawPanel(),BorderLayout.CENTER);

  add(getButtonPanel(), BorderLayout.SOUTH);
  getButtonPanel().add(getDcomp());
  }

  private JPanel getButtonPanel() {
  if(buttonsPanel == null){
      buttonsPanel = new JPanel();
       buttonsPanel.setBorder(BorderFactory.createLineBorder(Color.black));
      buttonsPanel.setPreferredSize(new Dimension(500, 100));
  }

  return buttonsPanel;
  }



  private DrawPanel getDrawPanel(){
  if(drawPanel == null){
      drawPanel = new DrawPanel();
      drawPanel.setVisible(true);
  }

  return drawPanel;
  }



  private JButton getDcomp() {
  if(dcomp == null){
      dcomp = new JButton("Click");
      dcomp.addActionListener(new ActionListener() {

          @Override
          public void actionPerformed(ActionEvent e) {
              Callmethod1 call1 = new Callmethod1();
              call1.passmethod1();
              getDrawPanel().repaint();
          }
      });
  }
  return dcomp;
  }

public static void main(String[] args) {
MainFem wnd = new MainFem();


wnd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
wnd.setPreferredSize(new Dimension(800, 600));
Container c = wnd.getContentPane();
c.setBackground(Color.red);
wnd.pack();
wnd.setVisible(true);



}

}


DrawPanel类

package paintpack;


import java.awt.Graphics;

import javax.swing.JPanel;



public class DrawPanel extends JPanel{
/**
 *
 */
private static final long serialVersionUID = 1L;
private DrawLines dlines;

public DrawPanel() {
    dlines = new DrawLines();
}

public void method1(int x1, int y1, int x2, int y2) {
    dlines.addLine(x1, y1, x2, y2);
    repaint();
    //System.out.println("bingo");
}



// beging PAINTING
@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    dlines.draw(g);

}

}


画线

package paintpack;


import java.awt.Graphics;
import java.util.LinkedList;

public class DrawLines{

public DrawLines() {

}

private static class Line {
    final int x1;
    final int y1;
    final int x2;
    final int y2;

    public Line(int x1, int y1, int x2, int y2) {
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;

    }

}

private final LinkedList<Line> lines = new LinkedList<Line>();

public void addLine(int x1, int y1, int x2, int y2) {

    lines.add(new Line(x1, y1, x2, y2));
}

public void clearLines() {
    lines.clear();
}

public void draw(Graphics g){
    for (Line line : lines) {
        g.drawLine(line.x1, line.y1, line.x2, line.y2);
    }
}


}

呼叫方法1

package paintpack;


public class Callmethod1 {
private DrawPanel pass1;

public Callmethod1() {
pass1 = new DrawPanel();
}

public void passmethod1(){
pass1.method1(300, 300, 200, 200);
pass1.repaint();
}

}

最佳答案

问题是您在DrawPanel的构造函数中分配了Callmethod1的新实例。因此,您正在另一个DrawPanel实例上绘制线,该实例不可见。要解决此问题,只需传递现有DrawPanel ei的实例:

public Callmethod1(DrawPanel drawPanel) {
    this.pass1 = drawPanel;
}


在动作监听器中:

@Override
public void actionPerformed(ActionEvent e) {
    Callmethod1 call1 = new Callmethod1(drawPanel);
    call1.passmethod1();
}


另外,您可以利用现有的Line2D类绘制线。

08-05 17:13