start button action caught

git-svn-id: svn://anubis/gvsu@88 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
josh 2008-03-28 22:56:14 +00:00
parent 33310aef16
commit 9a09f532ff

View File

@ -10,7 +10,7 @@ public class KnightsTour extends JFrame
private KnightsTourBoard m_ktBoard = new KnightsTourBoard(5, 5); private KnightsTourBoard m_ktBoard = new KnightsTourBoard(5, 5);
/* GUI elements */ /* GUI elements */
private ActionEventHandler m_handler; private ActionEventHandler m_handler = new ActionEventHandler(this);
private JTextField m_widthField; private JTextField m_widthField;
private JTextField m_heightField; private JTextField m_heightField;
private JTextField m_startXField; private JTextField m_startXField;
@ -26,12 +26,8 @@ public class KnightsTour extends JFrame
setDefaultCloseOperation(EXIT_ON_CLOSE); setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500, 600); setSize(500, 600);
m_handler = new ActionEventHandler(this);
Container c = getContentPane();
/* set up the main window contents */ /* set up the main window contents */
c.add(m_mainPanel = new JPanel(new BorderLayout()) {{ getContentPane().add(m_mainPanel = new JPanel(new BorderLayout()) {{
setBorder(BorderFactory.createEmptyBorder(9, 9, 9, 9)); setBorder(BorderFactory.createEmptyBorder(9, 9, 9, 9));
add(new JPanel() {{ add(new JPanel() {{
setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
@ -70,6 +66,7 @@ public class KnightsTour extends JFrame
setBorder(BorderFactory.createEmptyBorder()); setBorder(BorderFactory.createEmptyBorder());
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
add(m_startButton = new JButton("Start Knight's Tour")); add(m_startButton = new JButton("Start Knight's Tour"));
m_startButton.addActionListener(m_handler);
add(m_statusLabel = new JLabel()); add(m_statusLabel = new JLabel());
}}); }});
}}); }});
@ -88,9 +85,8 @@ public class KnightsTour extends JFrame
{ {
int width = m_ktBoard.getWidth(); int width = m_ktBoard.getWidth();
int height = m_ktBoard.getHeight(); int height = m_ktBoard.getHeight();
if (m_boardPanel != null) JPanel oldBoardPanel = m_boardPanel;
m_mainPanel.remove(m_boardPanel); m_boardPanel = new JPanel(new GridLayout(width, height, 1, 1));
m_boardPanel = new JPanel(new GridLayout(width, height));
m_boardPanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0)); m_boardPanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0));
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
{ {
@ -100,9 +96,12 @@ public class KnightsTour extends JFrame
? "h" ? "h"
: new Integer(m_ktBoard.getStepAt(x, y)).toString(), : new Integer(m_ktBoard.getStepAt(x, y)).toString(),
SwingConstants.CENTER); SwingConstants.CENTER);
l.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
m_boardPanel.add(l); m_boardPanel.add(l);
} }
} }
if (oldBoardPanel != null)
m_mainPanel.remove(oldBoardPanel);
m_mainPanel.add(m_boardPanel); m_mainPanel.add(m_boardPanel);
m_mainPanel.validate(); m_mainPanel.validate();
} }
@ -115,6 +114,10 @@ public class KnightsTour extends JFrame
public void actionPerformed(ActionEvent e) public void actionPerformed(ActionEvent e)
{ {
if (e.getSource() == m_startButton)
{
m_statusLabel.setText("Running...");
}
} }
} }