简体   繁体   中英

My JButton should execute and a rectangle should me seen on my Pane. How do i make it visible?

I have to code an interface with 4 Buttons. The buttons should draw a rectangle. I have set the layout and wanted to implement the action the button "Schwarz" (black in English) does after clicking it but it wont do anything... Are they alternatives of letting a button draw a rectangle or square?

I also tried to add a jpanel to my already implementet panel but to set the visiblity on false. After pressing the butten the boolean state should change but that didint work either

...

public class MyPanel extends javax.swing.JPanel {


    boolean E = false;

    public void paintComponement (Graphics rechtecke) {
        if(E == true) {
        super.paintComponents(rechtecke);
        rechtecke.setColor(Color.black);
        rechtecke.fillRect(200, 400, 400, 400);
        }

    }
    public MyPanel(){
        setBackground(Color.green);

...

...

public class MyFrame extends javax.swing.JFrame implements ActionListener  {    


public MyFrame()
 {
        int a = 1200;
        int b = 800;
        setSize(a, b);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        foad = new MyPanel();


        JButton Entfernen = new JButton("Entfernen");
        JButton Bild = new JButton("Bild");
        add(foad, BorderLayout.CENTER);
        //foad.setBackground(Color.GREEN);;
        JButton Rot = new JButton("rot");
        JButton Schwarz = new JButton("Schwarz");



        Entfernen.addActionListener(this);
        Bild.addActionListener(this);
        Rot.addActionListener(this);
        Schwarz.addActionListener(this);
        setVisible(true);
        }



        public void actionPerformed(ActionEvent e) { 
             if (e.getSource()==this.Schwarz) {
                 foad.E = true;

                repaint();}
        }

...

I do get the layout with the buttons and everything. It is just the functunalitiy of the button

I found it. the super.paintcomponent has to be out side of my if satement.

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