如何从java中的按钮面板更改JPanel中的paintcomponent

如何从java中的按钮面板更改JPanel中的paintcomponent,java,swing,jframe,panel,Java,Swing,Jframe,Panel,我很难弄清楚如何在绘图面板中更改绘画组件的颜色 我们需要使用的方法是,可以通过按钮面板更改油漆组件的颜色 问题是,我似乎找不到让处理程序识别面板并更改其颜色的方法 如何从java中的按钮面板更改JPanel中的paintcomponent main.java import javax.swing.SwingUtilities; public class main { public static void main(String[] args) { SwingUtilit

我很难弄清楚如何在绘图面板中更改绘画组件的颜色

我们需要使用的方法是,可以通过按钮面板更改油漆组件的颜色

问题是,我似乎找不到让处理程序识别面板并更改其颜色的方法

如何从java中的按钮面板更改JPanel中的paintcomponent

main.java

import javax.swing.SwingUtilities;

public class main {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                new Window();
            }           
        });     
    }
}
import javax.swing.JFrame;
import java.awt.BorderLayout;
import java.awt.Dimension;

public class Window extends JFrame {

    public Window () {
        // `super ' calls a function inherited from the parent class ( JFrame )
        super();
        setTitle("Callbacks");
        setSize (new Dimension(420, 350));

        // Make sure the window appears in the middle of your screen
        setLocationRelativeTo(null);

        // Determines what should happen when the frame is closed
        setDefaultCloseOperation (EXIT_ON_CLOSE);

        // Chooses a certain layout type for the elements in this frame
        getContentPane().setLayout (new BorderLayout());

        // TODO : add elements to the content pane
        DrawPanel dp = new DrawPanel ();
        ButtonPanel bp = new ButtonPanel ();

        // Places the DrawPanel in the center of the frame
        getContentPane (). add (dp , BorderLayout . CENTER );
        // Places the ButtonPanel in the top of the frame
        getContentPane (). add (bp , BorderLayout . NORTH );


        // Set the window to visible ! Yup ... This is necessary
        setVisible (true);
    }
}
import java.awt.Color;
import java.awt.Graphics;

import javax.swing.*;

public class DrawPanel extends JPanel {

    private Color color;

    public DrawPanel(){
        super();
        color = Color.BLACK ;
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(getColor());
        g.fillRect(100 , 30, 200 , 200);
    }

}
import javax.swing.JButton;
import javax.swing.JPanel;

public class ButtonPanel extends JPanel {


    public ButtonPanel () {
        super ();
        // Add a button to the panel . The argument to the JButton constructor
        // will become the text on the button .
        JButton b = new JButton ("Change color!");
        JButton c = new JButton ("Test");
        add (b);
        add (c);
        b.addActionListener(new InputHandler());

    }
}
Window.java

import javax.swing.SwingUtilities;

public class main {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                new Window();
            }           
        });     
    }
}
import javax.swing.JFrame;
import java.awt.BorderLayout;
import java.awt.Dimension;

public class Window extends JFrame {

    public Window () {
        // `super ' calls a function inherited from the parent class ( JFrame )
        super();
        setTitle("Callbacks");
        setSize (new Dimension(420, 350));

        // Make sure the window appears in the middle of your screen
        setLocationRelativeTo(null);

        // Determines what should happen when the frame is closed
        setDefaultCloseOperation (EXIT_ON_CLOSE);

        // Chooses a certain layout type for the elements in this frame
        getContentPane().setLayout (new BorderLayout());

        // TODO : add elements to the content pane
        DrawPanel dp = new DrawPanel ();
        ButtonPanel bp = new ButtonPanel ();

        // Places the DrawPanel in the center of the frame
        getContentPane (). add (dp , BorderLayout . CENTER );
        // Places the ButtonPanel in the top of the frame
        getContentPane (). add (bp , BorderLayout . NORTH );


        // Set the window to visible ! Yup ... This is necessary
        setVisible (true);
    }
}
import java.awt.Color;
import java.awt.Graphics;

import javax.swing.*;

public class DrawPanel extends JPanel {

    private Color color;

    public DrawPanel(){
        super();
        color = Color.BLACK ;
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(getColor());
        g.fillRect(100 , 30, 200 , 200);
    }

}
import javax.swing.JButton;
import javax.swing.JPanel;

public class ButtonPanel extends JPanel {


    public ButtonPanel () {
        super ();
        // Add a button to the panel . The argument to the JButton constructor
        // will become the text on the button .
        JButton b = new JButton ("Change color!");
        JButton c = new JButton ("Test");
        add (b);
        add (c);
        b.addActionListener(new InputHandler());

    }
}
DrawPanel.java

import javax.swing.SwingUtilities;

public class main {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                new Window();
            }           
        });     
    }
}
import javax.swing.JFrame;
import java.awt.BorderLayout;
import java.awt.Dimension;

