-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdvanGUI.java
67 lines (59 loc) · 2.02 KB
/
AdvanGUI.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javavideos;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class AdvanGUI {
public static void main(String args[])
{
//creates a new JFrame from the myjframe class
MyJFrame newFrame = new MyJFrame();
//sets the frame to be visible
newFrame.setVisible(true);
}
}
//this is a jFrame class the extends the JFrame class and implements the Action
//Listener
class MyJFrame extends JFrame implements ActionListener
{
//creates a question display
JLabel question = new JLabel("What is your name?");
//creates a text field to accept an answer
JTextField answer = new JTextField(15);
//creates a button
JButton clickMe = new JButton("Click Here");
//create an empty label that will output the users choice
JLabel output = new JLabel("");
public MyJFrame(){
super("Hello");
//sets the size to 300 x 200
setSize(300,200);
//sets the layout
setLayout(new FlowLayout());
//these following lines add the question, answer, button and output
//components to the JFrame
add(question);
add(answer);
add(clickMe);
add(output);
//sets the program to close when the frame is closed
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//makes the button an action listener
clickMe.addActionListener(this);
}
//this is the action performed method that will override and perform the action
@Override
public void actionPerformed(ActionEvent a){
//creates a string get the answer
String name = answer.getText();
//makes a greeting with the string
String greeting = "Hello " + name;
//displays the output to the user
output.setText(greeting);
}
}