Java Program Write a program that uses multiple layout managers, buttons, labels
ID: 3581750 • Letter: J
Question
Java Program
Write a program that uses multiple layout managers, buttons, labels, and text areas. It should have an appropriate title on the title bar.
You will have 5 buttons, one for each vowel and when the button is pressed it will display the text of the button in the area below. ONLY 1 ACTION LISTENER IS ALLOWED in the program.
It should look similar to the program below:
Make sure all functionality is there.
Have it spaced and lined up as shown above including margins.
Program Checklist
-Has 5 buttons, each labeled with a vowel
-Has a text field that displays letter of button pressed.
-Has one action listener that can handle all 5 buttons.
-It is laid out as shown in the sample, has margins around the fields.
You can look at the sample tic tac toe program below to help you with this one
/**************************************************************
*
*
* This program implements the game of tic-tac-toe.
* When the first blank button is clicked, its label changes
* to an X. Subsequent clicked blank buttons change their labels
* to O and X in alternating sequence.
**************************************************************/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TicTacToe extends JFrame
{
private boolean xTurn = true; // keeps track of whether
// it's X's turn or O's turn
//***********************************************************
public TicTacToe()
{
setTitle("Tic-Tac-Toe");
setSize(200, 220);
setDefaultCloseOperation(EXIT_ON_CLOSE);
createContents();
setVisible(true);
} // end TicTacToe constructor
//***********************************************************
// Create components and add to window.
private void createContents()
{
JButton button; // re-instantiate this button and use
// to fill entire board
setLayout(new GridLayout(3, 0));
for (int i=0; i<3; i++)
{
for (int j=0; j<3; j++)
{
button = new JButton();
button.addActionListener(new Listener());
add(button);
} // end for j
} // end for i
} // end createContents
//***********************************************************
// If user clicks a button, change its label to "X" or "O".
private class Listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JButton btn = (JButton) e.getSource();
if (btn.getText().isEmpty())
{
btn.setText(xTurn ? "X" : "O");
xTurn = !xTurn;
}
} // end actionPerformed
} // end class Listener
//***********************************************************
} // end class TicTacToe
public static void main(String[] args)
{
new TicTacToe();
}
Please send me a fully working program. Kinda on a time crunch here. The sample program/code is just something that may help. The Tic Tac Toe program has one action listener as is required of this program
E Buy a vowel You bought:Explanation / Answer
If you get error uses or overrides a deprecated API.
Please run using command
javac -d Vowel.java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Vowel extends JFrame implements ActionListener{
//declaring buttons
JButton b1,b2,b3,b4,b5;
//declaring textField
JTextField vowel;
//declaring Label
JLabel labe;
//constructor of class
Vowel(){
//giving proper title to J frame
super("Buy a Vowel");
//creating
b1=new JButton("a");
b2=new JButton("e");
b3=new JButton("i");
b4=new JButton("0");
b5=new JButton("u");
vowel=new JTextField("");
labe=new JLabel("labe");
//setting position
b1.setBounds(10,30,50,40);
b2.setBounds(60,30,50,40);
b3.setBounds(110,30,50,40);
b4.setBounds(10,70,50,40);
b5.setBounds(60,70,50,40);
labe.setBounds(70,120,100,40);
vowel.setBounds(70,150,90,20);
//setting text of label and clearing border
labe.setText("you bought:");
labe.setBorder(null);
//adding in frame
add(b1);add(b2);add(b3);add(b4);add(b5);add(labe);add(vowel);
//applying action listner
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
//setting text color inside textfield
vowel.setForeground(Color.green);
setSize(250,300);
setLayout(null);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}//end of constuctor
//action listner handling 5 buttons
public void actionPerformed(ActionEvent e){
//check which button is pressed and set textField value
if(e.getSource()==b1){
String s=b1.getLabel();
vowel.setText(s);
}//end of if
if(e.getSource()==b3){
String s=b3.getLabel();
vowel.setText(s);
}//end of if
if(e.getSource()==b2){
String s=b2.getLabel();
vowel.setText(s);
}//end of if
if(e.getSource()==b4){
String s=b4.getLabel();
vowel.setText(s);
}//end of if
if(e.getSource()==b5){
String s=b5.getLabel();
vowel.setText(s);
}//end of if
}//end of actionPerformed
public static void main(String[] args){
//calling constrructor of class
new Vowel();
}//end of main
}//end of class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.