public class Window extends JFrame {

    public Window () {
        // `super ' calls a function inherited from the parent class ( JFrame )
        super();
        setTitle("Callbacks");
        setSize (new Dimension(420, 350));

        // Make sure the window appears in the middle of your screen
        setLocationRelativeTo(null);

        // Determines what should happen when the frame is closed
        setDefaultCloseOperation (EXIT_ON_CLOSE);

        // Chooses a certain layout type for the elements in this frame
        getContentPane().setLayout (new BorderLayout());

        // TODO : add elements to the content pane
        DrawPanel dp = new DrawPanel ();
        ButtonPanel bp = new ButtonPanel ();

        // Places the DrawPanel in the center of the frame
        getContentPane (). add (dp , BorderLayout . CENTER );
        // Places the ButtonPanel in the top of the frame
        getContentPane (). add (bp , BorderLayout . NORTH );


        // Set the window to visible ! Yup ... This is necessary
        setVisible (true);
    }
}
import java.awt.Color;
import java.awt.Graphics;

import javax.swing.*;

public class DrawPanel extends JPanel {

    private Color color;

    public DrawPanel(){
        super();
        color = Color.BLACK ;
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(getColor());
        g.fillRect(100 , 30, 200 , 200);
    }

}
import javax.swing.JButton;
import javax.swing.JPanel;

public class ButtonPanel extends JPanel {


    public ButtonPanel () {
        super ();
        // Add a button to the panel . The argument to the JButton constructor
        // will become the text on the button .
        JButton b = new JButton ("Change color!");
        JButton c = new JButton ("Test");
        add (b);
        add (c);
        b.addActionListener(new InputHandler());

    }
}
ButtonPanel.java

import javax.swing.SwingUtilities;

public class main {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                new Window();
            }           
        });     
    }
}
import javax.swing.JFrame;
import java.awt.BorderLayout;
import java.awt.Dimension;

public class Window extends JFrame {

    public Window () {
        // `super ' calls a function inherited from the parent class ( JFrame )
        super();
        setTitle("Callbacks");
        setSize (new Dimension(420, 350));

        // Make sure the window appears in the middle of your screen
        setLocationRelativeTo(null);

        // Determines what should happen when the frame is closed
        setDefaultCloseOperation (EXIT_ON_CLOSE);

        // Chooses a certain layout type for the elements in this frame
        getContentPane().setLayout (new BorderLayout());

        // TODO : add elements to the content pane
        DrawPanel dp = new DrawPanel ();
        ButtonPanel bp = new ButtonPanel ();

        // Places the DrawPanel in the center of the frame
        getContentPane (). add (dp , BorderLayout . CENTER );
        // Places the ButtonPanel in the top of the frame
        getContentPane (). add (bp , BorderLayout . NORTH );


        // Set the window to visible ! Yup ... This is necessary
        setVisible (true);
    }
}
import java.awt.Color;
import java.awt.Graphics;

import javax.swing.*;

public class DrawPanel extends JPanel {

    private Color color;

    public DrawPanel(){
        super();
        color = Color.BLACK ;
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(getColor());
        g.fillRect(100 , 30, 200 , 200);
    }

}
import javax.swing.JButton;
import javax.swing.JPanel;

public class ButtonPanel extends JPanel {


    public ButtonPanel () {
        super ();
        // Add a button to the panel . The argument to the JButton constructor
        // will become the text on the button .
        JButton b = new JButton ("Change color!");
        JButton c = new JButton ("Test");
        add (b);
        add (c);
        b.addActionListener(new InputHandler());

    }
}
InputHandler

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import com.sun.prism.paint.Color;

public class InputHandler implements ActionListener {

    public InputHandler() {
        // TODO Auto-generated constructor stub
    }

    public void actionPerformed (ActionEvent e) {
        // TODO : add code here that will
        // be ran when the button is clicked
    }
}

希望你们能帮助我,或者把我送到正确的方向。

让他们成为朋友,这样
听众就可以知道
面板了。

// Window
DrawPanel dp = new DrawPanel ();
ButtonPanel bp = new ButtonPanel (dp); // pass panel to button
然后:

以及:

然后:


主要的问题是,代码在没有实际需要的情况下扩展组件。这适用于
窗口
按钮面板
(虽然扩展
绘图面板
)是有逻辑的。是的,这是我正在处理的作业的一部分。当我在寻找答案时,他们也没有扩展。通过一个表示您想要共享的数据的相互契约(即
接口
)将两者绑定在一起,在契约中提供一个观察者模式,以便感兴趣的各方可以监视数据的更改并大量更新他们自己的签名!关于这个项目的最后一个问题是,是否有可能改变按键的形状?我必须使用油漆组件吗?当然,那么形状应该在外部描述到
paintComponent