-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Additional updates made to UserInterface1 so it now dynamically popul…
…ates the menu based on the index file. Program still running in console while we build GUI windows.
- Loading branch information
Showing
3 changed files
with
190 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,150 @@ | ||
package madlibs; | ||
|
||
public class UserInterface1 { | ||
import javax.swing.*; | ||
import java.awt.*; | ||
import java.awt.event.*; | ||
import java.lang.reflect.Array; | ||
import java.util.ArrayList; | ||
|
||
/** | ||
* UserInterface1 class is a play area for testing implementation of GUI components using Swing library. | ||
* | ||
* @author Team 42 - Ross Beck-MacNeil, Paul Lysander, and Brenda Moss | ||
* | ||
* Design Progress Notes: This class is a test area for figuring out how to transition from console to GUI. | ||
*/ | ||
public class UserInterface1 extends JPanel implements ActionListener { | ||
|
||
private static final int STANDARD_WIDTH = 640; | ||
private JTextArea text; // a message will be posted to this text area | ||
// each time an event is generated by some | ||
// user action | ||
|
||
private JComboBox<String> combobox; // The pop-up menu. | ||
|
||
|
||
/** | ||
* This constructor adds several GUI components to the panel and listens | ||
* for action events from some of them. | ||
*/ | ||
public UserInterface1(ArrayList<MenuEntry> childrensMenu, ArrayList<MenuEntry> classicsMenu) { | ||
|
||
setBorder(BorderFactory.createLineBorder(Color.GRAY, 3)); | ||
setBackground(Color.WHITE); | ||
|
||
|
||
|
||
//Sets BorderLayout as overall layout and inserts title in the top (NORTH) cell | ||
|
||
setLayout(new BorderLayout()); | ||
JLabel title = new JLabel("Welcome to Literature Mad-Libs"); | ||
title.setHorizontalAlignment(SwingConstants.CENTER); | ||
title.setPreferredSize(new Dimension(STANDARD_WIDTH, 50)); | ||
add(title, BorderLayout.NORTH); | ||
|
||
|
||
//Nests GridLayout into the left-middle (WEST) cell of the overall BorderLayout | ||
//This area will include radio buttons to select the literature menu | ||
JPanel litMenu = new JPanel(); | ||
//Calculates numberMenuRows for GridLayout based on how many passage options are available | ||
//plus three to compensate for the instructions and "Children's Literature" & "Classic Literature" labels | ||
int numMenuRows = (childrensMenu.size() + classicsMenu.size() + 3); | ||
litMenu.setLayout(new GridLayout(numMenuRows, 1, 3, 10)); | ||
litMenu.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8)); | ||
add(litMenu, BorderLayout.WEST); | ||
|
||
//Populates the left-middle (WEST) cell with instructions and radio buttons for each menu option | ||
JLabel instructions = new JLabel("Please select a literature passage you would like to Mad-Lib: "); | ||
litMenu.add(instructions); | ||
|
||
int i; | ||
int j; | ||
JRadioButton[] radioButton = new JRadioButton[(childrensMenu.size() + classicsMenu.size() + 1)]; | ||
ButtonGroup group = new ButtonGroup(); | ||
|
||
|
||
//Displays the Children's Literature menu options | ||
JLabel childrenLabel = new JLabel("Children's Literature:"); | ||
litMenu.add(childrenLabel); | ||
|
||
|
||
for (i = 1; i <= childrensMenu.size(); i++){ | ||
radioButton[i] = new JRadioButton(childrensMenu.get(i-1).getLitTitle() + " by" + childrensMenu.get(i-1).getLitAuthor()); | ||
litMenu.add(radioButton[i]); | ||
group.add(radioButton[i]); | ||
} | ||
|
||
|
||
|
||
//Displays the Classic Literature menu to console | ||
JLabel classicLabel = new JLabel("Classic Literature:"); | ||
litMenu.add(classicLabel); | ||
|
||
|
||
for (j = childrensMenu.size() + 1; j <= (childrensMenu.size() + classicsMenu.size()); j++) { | ||
radioButton[j] = new JRadioButton(classicsMenu.get(j - childrensMenu.size() - 1).getLitTitle() + " by" + classicsMenu.get(j - childrensMenu.size() - 1).getLitAuthor()); | ||
litMenu.add(radioButton[j]); | ||
group.add(radioButton[j]);; | ||
} | ||
|
||
|
||
|
||
//Places scrolling text area into the right-middle (EAST) cell of the overall BorderLayout | ||
text = new JTextArea(); | ||
text.setEditable(false); | ||
text.setMargin(new Insets(4, 4, 4, 4)); | ||
add(new JScrollPane(text), BorderLayout.CENTER); | ||
post("Eventually the original text of each passage will be displayed here when the player clicks on the menu selection."); | ||
|
||
//Places a "PLAY MAD-LIBS button into the bottom cell of the overall BorderLayout | ||
JButton playButton = new JButton(); | ||
|
||
//Nests another BorderLayout within the button in order to display a 2-line button | ||
playButton.setLayout(new BorderLayout()); | ||
JLabel label1 = new JLabel("PLAY MAD-LIBS"); | ||
JLabel label2 = new JLabel("with selected passage"); | ||
label1.setHorizontalAlignment(SwingConstants.CENTER); | ||
label2.setHorizontalAlignment(SwingConstants.CENTER); | ||
playButton.add(BorderLayout.NORTH,label1); | ||
playButton.add(BorderLayout.SOUTH,label2); | ||
|
||
playButton.addActionListener(this); | ||
add(playButton, BorderLayout.SOUTH); | ||
|
||
|
||
|
||
} | ||
|
||
// end of constructor | ||
|
||
|
||
private void post(String message) { // add a message and line feed to the text | ||
text.append(message + "\n\n"); | ||
} | ||
|
||
/** | ||
* Respond to an ActionEvent from one of the GUI components in the panel. | ||
* In each case, a message about the event is posted to the text area. | ||
* This method is part of the ActionListener interface. | ||
* | ||
* Design Progress Note: This section needs to be expanded to display the text when each | ||
* radio button is pressed. | ||
*/ | ||
public void actionPerformed(ActionEvent evt) { | ||
Object target = evt.getSource(); // component that produced this event | ||
if (target instanceof JButton) { | ||
// Initiates program for that passage | ||
post("Eventually the Mad-Lib game will run when you push this button"); | ||
} | ||
else if (target instanceof JRadioButton) { | ||
if (((JRadioButton) target).isSelected()) | ||
post("Checkbox was turned on."); | ||
else | ||
post("Checkbox was turned off."); | ||
} | ||
} | ||
|
||
|
||
} | ||
|
||
|