Java 我如何使用一个文件中的代码影响另一个文件?

Java 我如何使用一个文件中的代码影响另一个文件?,java,swing,oop,netbeans,Java,Swing,Oop,Netbeans,在NetBeans 8.0中,我制作了一个Paint程序,我需要主文件中的代码来影响另一个文件(接口文件)中的某些内容。我该怎么做?我的代码: package paintapp; import java.awt.event.ActionEvent; import javax.swing.*; import java.awt.event.ActionListener; public class PaintApp extends javax.swing.JFrame { int co

在NetBeans 8.0中,我制作了一个Paint程序,我需要主文件中的代码来影响另一个文件(接口文件)中的某些内容。我该怎么做?我的代码:

package paintapp;


import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.event.ActionListener;

public class PaintApp extends javax.swing.JFrame {

    int colourR, colourG, colourB;
    static String Ccolour = "BLACK";

    public static void main(String[] args) {
        JFrame main = new JFrame("Tessellating Pi - Paint");
        PaintInterface pi = new PaintInterface();
        main.add(pi);
        main.setSize(1000, 1000);
        main.setVisible(true);
        JFrame j = new JFrame("Colour Chooser");
        JButton c = new JButton("Change Colour");
        j.add(c);
        j.setSize(150, 100);
        j.setVisible(true);
        c.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if ("BLACK".equals(Ccolour)) {
                    Ccolour = "RED";
                    //code to change code in interface to set colour to red
                }
            }
        }
        );
    }
}
这是接口:

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.*;

public class PaintInterface extends JPanel implements MouseListener, MouseMotionListener {

    static int x = 0, y = 0;

    @Override
    public void paint(Graphics g) {
        this.setBackground(Color.white);
        this.addMouseMotionListener(this);
        this.addMouseListener(this);
        g.setColor(Color.black);
        g.fillRect(x, y, 3, 3);
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        x = e.getX();
        y = e.getY();
        repaint();
    }

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void mouseMoved(MouseEvent e) {
    }
}
我需要将我更改颜色的信息传送到界面。我该怎么做?有没有其他方法可以做到这一点?

简单地说:

  • 创建setter方法来设置新颜色
  • 调用
    repaint
    更新形状

  • 首先,您可能希望读取和,然后从
    paint
    方法中删除
    addMouseMotionListener
    addMouseListener
    setBackground
    (并改用
    paintComponent

    接下来,您需要确定处理颜色管理的最佳方式,例如,您可以在
    JPanel
    上简单地使用
    setForeground
    ,并在绘制时调整
    图形
    的颜色

    @Override
    public void actionPerformed(ActionEvent e) {
        if ("BLACK".equals(Ccolour)){
            Ccolour="RED";
            pi.setForeground(Color.RED);
            pi.repaint();
        }
    
    }
    
    这意味着
    pi
    需要成为一个实例变量或
    final

    final PaintInterface pi=new PaintInterface();
    
    然后在
    PaintInterface
    类中,需要更新
    paintComponent
    方法

    @Override
    protected void paintComponent (Graphics g) {
        super.paintComponent(g);
        g.setColor(getForeground());
        g.fillRect(x,y,3,3);
    
    }
    

    我也不鼓励您使用
    main
    方法创建整个应用程序,除了问题之外,您还会遇到
    静态
    引用和可重用性方面的各种问题…

    .extensed JPanel@推翻public void paint(Graphics g){
    对于Swing组件,重写
    paintComponent
    而不是
    paint
    。重写其中一个时,首先调用super方法来绘制BG和边框等。