HI there, so my program so far only has buttons... can someone modify my program so when i press a button there will be another popup page, and so on? thanks !

p.s. my program right now only has buttons but the buttons does not do/go anywhere.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Dimension;

public class RestaurantFirstPage1 {
  final static String LABEL_TEXT = "Welcome to the restaurant rating website";
  JFrame frame;
  JPanel contentPane;
  JLabel label;
  JButton signIn;
  JButton signUp;
  JButton exit;
  
  public RestaurantFirstPage1() {
    frame = new JFrame("Restaurant First Page");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    contentPane = new JPanel();
    contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
    contentPane.setBorder(BorderFactory.createEmptyBorder(50,20,30,20));
    contentPane.setBackground(Color.white); 
    
    label = new JLabel(LABEL_TEXT);
    label.setForeground(Color.black); 
    label.setAlignmentX(JLabel.CENTER_ALIGNMENT);
    label.setBorder(BorderFactory.createEmptyBorder(20,50,20,50));
    contentPane.add(label);
    
    signIn = new JButton("Sign In");   
    contentPane.add(signIn);
    signIn.setAlignmentX(JButton.CENTER_ALIGNMENT);
    contentPane.add(Box.createRigidArea(new Dimension(0, 15)));
    signIn.setActionCommand("Sign In");
    signIn.addActionListener(new NextPage()); 
                          
    signUp = new JButton("Sign Up");
    contentPane.add(signUp);
    signUp.setAlignmentX(JButton.CENTER_ALIGNMENT);
    contentPane.add(Box.createRigidArea(new Dimension(0, 15)));
    signUp.setActionCommand("Sign Up");
    signUp.addActionListener(new NextPage());
    
    exit = new JButton("Exit");
    contentPane.add(exit);
    exit.setAlignmentX(JButton.CENTER_ALIGNMENT);
    contentPane.add(Box.createRigidArea(new Dimension(0, 15)));
    exit.addActionListener(new Terminate());
  
    frame.setContentPane(contentPane);
    
    frame.pack();
    frame.setVisible(true);
  }

    private static void runGUI() {
      JFrame.setDefaultLookAndFeelDecorated(true);
      
      RestaurantFirstPage1 firstPage = new RestaurantFirstPage1();
    }
    
    class NextPage implements ActionListener { 

    public void actionPerformed(ActionEvent event) {
      String eventName = event.getActionCommand();
      
      if(eventName.equals("Sign In")) {
        label.setText("Sign In Page");
  
      } 
      else if(eventName.equals("Sign Up")) {
        label.setText("Sign Up Page");
      } 

    }    
    }
    
    class Terminate implements ActionListener {   
      public void actionPerformed(ActionEvent event) {
      System.exit(0);
    }
    }
    

    public static void main(String[] args) {
      javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
          runGUI();
        }
      });
    }
  }

Recommended Answers

All 2 Replies

.

in Your ActionListener class simply set this frame.setVisble(false) and your other page frame.setVisble(true).

Another option will be to add many panels and make setVible for this panel(false) and setVisible(true) for other panel

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.