I have three different frames:
welcome.java
Register.java
LoginForm.java
If I click on Login button login page must open(i.e LoginForm.java) and if I click on Register button register page must open
//welcome.java
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
public class welcome extends JFrame
{
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
welcome window = new welcome();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public welcome() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnLogIn = new JButton("LOG IN");
btnLogIn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
btnLogIn.setBounds(200, 69, 117, 25);
frame.getContentPane().add(btnLogIn);
JButton btnRegister = new JButton("Register");
btnRegister.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
btnRegister.setBounds(200, 138, 117, 25);
frame.getContentPane().add(btnRegister);
JLabel lblNewToSvk = new JLabel("New to SVK Polytechnic ?");
lblNewToSvk.setBounds(12, 143, 191, 15);
frame.getContentPane().add(lblNewToSvk);
}
}
.
`//LoginForm.java
import java.awt.EventQueue;
import javax.swing.JScrollPane;
public class LoginForm extends JPanel implements ItemListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
private JFrame frame;
private JTextField textField;
private JPasswordField pwdJjh;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LoginForm window = new LoginForm();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public LoginForm() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblStudentLogin = new JLabel("Login ");
lblStudentLogin.setFont(new Font("Dialog", Font.BOLD | Font.ITALIC, 30));
lblStudentLogin.setHorizontalAlignment(SwingConstants.CENTER);
lblStudentLogin.setBounds(94, 12, 265, 36);
frame.getContentPane().add(lblStudentLogin);
JLabel lblUserName = new JLabel("USER NAME");
lblUserName.setFont(new Font("Dialog", Font.BOLD, 15));
lblUserName.setBounds(12, 74, 111, 15);
frame.getContentPane().add(lblUserName);
textField = new JTextField();
textField.setFont(new Font("Dialog", Font.PLAIN, 15));
textField.setBounds(141, 70, 160, 22);
frame.getContentPane().add(textField);
textField.setColumns(10);
JLabel lblPassword = new JLabel("PASSWORD");
lblPassword.setFont(new Font("Dialog", Font.BOLD, 15));
lblPassword.setBounds(12, 116, 111, 15);
frame.getContentPane().add(lblPassword);
pwdJjh = new JPasswordField();
pwdJjh.setFont(new Font("Dialog", Font.PLAIN, 15));
pwdJjh.setBounds(141, 114, 160, 22);
frame.getContentPane().add(pwdJjh);
JLabel lblUserType = new JLabel("USER TYPE");
lblUserType.setFont(new Font("Dialog", Font.BOLD, 15));
lblUserType.setBounds(12, 154, 111, 15);
frame.getContentPane().add(lblUserType);
JComboBox<String> cm = new JComboBox<String>();
cm.setBounds(141, 148, 160, 24);
cm.addItem("User");
cm.addItem("Admin");
cm.addItemListener(this);
setLayout(null);
frame.getContentPane().add(cm);
JButton btnLogin = new JButton("Login ");
btnLogin.setBounds(41, 199, 117, 25);
frame.getContentPane().add(btnLogin);
JButton btnCancle = new JButton("Cancel");
btnCancle.setBounds(209, 199, 117, 25);
frame.getContentPane().add(btnCancle);
}
#Override
public void itemStateChanged(ItemEvent arg0) {
// TODO Auto-generated method stub
}
}
.
//Register.java
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
public class Register {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Register window = new Register();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Register() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblRegisterForm = new JLabel("Register Form");
lblRegisterForm.setForeground(Color.BLUE);
lblRegisterForm.setFont(new Font("Dialog", Font.BOLD | Font.ITALIC, 25));
lblRegisterForm.setBounds(119, 12, 209, 25);
frame.getContentPane().add(lblRegisterForm);
JLabel lblSelectTheUser = new JLabel("Select the user type");
lblSelectTheUser.setFont(new Font("Dialog", Font.BOLD, 15));
lblSelectTheUser.setBounds(22, 83, 192, 25);
frame.getContentPane().add(lblSelectTheUser);
JComboBox cb1 = new JComboBox();
cb1.setModel(new DefaultComboBoxModel(new String[] {"Student", "Lecturer", "Office staff", "HOD"}));
cb1.setBounds(213, 83, 151, 24);
frame.getContentPane().add(cb1);
}
public void setVisible(boolean b) {
// TODO Auto-generated method stub
}
public void setTitle(String string) {
// TODO Auto-generated method stub
}
public void setSize(int i, int j) {
// TODO Auto-generated method stub
}
public void setLocationRelativeTo(Object object) {
// TODO Auto-generated method stub
}
public void setDefaultCloseOperation(int exitOnClose) {
// TODO Auto-generated method stub
}
}
You can use
frame.setVisible(true);
to make a JFrame (form) visible.
Related
I am trying to test the GUI of a banking system application, but in the TestLogin class I have an error 'Cannot resolve constructor 'FrameFixture(GUI.Login)'. I tried to extend the SampleFrame class in the Login class, but IntelliJ can't find dependencies. What can I do to fix this?
public class TestLogin {
protected FrameFixture window;
#BeforeClass
public static void setUpOnce() {
FailOnThreadViolationRepaintManager.install();
}
#Before
public void setUp() {
Login frame = GuiActionRunner.execute(() -> new Login());
window = new FrameFixture(frame); //Cannot resolve constructor 'FrameFixture(GUI.Login)
window.show();
}
#Test
public void test() {}
#After
public void tearDown() {
window.cleanUp();
}
}
Here is the Login class:
package GUI;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Login{
public JFrame frame;
private JTextField textField;
private JPasswordField textField_1;
/**
* Create the application.
*/
public Login() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Banking System");
frame.getContentPane().setLayout(null);
JLabel label = new JLabel("Banking System");
label.setFont(new Font("Tahoma", Font.BOLD, 17));
label.setBounds(147, 11, 151, 41);
frame.getContentPane().add(label);
JLabel lblLoginScreen = new JLabel("Login Screen");
lblLoginScreen.setFont(new Font("Tahoma", Font.PLAIN, 13));
lblLoginScreen.setBounds(170, 63, 101, 23);
frame.getContentPane().add(lblLoginScreen);
JLabel lblUsername = new JLabel("Username:");
lblUsername.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblUsername.setBounds(55, 119, 64, 23);
frame.getContentPane().add(lblUsername);
JLabel lblPassword = new JLabel("Password:");
lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblPassword.setBounds(55, 159, 64, 23);
frame.getContentPane().add(lblPassword);
textField = new JTextField();
textField.setBounds(130, 121, 86, 20);
frame.getContentPane().add(textField);
textField.setColumns(10);
textField.setText("admin");
textField_1 = new JPasswordField();
textField_1.setBounds(130, 161, 86, 20);
frame.getContentPane().add(textField_1);
textField_1.setColumns(10);
JButton btnLogin = new JButton("Login");
btnLogin.addActionListener(new ActionListener() {
#SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e) {
String user,pass;
textField.setText("admin");
user="admin";
pass=textField_1.getText();
if((user.equals("admin")&&(pass.equals("admin"))))
{
JOptionPane.showMessageDialog(frame.getComponent(0), "Login Successfully");
frame.setVisible(false);
GUIForm.menu.setVisible(true);
}
else
{
JOptionPane.showMessageDialog(frame.getComponent(0), "Login Failed");
}
}
});
btnLogin.setBounds(260, 138, 89, 23);
frame.getContentPane().add(btnLogin);
}
}
Try
public class TestLogin extends AssertJSwingJUnitTestCase {
private FrameFixture window;
#Override
protected void onSetUp() {
Login frame = GuiActionRunner.execute(() -> new Login());
window = new FrameFixture(robot(), frame);
window.show();
}
#Test
public void test(){
}
#Override
protected void onTearDown () {
window.cleanUp();
}
}
And in the Login class add
public class Login extends JFrame
I am a newbie in Java GUI development and I am stuck in the following code.
I am open to suggestions. I am actually trying to create a simple login that gives OK if the password is matched to the number 3124, and otherwise shows the error message. Please help me.
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
public class testing {
private JFrame frame;
private JTextField username;
private JTextField password;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
testing window = new testing();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public testing() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnNewButton_1 = new JButton("cancel");
btnNewButton_1.setBounds(266, 181, 109, 56);
frame.getContentPane().add(btnNewButton_1);
username = new JTextField();
username.setBounds(227, 11, 128, 39);
frame.getContentPane().add(username);
username.setColumns(10);
password = new JTextField();
password.setBounds(227, 76, 128, 39);
frame.getContentPane().add(password);
final int num;
num=Integer.parseInt(password);
password.setColumns(10);
JButton btnNewButton = new JButton("login");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(num==3124)
{JOptionPane.showMessageDialog(null, "correct");}
else
{JOptionPane.showMessageDialog(null, "wrong");}
}
});
btnNewButton.setBounds(62, 181, 123, 56);
frame.getContentPane().add(btnNewButton);
}
}
You were checking the password even before the user has had the chance to enter anything into the text box. You need to get and check the value of num in the event listener code i.e. in actionPerformed. Also, don't convert the password to int (someone may enter some non-numeric string).
This code below works better.
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
public class Testing {
private JFrame frame;
private JTextField username;
private JTextField password;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Testing window = new Testing();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Testing() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnNewButton_1 = new JButton("cancel");
btnNewButton_1.setBounds(266, 181, 109, 56);
frame.getContentPane().add(btnNewButton_1);
username = new JTextField();
username.setBounds(227, 11, 128, 39);
frame.getContentPane().add(username);
username.setColumns(10);
password = new JTextField();
password.setBounds(227, 76, 128, 39);
frame.getContentPane().add(password);
password.setColumns(10);
JButton btnNewButton = new JButton("login");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
final String num;
num = (password.getText());
if (num.equalsIgnoreCase("3124")) {
JOptionPane.showMessageDialog(null, "correct");
} else {
JOptionPane.showMessageDialog(null, "wrong");
}
}
});
btnNewButton.setBounds(62, 181, 123, 56);
frame.getContentPane().add(btnNewButton);
}
}
When I run my java program, two JFrames are showing, the JFrame that I created and another one that is blank that I cannot close. I think this error also has something to do with my JFrame that I created with components in it, not show when I run the program, instead the one showing is the blank JFrame. How do I fix this?
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import java.awt.Font;
public class trial extends Frame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
private JFrame frame;
JButton newButton1, newButton2, newButton3,newButton4,newButton5,newButton6,newButton7,newButton8,newButton9;
Icon ic1=new ImageIcon("C:\\Users\\DB\\Desktop\\eclipse\\JAVA\\Final Project\\img\\INSTRUMENTS\\A1.png");
Icon ic2=new ImageIcon("C:\\Users\\DB\\Desktop\\eclipse\\JAVA\\Final Project\\img\\INSTRUMENTS\\A2.png");
Icon ic3=new ImageIcon("C:\\Users\\DB\\Desktop\\eclipse\\JAVA\\Final Project\\img\\INSTRUMENTS\\A3.png");
Icon ic4=new ImageIcon("C:\\Users\\DB\\Desktop\\eclipse\\JAVA\\Final Project\\img\\INSTRUMENTS\\A4.png");
Icon ic5=new ImageIcon("C:\\Users\\DB\\Desktop\\eclipse\\JAVA\\Final Project\\img\\INSTRUMENTS\\A5.png");
Icon ic6=new ImageIcon("C:\\Users\\DB\\Desktop\\eclipse\\JAVA\\Final Project\\img\\INSTRUMENTS\\A6.png");
Icon ic7=new ImageIcon("C:\\Users\\DB\\Desktop\\eclipse\\JAVA\\Final Project\\img\\INSTRUMENTS\\A7.png");
Icon ic8=new ImageIcon("C:\\Users\\DB\\Desktop\\eclipse\\JAVA\\Final Project\\img\\INSTRUMENTS\\A8.png");
Icon pink=new ImageIcon("C:\\Users\\DB\\Desktop\\eclipse\\JAVA\\Final Project\\img\\INSTRUMENTS\\c9.png");
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
trial window = new trial();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public trial() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 986, 677);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
newButton1 = new JButton(new ImageIcon("C:\\Users\\jaxma\\Documents\\JAVA\\Final Project\\img\\INSTRUMENTS\\A1.png"));
newButton1.setBounds(43, 37, 150, 150);
newButton2 = new JButton(new ImageIcon("C:\\Users\\jaxma\\Documents\\JAVA\\Final Project\\img\\INSTRUMENTS\\A2.png"));
newButton2.setBounds(191,37, 150, 150);
newButton3 = new JButton(new ImageIcon("C:\\Users\\jaxma\\Documents\\JAVA\\Final Project\\img\\INSTRUMENTS\\A3.png"));
newButton3.setBounds(340,37, 150, 150);
newButton4 = new JButton(new ImageIcon("C:\\Users\\jaxma\\Documents\\JAVA\\Final Project\\img\\INSTRUMENTS\\A4.png"));
newButton4.setBounds(43,186, 150, 150);
newButton5 = new JButton(new ImageIcon("C:\\Users\\jaxma\\Documents\\JAVA\\Final Project\\img\\INSTRUMENTS\\A5.png"));
newButton5.setBounds(191,186, 150, 150);
newButton6 = new JButton(new ImageIcon("C:\\Users\\jaxma\\Documents\\JAVA\\Final Project\\img\\INSTRUMENTS\\A6.png"));
newButton6.setBounds(340,186, 150, 150);
newButton7 = new JButton(new ImageIcon("C:\\Users\\jaxma\\Documents\\JAVA\\Final Project\\img\\INSTRUMENTS\\A7.png"));
newButton7.setBounds(43,335, 150, 150);
newButton8 = new JButton(new ImageIcon("C:\\Users\\jaxma\\Documents\\JAVA\\Final Project\\img\\INSTRUMENTS\\A8.png"));
newButton8.setBounds(191,335,150, 150);
newButton9 = new JButton(pink);
newButton9.setBounds(340,335,150, 150);
newButton1.addActionListener(this);
newButton2.addActionListener(this);
newButton3.addActionListener(this);
newButton4.addActionListener(this);
newButton5.addActionListener(this);
newButton6.addActionListener(this);
newButton7.addActionListener(this);
newButton8.addActionListener(this);
newButton9.addActionListener(this);
frame.getContentPane().add(newButton1);frame.getContentPane().add(newButton2);frame.getContentPane().add(newButton3);frame.getContentPane().add(newButton4);frame.getContentPane().add(newButton5);frame.getContentPane().add(newButton6);
frame.getContentPane().add(newButton7);frame.getContentPane().add(newButton8);frame.getContentPane().add(newButton9);
JButton btnNewButton = new JButton("");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
dispose();
DrumInfo a = new DrumInfo();
a.setLocationRelativeTo(null);
a.setVisible(true);
}
});
btnNewButton.setIcon(new ImageIcon("C:\\Users\\jaxma\\Documents\\JAVA\\Final Project\\img\\INSTRUMENTS\\drums.png"));
btnNewButton.setBounds(597, 37, 300, 277);
frame.getContentPane().add(btnNewButton);
JLabel lblNewLabel = new JLabel("Click the picture to find out what's the picture about!");
lblNewLabel.setFont(new Font("Berlin Sans FB Demi", Font.PLAIN, 15));
lblNewLabel.setBounds(563, 315, 382, 45);
frame.getContentPane().add(lblNewLabel);
JButton btnNewButton_1 = new JButton("BACK");
btnNewButton_1.setBounds(412, 563, 156, 63);
frame.getContentPane().add(btnNewButton_1);
JButton btnCategories = new JButton("CATEGORIES");
btnCategories.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
Puzzle2 a = new Puzzle2();
a.setLocationRelativeTo(null);
a.setVisible(true);
}
});
btnCategories.setBounds(604, 563, 156, 63);
frame.getContentPane().add(btnCategories);
JButton btnNext = new JButton("NEXT");
btnNext.setBounds(789, 563, 156, 63);
frame.getContentPane().add(btnNext);
setSize(600,500);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==newButton1){
Icon s1 = newButton1.getIcon();
if(newButton2.getIcon().equals(pink)) {
newButton2.setIcon(s1);
newButton1.setIcon(pink);
}
if(newButton4.getIcon().equals(pink)) {
newButton4.setIcon(s1);
newButton1.setIcon(pink);
}
}
if(e.getSource()==newButton2){
Icon s1 = newButton2.getIcon();
if(newButton1.getIcon().equals(pink)) {
newButton1.setIcon(s1);
newButton2.setIcon(pink);
}
if(newButton3.getIcon().equals(pink)) {
newButton3.setIcon(s1);
newButton2.setIcon(pink);
}
if(newButton5.getIcon().equals(pink)) {
newButton5.setIcon(s1);
newButton2.setIcon(pink);
}
}
if(e.getSource()==newButton3){
Icon s1 = newButton3.getIcon();
if(newButton2.getIcon().equals(pink)) {
newButton2.setIcon(s1);
newButton3.setIcon(pink);
}
if(newButton6.getIcon().equals(pink)) {
newButton6.setIcon(s1);
newButton3.setIcon(pink);
}
}
if(e.getSource()==newButton4){
Icon s1 = newButton4.getIcon();
if(newButton1.getIcon().equals(pink)) {
newButton1.setIcon(s1);
newButton4.setIcon(pink);
}
if(newButton7.getIcon().equals(pink)) {
newButton7.setIcon(s1);
newButton4.setIcon(pink);
}
if(newButton5.getIcon().equals(pink)) {
newButton5.setIcon(s1);
newButton4.setIcon(pink);
}
}
if(e.getSource()==newButton5){
Icon s1 = newButton5.getIcon();
if(newButton2.getIcon().equals(pink)) {
newButton2.setIcon(s1);
newButton5.setIcon(pink);
}
if(newButton6.getIcon().equals(pink)) {
newButton6.setIcon(s1);
newButton5.setIcon(pink);
}
if(newButton4.getIcon().equals(pink)) {
newButton4.setIcon(s1);
newButton5.setIcon(pink);
}
if(newButton8.getIcon().equals(pink)) {
newButton8.setIcon(s1);
newButton5.setIcon(pink);
}
}
if(e.getSource()==newButton6){
Icon s1 = newButton6.getIcon();
if(newButton9.getIcon().equals(pink)) {
newButton9.setIcon(s1);
newButton6.setIcon(pink);
}
if(newButton3.getIcon().equals(pink)) {
newButton3.setIcon(s1);
newButton6.setIcon(pink);
}
if(newButton5.getIcon().equals(pink)) {
newButton5.setIcon(s1);
newButton6.setIcon(pink);
}
}
if(e.getSource()==newButton7){
Icon s1 = newButton7.getIcon();
if(newButton4.getIcon().equals(pink)) {
newButton4.setIcon(s1);
newButton7.setIcon(pink);
}
if(newButton8.getIcon().equals(pink)) {
newButton8.setIcon(s1);
newButton7.setIcon(pink);
}
}
if(e.getSource()==newButton8){
Icon s1 = newButton8.getIcon();
if(newButton9.getIcon().equals(pink)) {
newButton9.setIcon(s1);
newButton8.setIcon(pink);
}
if(newButton7.getIcon().equals(pink)) {
newButton7.setIcon(s1);
newButton8.setIcon(pink);
}
if(newButton5.getIcon().equals(pink)) {
newButton5.setIcon(s1);
newButton8.setIcon(pink);
}
}
if(e.getSource()==newButton9){
Icon s1 = newButton9.getIcon();
if(newButton6.getIcon().equals(pink)) {
newButton6.setIcon(s1);
newButton9.setIcon(pink);
}
if(newButton8.getIcon().equals(pink)) {
newButton8.setIcon(s1);
newButton9.setIcon(pink);
}
}
if(newButton1.getIcon().equals(ic1)&&newButton2.getIcon().equals(ic2)&&newButton3.getIcon().equals(ic3)&&newButton4.getIcon().equals(ic4)&&newButton5.getIcon().equals(ic5)&&newButton6.getIcon().equals(ic6)&&newButton7.getIcon().equals(ic7)&&newButton8.getIcon().equals(ic8)&&newButton9.getIcon().equals(pink)) {
JOptionPane.showMessageDialog(this,"Congratulations! You won.");
}
}
}
I tried setting JLabel to being visible only after I select a certain JRadioButton. The program is getting an error if I set lblNewLabel_1.setVisible(true) in the action performed of a radio button. It worked with the text field, but with this not. What can I do? Is it different with the label? Is there any advice someone can offer me?
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Window;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;
import javax.swing.JRadioButton;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.ButtonGroup;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Fereastra extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private final ButtonGroup buttonGroup = new ButtonGroup();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Fereastra frame = new Fereastra();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Fereastra() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 751, 565);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
setContentPane(contentPane);
JTextArea textArea = new JTextArea();
textArea.setBounds(12, 13, 447, 251);
contentPane.add(textArea);
JRadioButton rdbtnNewRadioButton = new JRadioButton("Cautarea pe nivel");
rdbtnNewRadioButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(rdbtnNewRadioButton.isSelected())
{
textField.setVisible(false);
textField_1.setVisible(false);
}
}
});
buttonGroup.add(rdbtnNewRadioButton);
rdbtnNewRadioButton.setBounds(488, 55, 185, 25);
contentPane.add(rdbtnNewRadioButton);
JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("Cautarea in adancime");
rdbtnNewRadioButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(rdbtnNewRadioButton_1.isSelected())
{
textField.setVisible(true);
textField_1.setVisible(true);
}
else
{
textField.setVisible(false);
textField_1.setVisible(false);
}
}
});
buttonGroup.add(rdbtnNewRadioButton_1);
rdbtnNewRadioButton_1.setBounds(488, 96, 237, 25);
contentPane.add(rdbtnNewRadioButton_1);
JRadioButton rdbtnNewRadioButton_2 = new JRadioButton("Costul uniform");
rdbtnNewRadioButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(rdbtnNewRadioButton_2.isSelected())
{
textField.setVisible(false);
textField_1.setVisible(false);
}
}
});
buttonGroup.add(rdbtnNewRadioButton_2);
rdbtnNewRadioButton_2.setBounds(488, 138, 127, 25);
contentPane.add(rdbtnNewRadioButton_2);
JRadioButton rdbtnNewRadioButton_3 = new JRadioButton("Adancime iterativa");
rdbtnNewRadioButton_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(rdbtnNewRadioButton_3.isSelected())
{
textField_1.setVisible(true);
textField.setVisible(true);
}
else
{
textField_1.setVisible(false);
textField.setVisible(false);
}
}
});
buttonGroup.add(rdbtnNewRadioButton_3);
rdbtnNewRadioButton_3.setBounds(488, 179, 237, 25);
contentPane.add(rdbtnNewRadioButton_3);
JRadioButton rdbtnNewRadioButton_4 = new JRadioButton("Greedy");
rdbtnNewRadioButton_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(rdbtnNewRadioButton_4.isSelected())
{
textField.setVisible(false);
textField_1.setVisible(false);
}
}
});
buttonGroup.add(rdbtnNewRadioButton_4);
rdbtnNewRadioButton_4.setBounds(488, 221, 127, 25);
contentPane.add(rdbtnNewRadioButton_4);
JLabel lblNewLabel = new JLabel("Alegeti:");
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel.setBounds(488, 13, 84, 33);
contentPane.add(lblNewLabel);
textField = new JTextField();
textField.setBounds(402, 277, 116, 22);
contentPane.add(textField);
textField.setColumns(10);
textField.setVisible(false);
textField_1 = new JTextField();
textField_1.setBounds(246, 277, 116, 22);
contentPane.add(textField_1);
textField_1.setColumns(10);
textField_1.setVisible(false);
JLabel lblNewLabel_1 = new JLabel("Introduceti valorile dorite:");
lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel_1.setBounds(22, 277, 212, 21);
contentPane.add(lblNewLabel_1);
lblNewLabel_1.setVisible(false);
}
}
It's because your label is declared in function context, put the declaration next the textbox declaration and it should work fine
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.