Sean
Sean

Reputation: 11

Making a second frame in Java swing

I have made a simple digital business card in Java swing using Eclipse, I am brand new to Java and programming in general so please bear with me. Basically what I want to do is add a button that says "Our Work" or "Portfolio" for example, which when clicked will open a new window where I can then add pictures or links or false reviews etc.

I know this is probably quite simple, but I struggle to understand a lot of tutorials when it is based on other peoples code.

package dbuscard;

import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.Font;
import javax.swing.JSeparator;

public class card {

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    card window = new card();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public card() {
        initialize();
    }

    /**
     * Initialise the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.getContentPane().setBackground(new Color(153, 204, 255));
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JLabel lblLogo = new JLabel("");
        lblLogo.setBounds(127, 11, 219, 104);
        frame.getContentPane().add(lblLogo);
        lblLogo.setIcon(new ImageIcon("Images\\zlogoimg.png")); 

        JLabel lblNewLabel = new JLabel("t: 01254 777494");
        lblNewLabel.setFont(new Font("Source Code Pro Light", Font.PLAIN, 11));
        lblNewLabel.setBounds(20, 187, 126, 14);
        frame.getContentPane().add(lblNewLabel);

        JLabel lblEApexuxdesigngmailcom = new JLabel("e: [email protected]");
        lblEApexuxdesigngmailcom.setFont(new Font("Source Code Pro Light", Font.PLAIN, 11));
        lblEApexuxdesigngmailcom.setBounds(20, 204, 200, 14);
        frame.getContentPane().add(lblEApexuxdesigngmailcom);

        JLabel lblVisitWebsite = new JLabel("visit website");
        lblVisitWebsite.setFont(new Font("Source Code Pro Light", Font.BOLD, 11));
        lblVisitWebsite.setBounds(10, 237, 117, 14);
        frame.getContentPane().add(lblVisitWebsite);

        JLabel facebook = new JLabel("");
        facebook.setBounds(282, 204, 64, 47);
        frame.getContentPane().add(facebook);
        facebook.setIcon(new ImageIcon("Images\\facebook.png"));

        JLabel twitter = new JLabel("");
        twitter.setBounds(320, 204, 72, 47);
        frame.getContentPane().add(twitter);
        twitter.setIcon(new ImageIcon("Images\\twitter.png"));

        JLabel youtube = new JLabel("");
        youtube.setBounds(356, 204, 68, 47);
        frame.getContentPane().add(youtube);
        youtube.setIcon(new ImageIcon("Images\\youtube.png"));

        JLabel lblSeanHutchinson = new JLabel("Sean Hutchinson");
        lblSeanHutchinson.setFont(new Font("Source Code Pro Light", Font.PLAIN, 11));
        lblSeanHutchinson.setBounds(20, 128, 126, 14);
        frame.getContentPane().add(lblSeanHutchinson);

        JLabel lblUxDesigner = new JLabel("UX Designer");
        lblUxDesigner.setFont(new Font("Source Code Pro Light", Font.PLAIN, 11));
        lblUxDesigner.setBounds(20, 145, 107, 14);
        frame.getContentPane().add(lblUxDesigner);

    JLabel lblNewLabel_1 = new JLabel("CEO - Apex UX Design");
        lblNewLabel_1.setFont(new Font("Source Code Pro Light", Font.PLAIN, 11));
        lblNewLabel_1.setBounds(20, 162, 158, 14);
        frame.getContentPane().add(lblNewLabel_1);


    }
}

Upvotes: 1

Views: 3165

Answers (1)

Rubydesic
Rubydesic

Reputation: 3476

Simple. Add an action listener which uses the method setVisible(true); on a seperate JFrame. Example code:

package com.nonsense;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class del {

private JFrame frame, frame2;


/**
 * Launch the application.
 */
public static void main(String[] args) {
    del window = new del();
    window.frame.setVisible(true);
}

/**
 * Create the application.
 */
public del() {
    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);

    frame2 = new JFrame();
    frame2.setBounds(100, 100, 450, 300);
    frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame2.getContentPane().setLayout(null);

    JButton btnOpenWindow = new JButton("Open Window");
    btnOpenWindow.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            frame2.setVisible(true);
        }
    });
    btnOpenWindow.setBounds(167, 118, 120, 23);
    frame.getContentPane().add(btnOpenWindow);

}
}

I use "DISPOSE_ON_CLOSE" so that the second window does not terminate the program when the X button is pressed.

Upvotes: 1

Related Questions