import javax.swing.*;
import java.awt.event.*;
import java.util.Scanner;
@SuppressWarnings("unused")
public class Fun {
@SuppressWarnings({"static-access", "resource"})
public static void main(String[] args) {
JFrame f = new JFrame("The Gamer Zone");
//set size and location of frame
f.setSize(390, 300);
f.setLocation(100, 150);
//make sure it quits when x is clicked
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//set look and feel
f.setDefaultLookAndFeelDecorated(true);
JLabel labelM = new JLabel("Are you an epic gamer? (true,false) ");
labelM.setBounds(50, 50, 250, 30);
JTextField Text = new JTextField();
//set size of the text box
Text.setBounds(50, 100, 200, 30);
//add elements to the frame
f.add(labelM);
f.add(Text);
f.setLayout(null);
f.setVisible(true);
String answer1 = Text.getText();
Scanner sc = new Scanner(answer1);
int x = 0;
while (x < 1) {
try {
if (sc.next() == "true") {
f.remove(labelM);
x++;
}
} catch (Exception e) {
System.out.println("Something went wrong");
}
}
}
}
I am trying to get it to remove LabelM
when the input in the jtext field is "true." I am just trying to learn how JFrame
s work, and this is my first time working with one. Is there a better way to scan from a JFrame
?