GUI COMPONENT TASKS I NEED HELP! 1. Create one application GreetingsFlow.java that includes: frame Flow Layout 4
Question:
GUI COMPONENT TASKS I NEED HELP!
1. Create one applicationGreetingsFlow.java that includes:
frame
Flow Layout
4 buttons with the names of 4 languages
4 labels with a text - greetings in these languages
One text field with size of 45 characters to enter a text
2. Change the previous task and instead of FlowLayout use the setBounds to place all components in appropriate places on the frame. Change thetext field to atext area with about 3 lines of 30 characters to enter a text. Manage the size of the text area so 3 lines of 30 characters can fit in.
Name this application as Greetings2.java
READ THE EXAMPLES FOR ASSISTANCE!
Example GUI1
import java.awt.*; import javax.swing.*;
public class GUI1 extends JFrame { private JButton play, stop, time; private JLabel label1; private JTextArea textArea1; public GUI1() { init(); } public void init() { //creating a frame window setTitle("Buttons Example # 1");//title on this frame setSize(400, 300);//frame size setVisible(true); setResizable(true); //adding the GUI components to ContentPane inside the frame window getContentPane().setLayout(null);//eliminate default layout play = new JButton("Play");//create the button Play //set coordinates of the button's left upper corner, then width and height play.setBounds(60, 20, 70, 30); play.setBackground(Color.RED); //set the background red play.setForeground(Color.WHITE);//set the text colour
getContentPane().add(play);//add the button to the container
stop = new JButton("Stop"); stop.setBounds(160, 20, 70, 30); stop.setBackground(Color.CYAN); stop.setForeground(Color.BLACK); getContentPane().add(stop);
time = new JButton("Time"); time.setBounds(260, 20, 70, 30); time.setBackground(Color.GREEN); time.setForeground(Color.RED); add(time);
label1 = new JLabel("Welcome to Game 1");// create the label to display a teat label1.setBounds(135, 60, 118, 30); label1.setBackground(Color.pink); getContentPane().add(label1); textArea1 = new JTextArea();//Has a rectangular area to enter a text textArea1.setBounds(80, 120, 220, 130); getContentPane().add(textArea1); } public static void main(String[] args) { new GUI1(); } }
|
Example GUI2
import java.awt.*; import javax.swing.*; public class GUI2 extends JFrame {
private JButton play, stop, time; private JLabel label1; private JTextField textField1;
public GUI2() { init(); }
public void init() { setTitle("Buttons Example # 2"); setSize(500, 150); setVisible(true); setResizable(true);//make frame resizable FlowLayout flo = new FlowLayout(); setLayout(flo);//set flowLayout
play = new JButton("Play"); editButton(play, Color.RED, Color.WHITE); getContentPane().add(play); stop = new JButton("Stop"); editButton(stop, Color.PINK, Color.ORANGE); getContentPane().add(stop); time = new JButton("Time"); editButton(time, Color.CYAN, Color.GREEN); getContentPane().add(time); label1 = new JLabel("Welcome to Game # 2"); getContentPane().add(label1); textField1 = new JTextField(30);//Has a textField line with max 30 characters to enter getContentPane().add(textField1); }
public void editButton(JButton button, Color c1, Color c2) { button.setBackground(c1); button.setForeground(c2); }
public static void main(String[] args) { new GUI2(); } }
|
Java How To Program Late Objects Version
ISBN: 9780136123712
8th Edition
Authors: Paul Deitel, Deitel & Associates