[英]How to change the paintcomponent in JPanel from a button panel in java
我很难弄清楚如何在DrawPanel中更改绘画组件的颜色。
我们需要使用的方法是可以通过ButtonPanel更改绘画组件的颜色。
问题是我似乎找不到找到使处理程序识别面板并更改其颜色的方法。
如何从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();
}
});
}
}
Window.java
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);
}
}
DrawPanel.java
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);
}
}
ButtonPanel.java
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
}
}
希望你们能帮助我或向正确的方向发送我。
使他们彼此成为朋友,以便Listener
了解Panel
:
// Window
DrawPanel dp = new DrawPanel ();
ButtonPanel bp = new ButtonPanel (dp); // pass panel to button
然后:
public class ButtonPanel extends JPanel {
public ButtonPanel (DrawPanel d) {
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(d)); // pass panel to listener
}
}
和:
public class InputHandler implements ActionListener {
DrawPanel d;
public InputHandler(DrawPanel d) {
this.d = d;
}
public void actionPerformed (ActionEvent e) {
d.setColor(Color.BLUE); // whatever
d.repaint(); // force painting
}
}
然后:
public class DrawPanel extends JPanel {
private Color color;
public DrawPanel(){
super();
color = Color.BLACK ;
}
public void setColor(Color c) {
this.color = c;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(getColor());
g.fillRect(100 , 30, 200 , 200);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.