I need to make my login button go to the next Class(page) of the system
atm i have add a few buttons i saw in videos and read about and really will like to make this work.
so I played around with a few commands but dont think i got it right.
Can you maybe tell me what I am doing extremely wrong?
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JToolBar;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JSeparator;
import javax.swing.JTable;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Scanner;
public class system {
protected static final int Admin = 0;
private JFrame frame;
private JTextField Usernameinput;
private JTextField Passwordinput;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
Scanner input = new Scanner(System.in);
public void run() {
try {
system window = new system();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public system() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 534, 365);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnLogin = new JButton("Login");
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String username;
String password;
System.out.println("Log in:");
System.out.println("username: ");
Scanner input = null;
username = input.next();
System.out.println("password: ");
password = input.next();
//users check = new users(username, password);
if (Usernameinput.equals(Admin) && Passwordinput.equals(Admin)) {
System.out.println("Welcome");
};
}
});
btnLogin.setBounds(29, 242, 89, 23);
frame.getContentPane().add(btnLogin);
JButton btnReset = new JButton("Reset");
btnReset.setBounds(144, 242, 89, 23);
frame.getContentPane().add(btnReset);
JButton btnExit = new JButton("Exit");
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
btnExit.setBounds(397, 282, 89, 23);
frame.getContentPane().add(btnExit);
Usernameinput = new JTextField();
Usernameinput.setBounds(144, 74, 209, 20);
frame.getContentPane().add(Usernameinput);
Usernameinput.setColumns(10);
Passwordinput = new JTextField();
Passwordinput.setColumns(10);
Passwordinput.setBounds(144, 106, 209, 20);
frame.getContentPane().add(Passwordinput);
JLabel lblUsername = new JLabel("Username:");
lblUsername.setBounds(73, 77, 55, 14);
frame.getContentPane().add(lblUsername);
JLabel lblPassword = new JLabel("Password:");
lblPassword.setBounds(73, 108, 55, 17);
frame.getContentPane().add(lblPassword);
JSeparator separator = new JSeparator();
separator.setBounds(10, 211, 498, 2);
frame.getContentPane().add(separator);
JSeparator separator_1 = new JSeparator();
separator_1.setBounds(10, 57, 498, 2);
frame.getContentPane().add(separator_1);
JLabel lblNewLabel = new JLabel("Welcome to The System v 0.1");
lblNewLabel.setBounds(182, 5, 151, 41);
frame.getContentPane().add(lblNewLabel);
}
}
There's a mistake in your btnLogin.addActionListener. You don't need the input here. Usually we need Scanner to read the input from the console.
But now as we have a UI we can get the user input from the specific JTextFields. In your case username from Usernameinput and password from Passwordinput.
username = Usernameinput.getText();
password = Passwordinput.getText();
Next the if statement in your btnLogin.addActionListener must check the text inside the JTextFields. As we have already obtained those and kept in the variables username and password you can use those.
And you can't use String.equals method with an int. username.equals(Admin) will always return false since the variable Admin is an int. So change,
if (username.equals("username") && password.equals("password")) {
System.out.println("Welcome");
};
Finally you can remove unused import statements (optional).
import javax.swing.JToolBar;
import javax.swing.JTable;
Hi Thank you for the help
But like now if i press Login it goes to a consol page where is says welcome but will like it to go to the next class??
never mind found out how to add it
now i just need to make use of the second frame
Related
This question already has answers here:
syntax error: insert } to complete ClassBody
(11 answers)
Closed 6 years ago.
I'm having trouble with the error "Syntax error, insert "}" to complete ClassBody"
and i don't know what to do.
This is my code:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
public class Password {
private JFrame frame;
private JTextField PassField;
private JTextField txtSecretPasswords;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Password window = new Password();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Password() {
initialize();
}/* Here*/
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 700, 650);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
PassField = new JTextField();
PassField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
PassField.setBounds(0, 42, 150, 32);
frame.getContentPane().add(PassField);
PassField.setColumns(10);
txtSecretPasswords = new JTextField();
txtSecretPasswords.setEditable(false);
txtSecretPasswords.setText("Secret passwords");
txtSecretPasswords.setBounds(0, 11, 131, 20);
frame.getContentPane().add(txtSecretPasswords);
txtSecretPasswords.setColumns(10);
JButton btnEnter = new JButton("Enter");
btnEnter.addActionListener(new ActionListener() {
private JTextField txtMessage;
private JTextField txtMessage2;
private JTextField txtMessage3;
private JTextField txtPass2;
private JButton btnEnter2;
public void actionPerformed(ActionEvent e) {
int pass;
pass = Integer.parseInt(PassField.getText());
if (pass == 3333) {
txtMessage = new JTextField();
txtMessage.setEditable(false);
txtMessage.setText("Postal Code: 3333");
txtMessage.setBounds(0, 100, 130, 20);
frame.getContentPane().add(txtMessage);
txtMessage.setColumns(10);
txtMessage2 = new JTextField();
txtMessage2.setEditable(false);
txtMessage2 .setText("Email password: 3333");
txtMessage2.setBounds(0, 125, 150, 20);
frame.getContentPane().add(txtMessage2);
txtMessage2.setColumns(10);
txtMessage3 = new JTextField();
txtMessage3.setEditable(false);
txtMessage3.setText("Steam password: 3333");
txtMessage3.setBounds(0, 170, 200, 20);
frame.getContentPane().add(txtMessage3);
txtMessage3.setColumns(10);
btnEnter2 = new JButton();
btnEnter2.setText("Enter");
btnEnter2.setBounds(175, 250, 100, 15);
frame.getContentPane().add(btnEnter2);
btnEnter2.addActionListener(new ActionListener() {
public void actionPreformed(ActionEvent e) {
int pass2;
pass2 = Integer.parseInt(txtPass2.getText());
if (pass2 == 030303) {
txtMessage3 = new JTextField();
txtMessage3.setEditable(false);
txtMessage3.setText("Steam password: 3333");
txtMessage3.setBounds(0, 170, 200, 20);
frame.getContentPane().add(txtMessage3);
txtMessage3.setColumns(10);
}
else {
JOptionPane.showMessageDialog(null, "Wrong");
}
}/* And here*/);
txtPass2 = new JTextField();
txtPass2.setEditable(true);
txtPass2.setBounds(0, 250, 150, 20);
frame.getContentPane().add(txtPass2);
txtPass2.setColumns(10);
}
else {
JOptionPane.showMessageDialog(null, "Wrong");
}
}
});
btnEnter.setBounds(160, 47, 99, 23);
frame.getContentPane().add(btnEnter);
}
I know its a long one. I've tried to look through the code but I'm not that experienced with Java or coding.
I don't know why the errors there or what to do so if you could help it would be appreciated a lot by a beginner!
I suggest using a good editor or IDE. Any decent editor should allow to "go to matching parenthesis". For example, if you use vi, position the cursor on a brace and type "%" to go to the matching one. You can use this to verify that parenthesis are matching as you expect.
IDEs such as Eclipse or IntelliJ allow you to reformat code, which will also help you spot where you're missing a closing brace. They also offer other invaluable features when programming in Java, so I strongly suggest spending the time to install and learn one.
This said, you're missing a close brace right before the "And here" comment.
You should use an IDE in order to be able to indent your code and see any missing curly braces.
If you use auto-indent from your text editor, you notice that the last '}' is still indented by one tab.
It means there are more { than }.
With a good IDE (e.g. Eclipse) it's easier to find out where it should be added (Once at the end, once before the last /* And here */ comment). Note that the first error shown by Eclipse isn't the right location in this case.
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
public class Password
{
private JFrame frame;
private JTextField PassField;
private JTextField txtSecretPasswords;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Password window = new Password();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Password() {
initialize();
}/* Here */
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 700, 650);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
PassField = new JTextField();
PassField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
PassField.setBounds(0, 42, 150, 32);
frame.getContentPane().add(PassField);
PassField.setColumns(10);
txtSecretPasswords = new JTextField();
txtSecretPasswords.setEditable(false);
txtSecretPasswords.setText("Secret passwords");
txtSecretPasswords.setBounds(0, 11, 131, 20);
frame.getContentPane().add(txtSecretPasswords);
txtSecretPasswords.setColumns(10);
JButton btnEnter = new JButton("Enter");
btnEnter.addActionListener(new ActionListener() {
private JTextField txtMessage;
private JTextField txtMessage2;
private JTextField txtMessage3;
private JTextField txtPass2;
private JButton btnEnter2;
public void actionPerformed(ActionEvent e) {
int pass;
pass = Integer.parseInt(PassField.getText());
if (pass == 5441) {
txtMessage = new JTextField();
txtMessage.setEditable(false);
txtMessage.setText("Postal Code: 42658");
txtMessage.setBounds(0, 100, 130, 20);
frame.getContentPane().add(txtMessage);
txtMessage.setColumns(10);
txtMessage2 = new JTextField();
txtMessage2.setEditable(false);
txtMessage2.setText("Email password: Vauxhal1");
txtMessage2.setBounds(0, 125, 150, 20);
frame.getContentPane().add(txtMessage2);
txtMessage2.setColumns(10);
txtMessage3 = new JTextField();
txtMessage3.setEditable(false);
txtMessage3.setText("Steam password: Vauxhal12");
txtMessage3.setBounds(0, 170, 200, 20);
frame.getContentPane().add(txtMessage3);
txtMessage3.setColumns(10);
btnEnter2 = new JButton();
btnEnter2.setText("Enter");
btnEnter2.setBounds(175, 250, 100, 15);
frame.getContentPane().add(btnEnter2);
btnEnter2.addActionListener(new ActionListener() {
public void actionPreformed(ActionEvent e) {
int pass2;
pass2 = Integer.parseInt(txtPass2.getText());
if (pass2 == 030303) {
txtMessage3 = new JTextField();
txtMessage3.setEditable(false);
txtMessage3.setText("Steam password: Vauxhal12");
txtMessage3.setBounds(0, 170, 200, 20);
frame.getContentPane().add(txtMessage3);
txtMessage3.setColumns(10);
} else {
JOptionPane.showMessageDialog(null, "Wrong");
}
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}/* And here */);
txtPass2 = new JTextField();
txtPass2.setEditable(true);
txtPass2.setBounds(0, 250, 150, 20);
frame.getContentPane().add(txtPass2);
txtPass2.setColumns(10);
} else {
JOptionPane.showMessageDialog(null, "Wrong");
}
}
});
btnEnter.setBounds(160, 47, 99, 23);
frame.getContentPane().add(btnEnter);
}
}
PS: If you just want to use a basic text editor, Java is the wrong language. Either use Java + full IDE (Netbeans or Eclipse), or Ruby/Python with pluma/notepad2/vim/...
The problem, as many others have suggested here, is in the balancing of the braces, meaning each { opens a block of code and thus needs a matching } to close that block of code.
IDEs do make it easier to do this matching for you, many free text editors are available to do this matching for you.
So a few changes to your code:
This line }/* And here*/); delete /* And here*/ and replace it with }.
Next, the main class public class Password needs to be closed on the last line with a }.
Lastly, since the btnEnter2 needs to override the addActionListener abstract method, you have a typo in line public void actionPreformed(ActionEvent e) {, actionPreformed should be spelled actionPerformed to successfully override the addActionListener anonymous method.
The final code should resemble:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
public class Password {
private JFrame frame;
private JTextField PassField;
private JTextField txtSecretPasswords;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Password window = new Password();
window.frame.setVisible(true);
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Password() {
initialize();
}/* Here*/
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 700, 650);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
PassField = new JTextField();
PassField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
PassField.setBounds(0, 42, 150, 32);
frame.getContentPane().add(PassField);
PassField.setColumns(10);
txtSecretPasswords = new JTextField();
txtSecretPasswords.setEditable(false);
txtSecretPasswords.setText("Secret passwords");
txtSecretPasswords.setBounds(0, 11, 131, 20);
frame.getContentPane().add(txtSecretPasswords);
txtSecretPasswords.setColumns(10);
JButton btnEnter = new JButton("Enter");
btnEnter.addActionListener(new ActionListener() {
private JTextField txtMessage;
private JTextField txtMessage2;
private JTextField txtMessage3;
private JTextField txtPass2;
private JButton btnEnter2;
public void actionPerformed(ActionEvent e) {
int pass;
pass = Integer.parseInt(PassField.getText());
if (pass == 5441) {
txtMessage = new JTextField();
txtMessage.setEditable(false);
txtMessage.setText("Postal Code: 42658");
txtMessage.setBounds(0, 100, 130, 20);
frame.getContentPane().add(txtMessage);
txtMessage.setColumns(10);
txtMessage2 = new JTextField();
txtMessage2.setEditable(false);
txtMessage2 .setText("Email password: Vauxhal1");
txtMessage2.setBounds(0, 125, 150, 20);
frame.getContentPane().add(txtMessage2);
txtMessage2.setColumns(10);
txtMessage3 = new JTextField();
txtMessage3.setEditable(false);
txtMessage3.setText("Steam password: Vauxhal12");
txtMessage3.setBounds(0, 170, 200, 20);
frame.getContentPane().add(txtMessage3);
txtMessage3.setColumns(10);
btnEnter2 = new JButton();
btnEnter2.setText("Enter");
btnEnter2.setBounds(175, 250, 100, 15);
frame.getContentPane().add(btnEnter2);
btnEnter2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int pass2;
pass2 = Integer.parseInt(txtPass2.getText());
if (pass2 == 030303) {
txtMessage3 = new JTextField();
txtMessage3.setEditable(false);
txtMessage3.setText("Steam password: Vauxhal12");
txtMessage3.setBounds(0, 170, 200, 20);
frame.getContentPane().add(txtMessage3);
txtMessage3.setColumns(10);
}
else {
JOptionPane.showMessageDialog(null, "Wrong");
}
}
});
txtPass2 = new JTextField();
txtPass2.setEditable(true);
txtPass2.setBounds(0, 250, 150, 20);
frame.getContentPane().add(txtPass2);
txtPass2.setColumns(10);
}
else {
JOptionPane.showMessageDialog(null, "Wrong");
}
}
});
btnEnter.setBounds(160, 47, 99, 23);
frame.getContentPane().add(btnEnter);
}
}
Hope this helps.
Insert } at the start of line 122 and at the end of code to complete classBody.
But you should really consider Roberto Attia's suggestion.
Hello I am working on this project to store student names and record their grades. I have the entire program mapped out and all I need to do now is record the integers and strings from the user input and call it from different classes. I am having trouble with this. Do I use an array? How do I call from another class? Thank you.
package gradebook;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.Scanner;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
public class Student extends JFrame {
private JFrame studentFrame;
private JPanel contentPane;
private JTextField studentNameTextField;
protected Component frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Student frame = new Student();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Student() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel gradebookLabel = new JLabel("Gradebook");
gradebookLabel.setBounds(189, 6, 68, 16);
contentPane.add(gradebookLabel);
JLabel lblPleaseEnterThe = new JLabel("Please enter the student name");
lblPleaseEnterThe.setBounds(130, 105, 194, 16);
contentPane.add(lblPleaseEnterThe);
studentNameTextField = new JTextField("Enter here");
String stdname = studentNameTextField.getText();
studentNameTextField.setBounds(130, 133, 194, 26);
contentPane.add(studentNameTextField);
studentNameTextField.setColumns(10);
JButton continueButton = new JButton("Continue");
continueButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent a) {
if(continueButton.isEnabled()){
Student.this.dispose();
Student studentScreen = new Student();
studentScreen.dispose();
AddGrades addGradesScreen = new AddGrades();
addGradesScreen.setVisible(true);
}
}
});
continueButton.setBounds(327, 243, 117, 29);
contentPane.add(continueButton);
JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a) {
if(cancelButton.isEnabled()){
MainScreen mainScreenScreen = new MainScreen();
mainScreenScreen.setVisible(true);
contentPane.setVisible(false);
contentPane.disable();
}
}
});
cancelButton.setBounds(207, 243, 117, 29);
contentPane.add(cancelButton);
}
private void initizalize() {
// TODO Auto-generated method stub
}
}
Is it something along the lines of this?
studentNameTextField = new JTextField("Enter here");
String stdname = studentNameTextField.getText();
I know this would be storing it but how do I call that in a different class so I can make it appear on a different Frame?
Update: Okay so I've done this
Student frame = new Student();
String stdname = frame.studentNameTextField.getText();
JLabel addgradesLabel = new JLabel("Add grades for" + frame.studentNameTextField);
addgradesLabel.setBounds(139, 34, 167, 29);
contentPane.add(addgradesLabel);
And it's still not working. I believe I'm not implementing this correctly. I'm trying to title the label with what the user inputs for the name. So it would but "Add grades for" + stdname But it's not calling it correctly. How can I fix this?
Here is my code from the AddStudentName class
studentNameTextField = new JTextField();
studentNameTextField.setBounds(130, 133, 194, 26);
contentPane.add(studentNameTextField);
studentNameTextField.setColumns(10);
JButton continueButton = new JButton("Continue");
continueButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent a) {
if(continueButton.isEnabled()){
Student.this.dispose();
Student studentScreen = new Student();
studentScreen.dispose();
AddGrades addGradesScreen = new AddGrades();
addGradesScreen.setVisible(true);
}
}
});
What should I add here to save the input that the user is inputting into the JTextField? Thanks for the help
Here's the full code for the classes:
package gradebook;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.Scanner;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
public class Student extends JFrame {
private JFrame studentFrame;
private JPanel contentPane;
public JTextField studentNameTextField;
protected Component frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Student frame = new Student();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Student() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel gradebookLabel = new JLabel("Gradebook");
gradebookLabel.setBounds(189, 6, 68, 16);
contentPane.add(gradebookLabel);
JLabel lblPleaseEnterThe = new JLabel("Please enter the student name");
lblPleaseEnterThe.setBounds(130, 105, 194, 16);
contentPane.add(lblPleaseEnterThe);
JTextField studentNameTextField = new JTextField();
studentNameTextField.setBounds(130, 133, 194, 26);
contentPane.add(studentNameTextField);
studentNameTextField.setColumns(10);
JButton continueButton = new JButton("Continue");
continueButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent a) {
if(continueButton.isEnabled()){
Student studentScreen = new Student();
studentScreen.dispose();
AddGrades addGradesScreen = new AddGrades();
addGradesScreen.setVisible(true);
}
}
});
continueButton.setBounds(327, 243, 117, 29);
contentPane.add(continueButton);
JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a) {
if(cancelButton.isEnabled()){
MainScreen mainScreenScreen = new MainScreen();
mainScreenScreen.setVisible(true);
contentPane.setVisible(false);
contentPane.disable();
}
}
});
cancelButton.setBounds(207, 243, 117, 29);
contentPane.add(cancelButton);
}
private void initizalize() {
// TODO Auto-generated method stub
}
}
and:
package gradebook;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class AddGrades extends JFrame {
private JPanel contentPane;
private JTextField Assignment1;
private JTextField Assignment2;
private JTextField Assignment3;
private JTextField Assignment4;
private JLabel testsLabel;
private JTextField Test1;
private JTextField Test2;
public JTextField Test3;
public JTextField Test4;
/**
* Launch the application.
*/
public JFrame frame;
public JButton continueButton;
public JButton exitButton;
public static void main(String[] args) {
AddGrades frame = new AddGrades();
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AddGrades frame = new AddGrades();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public AddGrades() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel gradebookLabel = new JLabel("Gradebook");
gradebookLabel.setBounds(189, 6, 68, 16);
contentPane.add(gradebookLabel);
JLabel addgradesLabel = new JLabel("Add grades for" + stdname);
addgradesLabel.setBounds(139, 34, 167, 29);
contentPane.add(addgradesLabel);
JLabel assignmentsLabel = new JLabel("Assignments:");
assignmentsLabel.setBounds(19, 75, 86, 16);
contentPane.add(assignmentsLabel);
Assignment1 = new JTextField();
Assignment1.setBounds(54, 91, 130, 26);
contentPane.add(Assignment1);
Assignment1.setColumns(10);
Assignment2 = new JTextField();
Assignment2.setBounds(54, 129, 130, 26);
contentPane.add(Assignment2);
Assignment2.setColumns(10);
Assignment3 = new JTextField();
Assignment3.setBounds(54, 167, 130, 26);
contentPane.add(Assignment3);
Assignment3.setColumns(10);
Assignment4 = new JTextField();
Assignment4.setBounds(54, 205, 130, 26);
contentPane.add(Assignment4);
Assignment4.setColumns(10);
testsLabel = new JLabel("Tests:");
testsLabel.setBounds(243, 75, 38, 16);
contentPane.add(testsLabel);
Test1 = new JTextField();
Test1.setBounds(262, 91, 130, 26);
contentPane.add(Test1);
Test1.setColumns(10);
Test2 = new JTextField();
Test2.setBounds(262, 129, 130, 26);
contentPane.add(Test2);
Test2.setColumns(10);
Test3 = new JTextField();
Test3.setBounds(262, 167, 130, 26);
contentPane.add(Test3);
Test3.setColumns(10);
Test4 = new JTextField();
Test4.setBounds(262, 205, 130, 26);
contentPane.add(Test4);
Test4.setColumns(10);
continueButton = new JButton("Continue");
continueButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
if (continueButton.isEnabled()){
MainScreen mainScreenScreen = new MainScreen();
mainScreenScreen.setVisible(true);
}
}
});
continueButton.setBounds(327, 243, 117, 29);
contentPane.add(continueButton);
exitButton = new JButton("Exit");
exitButton.setBounds(208, 243, 117, 29);
contentPane.add(exitButton);
}
}
I'm trying to take the input from the JTextField
JTextField studentNameTextField = new JTextField();
and set it on the label
JLabel addgradesLabel = new JLabel("Add grades for" + stdname);
Where stdname would be the user input from the JTextField.
Now I'm getting errors from
public static void main(String[] args) {
AddGrades frame = new AddGrades();
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AddGrades frame = new AddGrades();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
AddGrades frame = new AddGrades(); gives me error Add argument to match AddGrades(String)
The constructor AddGrades() is undefined
Edit: The best way to do this is to pass an argument to your newly created AddGrades class. See below:
public AddGrades(String stdname) {
JLabel addgradesLabel = new JLabel("Add grades for " + stdname);
}
You will need to modify your code to pass a string to your AddGrades class wherever you create it:
JButton continueButton = new JButton("Continue");
continueButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent a) {
if(continueButton.isEnabled()){
Student studentScreen = new Student();
studentScreen.dispose();
AddGrades addGradesScreen = new AddGrades(studentNameTextField.getText());
addGradesScreen.setVisible(true);
}
}
});
That worked for me - I downloaded your code and tested it.
JSpinner or JFormattedTextField would be my first thought, but if you can't do that then you could use Integer.parseInt to parse a String to int and a NumberFormat to format the numbers to Strings
Maybe have a look at How to Use Spinners and How to Use Formatted Text Fields for starters
I know this would be storing it but how do I call that in a different class so I can make it appear on a different JFrame?
That's a open question with little context. You might use some kind of "model" which represented one or more Student values; you might use a Observer Pattern to allow the editor to generate events to interested parties which would tell them that something has changed; you might use a Model-View-Controller; you might use a modal dialog and when it's closed, ask the editor for the values via getters.
Have a look at How to Make Dialogs for more details
One of things you want to keep in mind is "responsibility" and "encapsulation". You don't want outside parties to have uncontrolled access into your editor or model, this could allow them to modify the state in an uncontrolled way leading to inconsistencies in your data or UI.
You need to decide who is actually responsible for modifying the state of the model. There's no "right" answer, for example, you could have the editor either be capable of editing existing student objects or creating new ones based on how it's configured, equally, you could also have the editor be "dumb" and simply use setters to setup the editor and getters to get the values.
There are lots of options available to you, which you would use would be based on the context in which you want to use it. My gut feeling is start with an observer pattern and a modal dialog.
I looked up earlier on Stack overflow how to use buttons with window builder in eclipse to where if you clicked it once it would do a line (or more) of code, then if you click again it would do a different line of code and so on (third,fourth,fifth click etc..)
I was wondering how you could make it so in Java, you could do this with the mouse Pressed event with the button. Here is my testing code (this code is not right at the moment that is acknowledged but I was wondering if you could use this for an example) Thank you, any help is appreciated.
This game is essentially picking from two or more given choices that lead you down a different path, and they enter it in the bottom textbox which is called Log and at the very top the text box is called console, when a user enters the answer into Log and clicks Send it prints the result of that answer into the console text box.
[updated info part]
Hello, I am trying to make a game in which the user gets a question on the top box which is called "console" and they enter the answer in the bottom box "Log" and then press send to move on to the next part. After that I want it to do the same thing, Instead of down below (my updated code), of making different buttons and making the one non-visible so they are able to click the next one, I want it to just be one button that they click and each time it processes the different lines of code i.e if the question in box console says do you want a sword or bow, and the user chooses bow and enters it into the log box and hits send i want it to say ok, how much gold do you need. User enters the amount and hits send. so on and so forth. Thank you again for any help you may give.
package Game;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.JLabel;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JTextArea;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Random;
public class Swendiver {
private JFrame frmSwendiver;
private JTextField console;
private JTextField Log;
private JTextField Weapon;
private JTextField Arrows;
private JTextField next;
private JTextField Class;
private JTextField Coin;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Swendiver window = new Swendiver();
window.frmSwendiver.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Swendiver() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmSwendiver = new JFrame();
frmSwendiver.setTitle("Swendiver 1.0");
frmSwendiver.setAutoRequestFocus(false);
frmSwendiver.setAlwaysOnTop(true);
frmSwendiver.setBounds(100, 100, 450, 300);
frmSwendiver.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmSwendiver.getContentPane().setLayout(null);
console = new JTextField();
console.setBounds(0, 0, 434, 44);
console.setEditable(false);
frmSwendiver.getContentPane().add(console);
console.setColumns(10);
Log = new JTextField();
Log.setBounds(0, 241, 434, 20);
Log.addKeyListener(new KeyAdapter() {
#Override
public void keyPressed(KeyEvent e) {
// KeyEvent.getKeyCode(VK_ENTE-R);
}
});
frmSwendiver.getContentPane().add(Log);
Log.setColumns(10);
JLabel lblThankYouFor = new JLabel("Health : 100%");
lblThankYouFor.setBackground(new Color(50, 205, 50));
lblThankYouFor.setBounds(165, 165, 100, 27);
frmSwendiver.getContentPane().add(lblThankYouFor);
console.setText("Press The enter button to start Swendiver");
JButton Send = new JButton("Enter");
Send.setBounds(345, 41, 89, 200);
Send.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
Send.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
boolean isClicked = true;
if (isClicked == true) {
isClicked = false;
lblThankYouFor.setName("Health: 75%");
console.setText("[Hermin] Welcome. Ready for your adventure? (Click Enter)");
Send.setVisible(false);
}
}
#Override
public void mousePressed(MouseEvent arg0) {
Log.setText("");
ArrayList<String> weapon = new ArrayList<String>();
console.setText("Ok, choose your weapon. <SWORD>||<BOW>");
// Log.getText().equalsIgnoreCase("SWORD")
// console.setText("Good choice.");
Log.setText("");
weapon.add("Sword");
if (weapon.get(0).equalsIgnoreCase("Sword")) {
console.setText("[Hermin] A sword? I see. Good choice...");
next.setText("This box is used for Instructions/Actions");
}
}
});
frmSwendiver.getContentPane().add(Send);
JLabel lblWeapon = new JLabel("Weapon:");
lblWeapon.setBounds(266, 87, 56, 14);
frmSwendiver.getContentPane().add(lblWeapon);
JButton button3 = new JButton("Enter");
button3.setBounds(345, 41, 89, 200);
Weapon = new JTextField();
Weapon.setBounds(249, 101, 86, 20);
Weapon.setEditable(false);
frmSwendiver.getContentPane().add(Weapon);
Weapon.setColumns(10);
JButton Enter = new JButton("Enter");
Enter.setBounds(345, 41, 89, 200);
frmSwendiver.getContentPane().add(Enter);
JLabel lblArrows = new JLabel("Arrows:");
lblArrows.setEnabled(false);
lblArrows.setBounds(165, 87, 46, 14);
frmSwendiver.getContentPane().add(lblArrows);
Arrows = new JTextField();
Arrows.setEnabled(false);
Arrows.setBounds(155, 101, 68, 20);
Arrows.setEditable(false);
frmSwendiver.getContentPane().add(Arrows);
Arrows.setColumns(10);
Random attack = new Random();
next = new JTextField();
next.setBounds(0, 41, 239, 27);
next.setEditable(false);
frmSwendiver.getContentPane().add(next);
next.setColumns(10);
boolean attacksituation = false;
JButton Attack = new JButton("Attack");
Attack.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
if(attacksituation == true) {
next.setText(Integer.toString(attack.nextInt(5)));
Attack.setEnabled(false);
Attack.setEnabled(true);
}
}
});
Attack.setBounds(0, 68, 89, 173);
frmSwendiver.getContentPane().add(Attack);
JLabel lblClass = new JLabel("Class:");
lblClass.setBounds(276, 47, 46, 14);
frmSwendiver.getContentPane().add(lblClass);
Class = new JTextField();
Class.setEditable(false);
Class.setBounds(249, 61, 86, 20);
frmSwendiver.getContentPane().add(Class);
Class.setColumns(10);
Coin = new JTextField();
Coin.setText("78");
Coin.setEditable(false);
Coin.setBounds(125, 221, 86, 20);
frmSwendiver.getContentPane().add(Coin);
Coin.setColumns(10);
next.setText("");
JLabel lblCoin = new JLabel("Coin:");
lblCoin.setBounds(99, 224, 46, 14);
frmSwendiver.getContentPane().add(lblCoin);
Enter.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
console.setText("[Hermin]Choose your class <Warrior> or <Archer>");
if (Log.getText().equalsIgnoreCase("Warrior")) {
console.setText("[Hermin]A Swordsman.. I see. Good choice.");
Weapon.setText("Sword");
next.setEnabled(true);
next.setText("Click Enter to continue");
Log.setText("");
Class.setText("Warrior");
Enter.setVisible(false);
}
if (Log.getText().equalsIgnoreCase("Archer")) {
console.setText("[Hermin]An Archer.. Interesting. Here are your Items.");
Weapon.setText("Long-Bow");
Arrows.setText("19");
next.setEnabled(true);
next.setText("Click Enter to continue");
Log.setText("");
Class.setText("Archer");
lblArrows.setEnabled(true);
Arrows.setEnabled(true);
Enter.setVisible(false);
}
if(Log.getText().equalsIgnoreCase("/secretcommand")) {
Coin.setText(Integer.toString(300));
next.setText("222 Coin Aquired");
next.setEnabled(true);
Log.setText("");
next.setText("Click Enter to continue!");
Enter.setVisible(false);
}
}
});
button3.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
console.setText("[Hermin] Great. Welcome then. Head into SheildsTown to collect some items.");
next.setText("/EquipArmor to get geared and go.");
if(Log.getText().equalsIgnoreCase("/EquipArmor")){
next.setText("You leave the armory.");
console.setText("");
Log.setText("");
}
}
});
frmSwendiver.getContentPane().add(button3);
}
}
I've started working on an extension of a project I've been working on for a few months, and I've felt the need to take it out of the console and put in in a GUI window. So far everything's going great! Except one thing, When I try to test the Login button (It's just so I can limit who uses it, and to see if I could do it.) the action listener isn't responding like I hoped it would. Here's the code:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JLayeredPane;
public class Main {
private JFrame frame;
private JTextField textField;
private JTextField textField_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main window = new Main();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Main() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.getContentPane().setForeground(Color.GREEN);
frame.setBounds(100, 100, 612, 389);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblUserName = new JLabel("User name");
lblUserName.setBackground(Color.YELLOW);
lblUserName.setBounds(158, 70, 67, 25);
frame.getContentPane().add(lblUserName);
JLabel lblPassword = new JLabel("Password");
lblPassword.setBounds(158, 146, 53, 14);
frame.getContentPane().add(lblPassword);
textField = new JTextField();
textField.setBounds(235, 143, 118, 20);
frame.getContentPane().add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setColumns(10);
textField_1.setBounds(235, 72, 118, 20);
frame.getContentPane().add(textField_1);
JButton btnLogin = new JButton("Login");
btnLogin.setBounds(151, 228, 256, 61);
frame.getContentPane().add(btnLogin);
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if(textField.equals("Admin"))
{
if(textField.equals("Admin"))
{
JLabel lblLoginSuccessPlease = new JLabel("LOGIN SUCCESS! Please wait while the other functions are loaded");
lblLoginSuccessPlease.setBounds(124, 204, 344, 14);
frame.getContentPane().add(lblLoginSuccessPlease);
}
}
}
});
JLabel lblWelcomeToMy = new JLabel("Welcome to my amazing program!");
lblWelcomeToMy.setBounds(174, 11, 242, 14);
frame.getContentPane().add(lblWelcomeToMy);
}
}
I've tried to use the && to test both the Username and the Password box, and it wouldn't work either. Also, if someone could instruct me on how to make the Password box have the characters masked that'd be extremely helpful.
if(textField.equals("Admin"))
{
if(textField.equals("Admin"))
{
JLabel lblLoginSuccessPlease = new JLabel("LOGIN SUCCESS! Please wait while the other functions are loaded");
lblLoginSuccessPlease.setBounds(124, 204, 344, 14);
frame.getContentPane().add(lblLoginSuccessPlease);
}
}
First of all, no need to check if textField is equal to "Admin" twice.
But the main problem is that a text field, which is a white rectangle accepting input in a UI, is ever equal to a String, which is a sequence of characters.
What you want to test is if the text entered in the text field is equal to "Admin":
if (textField.getText().equals("Admin"))
Your code has other problems:
using absolute coordinates instead of using a layout manager
adding elements to the frame dynamically instead of adding them from the start and simply making them visible when needed. Adding is needed, but then the GUI needs to be revalidated.
The frame has private access in Main class
private JFrame frame;
change it to public or remove the modifier
To get the text of JTextfield use textfield.getText()
//if (textField.equals("Admin")) {wrong
if (textField.getText().equals("Admin")) {//right way
You have to check the password not the username again
if (textField_1.getText().equals("Admin")) {
f (textField.getText().equals("Admin")) {//this is the password field
To mask the passwords you have to use JPasswordField.so create like
private JPasswordField textField;
and initialize like
textField = new JPasswordField();
and check like
if (textField_1.getText().equals("Admin")) {
if (String.valueOf(textField.getPassword()).equals("Admin")) {
JLabel lblLoginSuccessPlease = new JLabel("LOGIN SUCCESS! Please wait while the other functions are loaded");
lblLoginSuccessPlease.setBounds(124, 204, 344, 14);
frame.getContentPane().add(lblLoginSuccessPlease);
}
}
Afternoon all, i have a problem with some JFrame code, this JFrame is started when the user presses "New User", and whenever they do so, i get:
Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at frontend.Registration.<init>(Registration.java:36)
at frontend.Registration.<init>(Registration.java:25)
at frontend.Registration.<init>(Registration.java:25)
at frontend.Registration.<init>(Registration.java:25)
With this code:
package frontend;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class Registration extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private static boolean ranOnce = false;
private JPanel contentPane;
private Registration reg = new Registration();
private JTextField userTF;
private JTextField passTF;
private JTextField emailTF;
private LoginProcess lp = new LoginProcess();
private JLabel error;
/**
* Create the frame.
*/
public Registration() {
if (!ranOnce) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ranOnce = true;
reg = new Registration();
reg.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 245, 195);
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblUsername = new JLabel("Username:");
lblUsername.setBounds(10, 11, 75, 14);
contentPane.add(lblUsername);
JLabel lblPassword = new JLabel("Password:");
lblPassword.setBounds(10, 36, 75, 14);
contentPane.add(lblPassword);
JLabel lblEmail = new JLabel("Email:");
lblEmail.setBounds(10, 61, 75, 14);
contentPane.add(lblEmail);
userTF = new JTextField();
userTF.setBounds(95, 8, 130, 20);
contentPane.add(userTF);
userTF.setColumns(10);
passTF = new JTextField();
passTF.setColumns(10);
passTF.setBounds(95, 33, 130, 20);
contentPane.add(passTF);
emailTF = new JTextField();
emailTF.setColumns(10);
emailTF.setBounds(95, 58, 130, 20);
contentPane.add(emailTF);
error = new JLabel("Error: Username already in use");
error.setBounds(10, 120, 215, 14);
contentPane.add(error);
JButton regBtn = new JButton("Register");
regBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
if (lp.newUser(userTF.getText(), passTF.getText(), emailTF.getText())) {
reg.setVisible(false);
Main.mainFrame.setVisible(true);
} else {
if (lp.duplicateAccount) {
error.setText("lol");
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
regBtn.setBounds(10, 86, 215, 23);
contentPane.add(regBtn);
}
}
I know this error is caused by an infinite loop, or something. but i have a boolean in place to stop it from infinitely running. This code was working about 20mins ago, and i haven't changed the first part of the constructor since i created the code.
Any ideas? Thanks..
You create a new Object in the line private Registration reg = new Registration();.
You should create this object in the consturctor.
The problem at that line:
private Registration reg = new Registration();
You get in a infinite loop and then stack overflow.