Added Event handling to buttons
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@16 72836036-5685-4462-b002-a69064685172
This commit is contained in:
parent
5a0be3261a
commit
839e020f4b
9 changed files with 135 additions and 0 deletions
|
@ -1,8 +1,14 @@
|
|||
package jrummikub.view;
|
||||
|
||||
import jrummikub.util.IEvent;
|
||||
|
||||
public interface IPlayerPanel {
|
||||
public IBoard getBoard();
|
||||
|
||||
public void setCurrentPlayerName(String playerName);
|
||||
public void setTimeLeft(int time);
|
||||
|
||||
public IEvent getSortByNumberEvent();
|
||||
public IEvent getSortByColorEvent();
|
||||
public IEvent getEndTurnEvent();
|
||||
}
|
|
@ -5,6 +5,8 @@ import java.awt.Dimension;
|
|||
import java.awt.GridBagConstraints;
|
||||
import java.awt.GridBagLayout;
|
||||
import java.awt.Insets;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.text.DecimalFormat;
|
||||
|
||||
import javax.swing.JButton;
|
||||
|
@ -12,6 +14,9 @@ import javax.swing.JLabel;
|
|||
import javax.swing.JPanel;
|
||||
import javax.swing.JProgressBar;
|
||||
|
||||
import jrummikub.util.Event;
|
||||
import jrummikub.util.IEvent;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class PlayerPanel extends JPanel implements IPlayerPanel {
|
||||
private final static DecimalFormat secondFormat = new DecimalFormat("00");
|
||||
|
@ -24,6 +29,10 @@ public class PlayerPanel extends JPanel implements IPlayerPanel {
|
|||
private JProgressBar timeBar;
|
||||
private JButton endTurnButton;
|
||||
|
||||
private Event sortByNumberEvent = new Event();
|
||||
private Event sortByColorEvent = new Event();
|
||||
private Event endTurnEvent = new Event();
|
||||
|
||||
|
||||
@Override
|
||||
public IBoard getBoard() {
|
||||
|
@ -42,6 +51,21 @@ public class PlayerPanel extends JPanel implements IPlayerPanel {
|
|||
timeBar.setString(Integer.toString(time/60) + ":" + secondFormat.format(time%60));
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEvent getSortByNumberEvent() {
|
||||
return sortByNumberEvent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEvent getSortByColorEvent() {
|
||||
return sortByColorEvent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEvent getEndTurnEvent() {
|
||||
return endTurnEvent;
|
||||
}
|
||||
|
||||
|
||||
JPanel createLeftPanel() {
|
||||
JPanel panel = new JPanel();
|
||||
|
@ -67,6 +91,12 @@ public class PlayerPanel extends JPanel implements IPlayerPanel {
|
|||
|
||||
sortByNumberButton = new JButton("<html><center>Sort by<br>number");
|
||||
sortByNumberButton.setPreferredSize(new Dimension(85, 50));
|
||||
sortByNumberButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
sortByNumberEvent.fire();
|
||||
}});
|
||||
|
||||
c.gridwidth = GridBagConstraints.RELATIVE;
|
||||
c.gridheight = GridBagConstraints.REMAINDER;
|
||||
c.insets = new Insets(15, 0, 20, 5);
|
||||
|
@ -75,6 +105,12 @@ public class PlayerPanel extends JPanel implements IPlayerPanel {
|
|||
|
||||
sortByColorButton = new JButton("<html><center>Sort by<br>color");
|
||||
sortByColorButton.setPreferredSize(new Dimension(85, 50));
|
||||
sortByColorButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
sortByColorEvent.fire();
|
||||
}});
|
||||
|
||||
c.gridwidth = GridBagConstraints.REMAINDER;
|
||||
c.insets = new Insets(15, 5, 20, 0);
|
||||
layout.setConstraints(sortByColorButton, c);
|
||||
|
@ -103,6 +139,12 @@ public class PlayerPanel extends JPanel implements IPlayerPanel {
|
|||
|
||||
endTurnButton = new JButton("End turn");
|
||||
endTurnButton.setPreferredSize(new Dimension(180, 50));
|
||||
endTurnButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
endTurnEvent.fire();
|
||||
}});
|
||||
|
||||
c.gridheight = GridBagConstraints.REMAINDER;
|
||||
c.insets = new Insets(15, 0, 20, 0);
|
||||
layout.setConstraints(endTurnButton, c);
|
||||
|
|
|
@ -7,6 +7,8 @@ import java.awt.Dimension;
|
|||
import javax.swing.JFrame;
|
||||
import javax.swing.UIManager;
|
||||
|
||||
import jrummikub.util.IListener;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class View extends JFrame implements IView {
|
||||
private Table table;
|
||||
|
@ -45,6 +47,7 @@ public class View extends JFrame implements IView {
|
|||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
// FIXME Only test main for GUI preview
|
||||
String nativeLF = UIManager.getSystemLookAndFeelClassName();
|
||||
|
||||
try {
|
||||
|
@ -60,6 +63,22 @@ public class View extends JFrame implements IView {
|
|||
view.getTable().setTopPlayerName("Player 3");
|
||||
view.getTable().setRightPlayerName("Player 4");
|
||||
|
||||
view.getPlayerPanel().getSortByNumberEvent().add(new IListener() {
|
||||
@Override
|
||||
public void fire() {
|
||||
System.out.println("'Sort by number' fired");
|
||||
}});
|
||||
view.getPlayerPanel().getSortByColorEvent().add(new IListener() {
|
||||
@Override
|
||||
public void fire() {
|
||||
System.out.println("'Sort by color' fired");
|
||||
}});
|
||||
view.getPlayerPanel().getEndTurnEvent().add(new IListener() {
|
||||
@Override
|
||||
public void fire() {
|
||||
System.out.println("'End turn' fired");
|
||||
}});
|
||||
|
||||
view.setVisible(true);
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue