Java How to Program Early Objects 10th edition. Part 1 Code the GUI example foun
ID: 3827094 • Letter: J
Question
Java How to Program Early Objects 10th edition. Part 1 Code the GUI example found on pages 496 and 497. You will need two files (ButtonFrame.java, ButonTest.java). The file for ButtonFrame will also contain the ButtonHandler class. Part 2 Create a class called HighLowGame to play the high low game. The class should have the following methods: HighLowGame(int numberOfNumbers), String guess(int numberGuessed) should return “Correct after X guesses” or “Wrong, Guess Higher”, or “Wrong, Guess Lower”. Part 3 In create a GUI app that uses this class to play the game. Use a JTextField to collect the user guess. Code for GUI example follows: import java.awt.FlowLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JOptionPane; public class ButtonFrame extends JFrame{ private final JButton plainJButton; // button with just text private final JButton fancyJButton; // button with icons //ButtonFrame adds JButtons to JFrame public ButtonFrame(){ super("Testing Buttons"); setLayout(new FlowLayout()); plainJButton = new JButton("Plain Button");//button with text add(plainJButton);//add plainJButton to JFrame Icon bug1 = new ImageIcon(getClass().getResource("bug1.gif")); Icon bug2 = new ImageIcon(getClass().getResource("bug2.gif")); fancyJButton = new JButton("Fancy Button", bug1);//set image fancyJButton.setRolloverIcon(bug2);//set rollover image add(fancyJButton); //addfancyJButton to JFrame //create new ButtonHandler for button event handling ButtonHandler handler = new ButtonHandler(); fancyJButton.addActionListener(handler); plainJButton.addActionListener(handler); } //inner class for button event handling private class ButtonHandler implements ActionListener{ //handle button event @Override public void actionPerformed(ActionEvent event) { JOptionPane.showMessageDialog(ButtonFrame.this, String.format("You pressed: %s", event.getActionCommand())); } } }//end class ButtonFrame import javax.swing.JFrame; //Fig 12.16: ButtonTest.java //Testing ButtonFrame public class ButtonTest { public static void main(String[] args) { ButtonFrame buttonFrame = new ButtonFrame(); buttonFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); buttonFrame.setSize(375, 210); buttonFrame.setVisible(true); } }
//Command buttons and action events
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class ButtonFrame extends JFrame{
private final JButton plainJButton; // button with just text
private final JButton fancyJButton; // button with icons
//ButtonFrame adds JButtons to JFrame
public ButtonFrame(){
super("Testing Buttons");
setLayout(new FlowLayout());
plainJButton = new JButton("Plain Button");//button with text
add(plainJButton);//add plainJButton to JFrame
Icon bug1 = new ImageIcon(getClass().getResource("bug1.gif"));
Icon bug2 = new ImageIcon(getClass().getResource("bug2.gif"));
fancyJButton = new JButton("Fancy Button", bug1);//set image
fancyJButton.setRolloverIcon(bug2);//set rollOver image
add(fancyJButton); //addfancyJButton to JFrame
//create new ButtonHandler for button event handling
ButtonHandler handler = new ButtonHandler();
fancyJButton.addActionListener(handler);
plainJButton.addActionListener(handler);
}
//inner class for button event handling
private class ButtonHandler implements ActionListener{
//handle button event
@Override
public void actionPerformed(ActionEvent event)
{
JOptionPane.showMessageDialog(ButtonFrame.this, String.format("You pressed: %s", event.getActionCommand()));
}
}
}//end class ButtonFrame
Explanation / Answer
Number-guessing game in Java with Swing UI
java game swing number-guessing-game
I am trying not to pick up any bad habits while improving the way I write programs. Any suggestions on the following lines of code?
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.