Prosty kod w javie, czemu po nacisnieciu na jeden z przycisków nie zmienia się kolor tla? Gdzie jest błąd?

0

package przycisk;
import javax.swing.;
import java.awt.
;
import java.awt.event.*;

public class Przycik extends JFrame{
public Przycisk () {
super("Przyciski");
this.setBounds(100,200,300,400);
this.setDefaultCloseOperation(3);
InitComponents();

}

public void InitComponents () {

button1.addActionListener(new ActionListener() {
    
    public void actionPerformed(ActionEvent ae) {
        panel.setBackground(Color.YELLOW);
    }
});
 panel.add(button1);

GroupLayout layout= new GroupLayout(getContentPane ());
this.getContentPane().setLayout(layout);
layout.setHorizontalGroup (
        layout.createSequentialGroup()
        .addComponent(button1)
        .addGroup (
                layout.createParallelGroup().addComponent(button2).addComponent(button4)
        )
        .addComponent(button3)
);
layout.setVerticalGroup(
        layout.createSequentialGroup()
        .addGroup (
                layout.createParallelGroup().addComponent(button1).addComponent(button2).addComponent(button3)
        )
        .addComponent(button4)
                );
this.getContentPane().add(panel);

}
JButton button1=new JButton("1");
JButton button2= new JButton ("2");
JButton button3=new JButton("2");
JButton button4= new JButton("4");
JPanel panel=new JPanel();

public static void main(String[] args) {
    new Przycisk().setVisible(true);
 
}

}

0
  1. Formatuj kod na forum.
  2. Masz actionListenera podpiętego pod button1 dlatego nie działa pod jakimkolwiek przyciskiem, tylko pod "1"
  3. Działa, po prostu ten panel jest wielkości przycisku i nie widać zmiany.
    wpisz taki kod do initComponents
panel.setSize(new Dimension(100,100));

I dalej kombinuj :)

0
package przycisk;

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

public class Przycisk extends JFrame {
	
private static final long serialVersionUID = 1L;

private static final int WIDTH = 300;
private static final int HEIGHT = 400;

public Przycisk () {
	this.setBounds(100,200,WIDTH,HEIGHT);
	this.setDefaultCloseOperation(3);
	InitComponents();
}

public void InitComponents () {

button1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
        panel.setBackground(Color.YELLOW);
        repaint();
    }
});

panel.setSize(new Dimension(WIDTH,HEIGHT));
panel.add(button1);

GroupLayout layout= new GroupLayout(getContentPane ());
this.getContentPane().setLayout(layout);
layout.setHorizontalGroup (
        layout.createSequentialGroup()
        .addComponent(button1)
        .addGroup (
                layout.createParallelGroup().addComponent(button2).addComponent(button4)
        )
        .addComponent(button3)
);

layout.setVerticalGroup(
        layout.createSequentialGroup()
        .addGroup (
                layout.createParallelGroup().addComponent(button1).addComponent(button2).addComponent(button3)
        )
        .addComponent(button4)
                );
this.getContentPane().add(panel);
}

JButton button1=new JButton("1");
JButton button2= new JButton ("2");
JButton button3=new JButton("2");
JButton button4= new JButton("4");
JPanel panel=new JPanel();

public static void main(String[] args) {
    
	Przycisk p = new Przycisk();
	p.setVisible(true);

}

}

1 użytkowników online, w tym zalogowanych: 0, gości: 1