Fixed layout issues

This commit is contained in:
Dorian Zedler 2020-02-22 10:39:57 +01:00
parent acb0c067c7
commit 27f4b72fe2

View file

@ -1,52 +1,94 @@
package de.itsblue.ConnectFour; /*
Connect four - written in java
Copyright (C) 2020 Oliver Schappacher and Dorian Zedler
import javax.swing.JButton; This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.itsblue.ConnectFour;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.io.Serializable; import java.awt.event.*;
public class ButtonRow extends JPanel{ /**
* Button row is a class meant for usage with de.itsblue.ConnectFour. It is a
* row contining a given umber of buttons.
*
* @author Oliver Schappacher
* @author Dorian Zedler
*/
public class ButtonRow extends JPanel {
JButton inputButtons[]; /**
int buttoncount = 0; *
*/
private static final long serialVersionUID = 1L;
public void InitButton() { /**
* Array that sores all buttons in the row
*/
private PlateInsertButton inputButtons[];
/**
* The number of buttons in the row
*/
private int buttonCount = 0;
/**
* Constructor
*
* @param buttonCount The number of buttons to create
*/
ButtonRow(int buttonCount) {
// configure basic values
this.buttonCount = buttonCount;
this.inputButtons = new PlateInsertButton[buttonCount];
// configure the layout // configure the layout
this.setLayout(new GridLayout(1,7)); this.setLayout(new GridLayout(1, 7));
for (int i = 0; i < this.buttoncount; i++) { // insert all the buttons
this.inputButtons[i] = new JButton(); for (int i = 0; i < this.buttonCount; i++) {
this.inputButtons[i].setText(""+i); this.inputButtons[i] = new PlateInsertButton(i);
this.inputButtons[i].setText("" + i);
this.add(this.inputButtons[i]); this.add(this.inputButtons[i]);
} }
} }
ButtonRow(int buttoncount) { /**
this.buttoncount = buttoncount; * Function to add an ActionListener to all contained buttons
this.inputButtons = new JButton[buttoncount]; *
this.InitButton(); * @param listener
this.setBackground(Color.RED); */
public void addActionListenerToButtons(ActionListener listener) {
for (int i = 0; i < this.buttonCount; i++) {
this.inputButtons[i].addActionListener(listener);
}
} }
/**
* Override the paint function in order to adjust the button size when rescaling
*/
@Override @Override
public void paint(Graphics g){ public void paint(Graphics g) {
Dimension size = this.getSize(); Dimension size = this.getSize();
for (int i = 0; i < this.buttoncount; i++) { for (int i = 0; i < this.buttonCount; i++) {
this.inputButtons[i].setPreferredSize(new Dimension(size.width/this.buttoncount, size.height)); this.inputButtons[i].setPreferredSize(new Dimension(size.width / this.buttonCount, size.height));
} }
super.paint(g); super.paint(g);
} }
@Override
public void setPreferredSize(Dimension preferredSize) {
super.setPreferredSize(preferredSize);
}
} }