View Single Post
  #2 (permalink)  
Old 12-21-2007, 11:32 AM
CaptainMorgan's Avatar
CaptainMorgan CaptainMorgan is offline
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
You need to implement an ActionListener for the event you wish to operate on. The key here is to override the abstract method actionPerformed() otherwise you'll get an abstract class but non-abstract method error. Make sure you use the try/catch properly by writing a catch statement.

Go here for information on event listeners.


Code:
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; import java.io.*; import java.util.Scanner; import java.util.Arrays; public class Ch14FlowLayoutSample extends JFrame implements ActionListener { private static final int FRAME_WIDTH = 145; private static final int FRAME_HEIGHT = 170; private static final int FRAME_X_ORIGIN = 70; private static final int FRAME_Y_ORIGIN = 50; public static void main (String[] args) { JFrame jFrame; jFrame = new JFrame(); JOptionPane.showMessageDialog(jFrame, "This is a Geography Quiz"); JOptionPane.showMessageDialog(null, "Good Luck"); char choice; int i, choice1, Password; String yourChoice, passString; passString = JOptionPane.showInputDialog("Enter the Password"); //Password = passString.nextInt(); Password = Integer.parseInt(passString); if (Password == 123) { JOptionPane.showMessageDialog(null, "Valid. You typed the right password. Now choose from the following menu"); Ch14FlowLayoutSample frame = new Ch14FlowLayoutSample(); frame.setVisible(true); } else { JOptionPane.showMessageDialog(null, "Invalid Password. Try Again"); } } public Ch14FlowLayoutSample() { Container contentPane; JButton button1, button2, button3, button4, button5; setSize (FRAME_WIDTH, FRAME_HEIGHT); setTitle("Geography Quiz"); setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN); contentPane = getContentPane(); contentPane.setBackground(Color.pink); contentPane.setLayout(new FlowLayout()); button1 = new JButton("Plate Tectonics"); button2 = new JButton("Rivers"); button3 = new JButton("Rocks"); button4 = new JButton("Quit"); contentPane.add(button1); contentPane.add(button2); contentPane.add(button3); contentPane.add(button4); button1.addActionListener(this); button1.setActionCommand("b1"); button4.addActionListener(this); button4.setActionCommand("b4"); setDefaultCloseOperation(EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e) { if ("b1".equals(e.getActionCommand())) { Scanner s = null; try { s = new Scanner(new BufferedReader(new FileReader("plate_tectonics.txt"))); while (s.hasNext()) { s.useDelimiter(",\\s*"); JOptionPane.showInputDialog(null,s.nextLine()); } } catch (java.io.FileNotFoundException f) { JOptionPane.showMessageDialog(null, "File not found."); } finally { if (s != null) s.close(); } } else if ("b4".equals(e.getActionCommand())) { System.exit(0); } } }
Reply With Quote