Homework 3
For this homework the goal is to make a program with a gui that can take in
user input of a string and display that string in the gui.
And, the submission system seems to work, so try to use that
instead of emailing your homework.
Here is an example of the gui:
The gui should have:
- A JFrame which is the main window of the program.
- A JTextArea which is used as the "log" part of the window.
- A JTextField where the text can be entered.
- A JButton which can be use to take the text from the JTextField
and put it in the JTextArea.
The main goal of this program is that
when something is put in the text field and the "Enter" button on the screen
is clicked, that text should show up in the text area. I added the time to
it, but you don't have to.
There are examples in the book, and here are some more
links to tutorials which can help show how to use and make guis:
Here is an outline of how to make the program:
- You are going to have to import libraries that you will be using. Something like:
import javax.swing.*; - This is for the JFrame, JTextArea, ...
import java.awt.*; - This is for the ContentPane part.
import java.awt.event.*; - This is for the ActionListener part.
- Create a class. You can give it any name you want.
- In this class, and outside of any function, declare variables (and don't initialize) for:
- A JFrame object - This is the main window of the gui.
- A JTextArea object - This is the part where the text shows up.
- A JTextField object - This is the part where you enter words.
- A JButton object - This is a button that is used to take stuff from the text field
and put it in the text area.
So, if you were declaring a JFrame, you might say: JFrame frame;
This means that every instance of your class will have a variable of type JFrame called frame.
- Make the class have a function where you set up the frame. Something like:
"public void setUpFrame()" would work.
- In setUpFrame():
- Initialize the variable for your JFrame with a new JFrame object. Something like:
frame = new JFrame("Title goes here"); where frame is the name of your variable
you declared earlier.
- Initialize the variable for your JTextArea with a new JTextArea object. Like:
textArea = new JTextArea();
There are a lot of ways to set the size of this text area. One is
to use the setPreferredSize(new Dimension(300, 300)); function.
The "new Dimension(300, 300)" part makes a "Dimension" object that is 300 x 300.
- Initialize the variable for your JTextField with a new JTextField object. Like:
textField = new JTextField(15); where 15 is the number of columns you want.
- Initialize the variable for your JButton with a new JButton object. Like:
button = new JButton("Blah Blah"); where blah blah is the text on the button.
- Add "actionListeners" to your button and text field by using their "addActionListener()" function.
Something like: button.addActionListenter(this); would work. This will be used
later.
- Get the contentPane of the frame by using frame.getContentPane();
- Add the textArea, textField, and button to the contentPane by using its
add method
- Use the frame.pack() method which makes the window big enough to show all
of the stuff in it.
- Use the frame.setVisible(true) function to make the window show up on the screen.
- In your class' main function ("public static void main(String[] args){ ... your code ...}"),
make a new instance
of your class. Something like: MyClass mycls = new MyClass(); if your class' name was
"MyClass".
- Use this object to call the setUpFrame() function. (Like mycls.setUpFrame();)
- To make the button take text from the text field and put it in the text area:
- Make your class implement the "ActionListener" interface. So the start of your
class should look something like:
class MyClass implements ActionListener{ ... the rest of your code ...}
- Because it implements this interface, your class is going to have to define the function:
public void actionPerformed(ActionEvent ae)
- In the actionPerformed(ActionEvent ae) function:
- Get the text from the text field using that object's getText() function.
(like textField.getText();)
- Put that text in the text area using the text area object's append() function.
-
- Set the text in the text field to empty using the text field's setText() function.
This is only an outline of how to make the program. If you have a different way,
feel free to try it. I added the time to each message, but you don't have to. That is
just an example of what can be done.
Also, the submission system should be working for this homework (at least
I hope so). Here is how that system should work.
If it still doesn't work, you can email your homework instead.