>>32
For the program I wrote the only thing I asked for--and received--help with was:
for (int i=0; i < buttons.length; i++) {
buttons
[i] = new JButton("Seat " + (i+1) + " is open");
buttonPanel.add(buttons
[i]);
I did also ask for help with:
if (buttons
[i] == e.getSource ())
but ended up figuring out why it wasn't working shortly after posting the question.
Yep, those two pieces of code sure did make up the bulk of my program. You're right, I am a complete leech.
Learn how to use possessive pronouns.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import java.io.*;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class buttons extends JApplet
implements ActionListener{
private int seatCounter = 0;
private static final int MAX_NUMBER_SEATS = 15;
JButton[] buttons = new JButton[MAX_NUMBER_SEATS];
JButton exitButton = new JButton("Exit");
private JLabel statusLabel1 = new JLabel("Welcome!");
private JLabel statusLabel2 = new JLabel("Click seat number to book.");
private JLabel statusLabel3 = new JLabel("Click the exit button to exit program. Seat selection will be saved.");
private JLabel statusLabel4 = new JLabel ("To unbook a seat you have reserved click the occupied seat and enter the code when prompted.");
JPanel buttonPanel = new JPanel ( );
JPanel exitPanel = new JPanel ( );
JPanel instructionPanel = new JPanel ( );
Container contentPane = getContentPane();
JFrame frame = new JFrame ( );
{
contentPane.setLayout(new BorderLayout( ));
contentPane.add(buttonPanel, BorderLayout.CENTER);
contentPane.add(exitPanel, BorderLayout.SOUTH);
contentPane.add(instructionPanel, BorderLayout.NORTH);
buttonPanel.setBackground(Color.GRAY);
buttonPanel.setLayout(new GridLayout(5,3));
exitPanel.setBackground(Color.GRAY);
exitPanel.setLayout(new FlowLayout( ));
instructionPanel.setBackground(Color.GRAY);
instructionPanel.setLayout(new FlowLayout( ));
instructionPanel.setLayout(new BoxLayout(instructionPanel,BoxLayout.Y_AXIS));
instructionPanel.add(statusLabel1);
instructionPanel.add(statusLabel2);
instructionPanel.add(statusLabel3);
instructionPanel.add(statusLabel4);
for(int i=0; i<buttons.length; i++)
{
buttons
[i] = new JButton("Seat " + (i+1) + " is open.");
buttons
[i].addActionListener ((ActionListener) this);
buttonPanel.add(buttons
[i]);
}
File file = new File("C:/file.txt");
if (file.exists()){
readDataFromFile();
}
exitButton.addActionListener(this);
exitPanel.add(exitButton);
SeatsFilled();
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand( ).equals("Exit")){
int choice = JOptionPane.showConfirmDialog(null, "Do you want to exit?", "Confirm", JOptionPane.YES_NO_CANCEL_OPTION);
if(choice == JOptionPane.YES_OPTION){
createFile();
System.exit(0);
}
}
else {
for(int i=0; i<buttons.length; i++){
if (buttons
[i] == e.getSource ()) {
if(buttons
[i].getActionCommand().equals("Occupied")){
int choice = JOptionPane.showConfirmDialog(null, "This seat is occupied - do you want to unbook this seat?", "Confirm", JOptionPane.YES_NO_CANCEL_OPTION);
if(choice == JOptionPane.YES_OPTION){
String code = JOptionPane.showInputDialog(null,"Enter code to unbook");
if(code.equals("7777")){
buttons
[i].setText("Seat " +(i+1)+" is open.");
seatCounter--;
}
}
}
else{
int choice = JOptionPane.showConfirmDialog(null, "Do you want to book this seat?", "Confirm", JOptionPane.YES_NO_OPTION);
if(choice == JOptionPane.YES_OPTION){
buttons
[i].setText("Occupied");
seatCounter++;
SeatsFilled();
}
}
}
}
}}
public void createAndShowGUI () {
frame = new JFrame ("buttons");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setLayout (new GridLayout (3, 1));
for (JButton button : buttons) {
buttonPanel.add (button);
}
frame.add(instructionPanel);
frame.add(buttonPanel);
frame.add(exitPanel);
frame.pack ();
frame.setVisible (true);
}
public void createFile() {
try
{
PrintWriter writer = new PrintWriter("c:/file.txt");
for(int i=0; i<16; i++){
if(i==15){
writer.println("" + seatCounter);
}
else{
writer.println(buttons
[i].getText());
}
}
writer.close();
}
catch (IOException e)
{
System.out.println("File not created or written correctly");
}
}
public void readDataFromFile()
{
try
{
BufferedReader reader = new BufferedReader (new FileReader("c:/file.txt"));
for (int i=0; i<16; i++){
if(i==15){
seatCounter = Integer.parseInt(reader.readLine());
}
else{
buttons
[i].setText(reader.readLine());
}
}
reader.close();
}
catch(FileNotFoundException e)
{
System.out.println("File not founnd at c:/file.txt");
}
catch(IOException e)
{
System.out.println("Something went wrong with reading or closing the file.");
}
}
private void SeatsFilled(){
if(seatCounter>=15){
JOptionPane.showMessageDialog(null, "All seats are booked.");
}
}
}