Zwiększenie rozmiaru przycisków

0

Witam

Gdzie mogę dodać parametry żeby ustalić rozmiar a właściwie zwiększyć przyciski z standardowych?

package przyciski_akcja;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class ButtonPanel extends JPanel{

	public static final int HEIGHT = 300;
	public static final int WIDTH = 00;
	private JButton greenButton;
	private JButton blueButton;
	private JButton redButton;

	private JPanel buttonPanel;

	public ButtonPanel() {
		greenButton = new GreenButton();
		blueButton = new BlueButton();
		redButton = new RedButton();

		buttonPanel = this;

		setLayout(new FlowLayout());
		setPreferredSize(new Dimension(WIDTH, HEIGHT));
		add(greenButton);
		add(blueButton);
		add(redButton);
	}

	class GreenButton extends JButton implements ActionListener {

		GreenButton() {
			super("Green");
			addActionListener(this);
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			buttonPanel.setBackground(Color.GREEN);
		}
	}

	class BlueButton extends JButton implements ActionListener {

		BlueButton() {
			super("Blue");
			addActionListener(this);
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			buttonPanel.setBackground(Color.BLUE);
		}
	}

	class RedButton extends JButton implements ActionListener {

		RedButton() {
			super("Red");
			addActionListener(this);
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			buttonPanel.setBackground(Color.RED);
		}
	}

}

1

Np. tu:

        GreenButton() {
            super("Green");
            setPreferredSize(new Dimension(50,50));
            addActionListener(this);
    }

Możesz też skrócić kod, korzystając z możliwości javy 8.

greenButton = new JButton("Green");
greenButton.setPreferredSize(new Dimension(50,50));
greenButton.setActionListener(e -> buttonPanel.setBackground(Color.GREEN)); //tylko ten wiersz korzysta z 8

Klasa GreenButton jest zbędna.

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