简体   繁体   中英

Java Swing/Graphics - repaint() or validate() doesn't work?

I'm from Germany. My English isn't very good but I try to do as less mistakes as possible. In addition it's my first question in this community and I hope I will get a useful and helpful answer (I've asked Google for more than 20 times)

So, I've wrote a program that paints two rectangles in different colors. When I click on a button, the color of one of these rectangles should change but it doesn't do so. I've tried repaint(), validate() and something I don't understand: pack() Nothing helps. The color changes when I resize the windows but it should changes when I click the button... Does somebody have an idea or the answer for this problem?

My code:

import java.awt.Graphics2D;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;

@SuppressWarnings("serial")

public class Test extends JFrame {

    public static boolean buttonClicked = false;

    public Test() {
        initComponents();
    }

    private void initComponents() {

        zeichne z = new zeichne();

        JFrame frame = new JFrame();
        frame.setTitle("Grafiktest");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 500);
        Dimension dm = new Dimension(500,500);
        frame.setMinimumSize(dm);

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());

        JButton button = new JButton("Push me!");
        button.setSize(100, 50);

        button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonActionPerformed(evt);
            }
        });

        panel.add(z);
        panel.add(button, BorderLayout.SOUTH);

        frame.setContentPane(panel);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {

        new Test();
    }

    private void buttonActionPerformed(java.awt.event.ActionEvent evt){
        if(buttonClicked == false)
            buttonClicked = true;
        else
            buttonClicked = false;

        repaint();
    }

    public static boolean getClicked() {
        return buttonClicked;
    }
 }

 @SuppressWarnings("serial")

 class zeichne extends JPanel {

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.drawRect(0, 0, 150, 100);
        g2.setColor(Color.black);
        g2.fillRect(0, 0, 150, 100);

        boolean clicked = Test.getClicked();

        if(clicked) {
            g2.setColor(Color.red);
        }
        else {
            g2.setColor(Color.gray);
        }
        g2.fillRect(10,10,130,80);

        System.out.println(g2.getColor());

    }
 }

The properties of a components should be defined within the component. For example when you use a JLabel you can use setText(...), setForeground(...), setFont(...) etc. to change the state of the label and then the label will repaint itself.

So when you do custom painting you should do the same thing. That is in your "Zeichne" class you create a method to change the state of the class like setSelected(...) . So your code might be something like:

public void setSelected(boolean selected)
{
    this.selected = selected;
    repaint();
}

You would also need to create the isSelected() method so you can access the state of the property. Then in the paintComponent(...) method you can do:

g2.setColor( selected ? Color.RED : Color.GRAY );
g2.fillRect(10,10,130,80);

Finally in the ActionListener the code would be:

z.setSelected( !z.isSelected() );

I get it! The extension of JFrame and the additional declaration of a JFrame was the problem. I've deleted the declaration, put the super() into the method Test() and modulated the used methods of JFrame. Now it's working.

Thanks for your help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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