Project 5 (Optional!)
Administrative Details
Due Date: Friday, December 6, 2006 at 11:59pm. It cannot be turned in after December 6. However, if you can't find a TA or Dr. Ball then you can't get credit for it either. So, look carefully at the TA's office hours to find them to pass off with them.
Points: 25 points possible.
Project Description
Your optional assignment is to create a JFrame with widgets. Not doing this project will not hurt your grade.
Details:
- Start out the project by opening BlueJ.
- If a project is already present then close it.
- Start a new project - under the menu select "Project" then "New Project" - call the project whatever you would like.
Choose your own adventure! In order to get the extra credit you have to add functionality to your JFrame so that the TA can click on a menu item or button and text appear in a JTextField. This first part will guide you through setting up a JFrame and a JTextfield. The next two parts guide you through adding a button or adding a menu. Feel free to add both if you would like.
- If you would like to look at the documentation, here is a quick link.
- Create a new class - call it whatever you would like - for example, GUI.
- Let's suppose you wanted to call it "GUI".
- Now, so that the JFrame will appear we need to add the correct library. Type "import javax.swing.*" at the very top of the class file.
- In your constructor you need to create a JFrame object (e.g. "JFrame frame = new JFrame();")
- If your class does not compile then check your spelling and your syntax.
- Now let's allow the JFrame to close the way we expect it to when the "X" icon is pressed: "frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);".
- Let's set the size of the JFrame to be small: say 300 pixels by 400 pixels (e.g. "frame.setSize(300, 400);").
- Something that we are missing is making the JFrame visible! So, let's make it visible! (e.g. "frame.setVisible(true);").
- Test your JFrame. Compile the class and create a new object in BlueJ. Your JFrame should now appear (or be minimized).
-
- Let's add a title! Put whatever title you want (e.g. "frame.setTitle("Dr. Ball's title");).
- Now let's add the textfield: "JTextField text = new JTextField();"
- Compile your class and play with your text field. What? What do you mean your text field isn't there? Did you forget to add it to the frame? (e.g. "frame.getContentPane().add(text);")
- It still isn't there?!? Well, you probably had "frame.setVisible(true);" before you added the text field. Make sure that the "frame.setVisible(true);" command is the very last command in your constructor. Move the command, re-compile, and create the object again. You should now be able to add text to your text field.
Add a button:
- Let's add a button!
- Before the setVisible command create a button (e.g. "JButton button1 = new JButton("some text that I choose");). Then add the button to the JFrame (e.g. "frame.getContentPane().add(button1)")
- What does it look like? Where did the text field go????
- Hmmm.... Let's add a JPanel now so that our life is easier (e.g. "JPanel pan = new JPanel();"). Now, let's add the JPanel to the JFrame (e.g. "frame.getContentPane().add(pan)"). Now, instead of adding the text field and button to the JFrame, let's add them to the JPanel (e.g. "pan.add(text);" and "pan.add(button1);")
- Where did the text field go? Is that tiny rectangle the text field? Hmmm... to keep things simple let's just give the text field some basic text when we create it (e.g. instead of "JTextField text = new JTextField();" let's have "JTextField text = new JTextField("some default text - or whatever text you want here");
- Now let's make the button do something! First, we need to import another library: "import java.awt.event.*;" (this goes at the very top).
- Now we have to use an interface called "ActionListener" - we have to change the class declaration. Instead of "public class GUI" we now need "public class GUI implements ActionListener"
- We have a problem - we didn't override the method "actionPerformed"! So, let's do exactly that. After the constructor add "public void actionPerformed(ActionEvent e){}"
- Your class should now compile. Yeah!
- However, it doesn't do anything useful. So, in your actionPerformed method let's put text in the text field. Specifically, let's add a counter.
- So, let's create an instance variable called "counter" and add the code "counter++;" in your actionPerformed method. That should compile.
- Now, let's add text to the text field in your actionPerformed method (e.g. "text.setText(counter+" ");")
-
- Uh, oh. Compilation error. We have to make "text" (our text field) global. So, let's declare text as an instance variable (e.g. "JTextField text") and instantiate it in the constructor (e.g. "text = new JTextField("some default text - or whatever text you want here");")
- Hmmm... What now? I know! Let's make the button call the actionPerformed method everytime someone clicks on the button! Brillant!
- Let's add an "addActionListener" to the button (e.g. "button1.addActionListener(this);")
- It works!!!!!!!!!!!! Pass off with a TA or add the menu item as well.
Add a menu item:
- Let's add a menu item!
- Before the setVisible command create a menubar (e.g. "JMenuBar menuBar = new JMenuBar();). Then add the menu bar to the JFrame (e.g. "frame.setJMenuBar(menuBar);")
- If you run the JFrame there is no menubar to look at as there are no menus. So, let's make and add a menu (e.g. "JMenu menu = new JMenu("a menu");" and "menuBar.add(menu);"
- Nice, now we have a menu, but nothing in it! Let us create and add a menu item (e.g. "JMenuItem menuItem = new JMenuItem("do something!");" and "menu.add(menuItem);")
- Now let's make the menu item do something! First, we need to import another library: "import java.awt.event.*;" (this goes at the very top.
- Now we have to use an interface called "ActionListener" - we have to change the class declaration. Instead of "public class GUI" we now need "public class GUI implements ActionListener"
- We have a problem - we didn't override the method "actionPerformed"! So, let's do exactly that. After the constructor add "public void actionPerformed(ActionEvent e){}"
- Your class should now compile. Yeah!
- However, it doesn't do anything useful. So, in your actionPerformed method let's put text in the text field. Specifically, let's add a counter.
- So, let's create an instance variable called "counter" and add the code "counter++;" in your actionPerformed method. That should compile.
- Now, let's add text to the text field in your actionPerformed method (e.g. "text.setText(counter+" ");")
-
- Uh, oh. Compilation error. We have to make "text" - our text field global. So, let's declare text as an instance variable (e.g. "JTextField text") and instantiate it in the constructor (e.g. "text = new JTextField("some default text - or whatever text you want here");)
- Hmmm... What now? I know! Let's make the menu item call the actionPerformed method everytime someone clicks on the menu item! Brillant!
- Let's add an "addActionListener" to the menuItem (e.g. "menuItem.addActionListener(this);")
- It works!!!!!!!!!!!! Pass off with a TA or add the menu item as well.
How to submit
Have a TA look at your JFrame. Have him/her press your button or menu item to make sure that it works. If one of them works you get the extra credit. They should add the extra credit directly to BlackBoard. Have a nice day.