0

I am currently designing a login screen, but I ran into a strange issue. I already designed my GUI with the help of swing, and it was time to make functional buttons. I wanted to test my login button and if it would take me to the frame I want, but it is unable to. I can set a JOptionPane.showMessageDialog for example, which works just fine, but I am unable to open another frame from the button. I tried with New JFrameName().setVisible(true), and also JFrameName test = new JFrameName(); test.setVisible(true);, but the methods show up in red. Here is my code.

package com.edu4java.swing.tutrial3;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginView {


    public static void main(String[] args) {
        JFrame frame = new JFrame("Bus Tour Booking System");
        frame.setSize(300, 200);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        frame.add(panel);
        placeComponents(panel);

        frame.setVisible(true);
    }

    private static void placeComponents(JPanel panel) {

        panel.setLayout(null);

        JLabel titleLabel = new JLabel("Bus Tour Booking System");
        titleLabel.setBounds(70,15,150,25);
        panel.add(titleLabel);

        JLabel userLabel = new JLabel("Username: ");
        userLabel.setBounds(30, 50, 80, 25);
        panel.add(userLabel);

        JTextField userText = new JTextField(20);
        userText.setBounds(120, 50, 130, 25);
        panel.add(userText);

        JLabel passwordLabel = new JLabel("Password: ");
        passwordLabel.setBounds(30, 80, 80, 25);
        panel.add(passwordLabel);

        JPasswordField passwordText = new JPasswordField(20);
        passwordText.setBounds(120, 80, 130, 25);
        panel.add(passwordText);

        JButton loginButton = new JButton("login");
        loginButton.setBounds(100, 125, 80, 25);
        panel.add(loginButton);

        ActionListener myButtonListener = new MyButtonListener();
        loginButton.addActionListener(myButtonListener);

    }

    private static class MyButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            //can't access a new frame from here :(
        }
    }
    }

I would be very grateful if someone could help me out, I read a lot on Stackoverflow and Reddit, but just can't find the solution. I am also new to Java, so that doesn't help a lot either :D. Thanks in advance!

P.S. As far as the actual functionality for the login screen, I am going to do that in a later stage.

Gergan Zhekov
  • 944
  • 1
  • 8
  • 27
  • 1) A log-in screen would typically be shown in a dialog (`JDialog` or `JOptionPane`). See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Mar 13 '18 at 10:37
  • 3) *"..but the methods show up in red."* Is Clint Eastwood nearby with a paint brush? No? Then it probably indicates the IDE trying to tell you there are errors in the code. What is the **text** of those errors? – Andrew Thompson Mar 13 '18 at 10:39

1 Answers1

0

This is your login class. I put the JFrame frame in the global scope, so you can manipulate it from the ButtonListener method. I also created a SomeFrame class, just to demonstrate the new JFrame that would be created when you click the button. When an action is performed(the button is clicked) a new object of SomeFrame is created. Since SomeFrame extends JFrame we can use the method setVisible() to a SomeFrame object. The SomeFrame frame appears and the LoginView is no longer visible.

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginView {

 public static JFrame frame = new JFrame("Bus Tour Booking System");

    public static void main(String[] args) {

        frame.setSize(300, 200);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        frame.add(panel);
        placeComponents(panel);

        frame.setVisible(true);
    }

    private static void placeComponents(JPanel panel) {

        panel.setLayout(null);

        JLabel titleLabel = new JLabel("Bus Tour Booking System");
        titleLabel.setBounds(70,15,150,25);
        panel.add(titleLabel);

        JLabel userLabel = new JLabel("Username: ");
        userLabel.setBounds(30, 50, 80, 25);
        panel.add(userLabel);

        JTextField userText = new JTextField(20);
        userText.setBounds(120, 50, 130, 25);
        panel.add(userText);

        JLabel passwordLabel = new JLabel("Password: ");
        passwordLabel.setBounds(30, 80, 80, 25);
        panel.add(passwordLabel);

        JPasswordField passwordText = new JPasswordField(20);
        passwordText.setBounds(120, 80, 130, 25);
        panel.add(passwordText);

        JButton loginButton = new JButton("login");
        loginButton.setBounds(100, 125, 80, 25);
        panel.add(loginButton);

        ActionListener myButtonListener = new MyButtonListener();
        loginButton.addActionListener(myButtonListener);

    }

    private static class MyButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
           SomeFrame newFrame = new SomeFrame();
           newFrame.setVisible(true);
           frame.setVisible(false);
        }

      }
 }

This is the SomeFrame class.

import javax.swing.JFrame;
import javax.swing.JPanel;

public class SomeFrame extends JFrame {

    public SomeFrame(){
         super("something");
            this.setSize(300, 200);
            this.setResizable(false);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JPanel panel = new JPanel();
            this.add(panel);

            this.setVisible(true);
    }

}
Eriona
  • 16
  • 3