1

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: apexuxdesign@gmail.com");
        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);


    }
}
Filburt
  • 17,626
  • 12
  • 64
  • 115
Sean
  • 11
  • 1
  • 3
  • I haven't tried to do so, but I believe it would be as simple as having two JFrames. `JFrame mainWindow, portfolioWindow` and initialize both with information. – CoderMusgrove Oct 06 '15 at 22:51
  • 2
    Have a look at [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) for reasons why this is a bad idea and [How to Make Dialogs](http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html) for a more reasonable solution – MadProgrammer Oct 06 '15 at 23:00
  • 1
    In fact, this is pretty much Swing 101, see [Creating a GUI With JFC/Swing](http://docs.oracle.com/javase/tutorial/uiswing/) for more details. Avoid using `null` layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify – MadProgrammer Oct 06 '15 at 23:01
  • *"I struggle to understand a lot of tutorials when it is based on other peoples code."* .. I fail to understand how we can help you then. SO is not a place to source a personal tutor. – Andrew Thompson Oct 07 '15 at 02:57

1 Answers1

1

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.

Rubydesic
  • 3,386
  • 12
  • 27
  • `package com.nonsense;` ...hmm, ironically appropriate. 1) 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). 3) Swing/AWT GUIs should be started on the EDT .. – Andrew Thompson Oct 07 '15 at 02:54
  • @Andrew Thompson Many good points. However I cannot test on different OSes. This one works on Win8, for anyone who wants to know. – Rubydesic Oct 07 '15 at 10:26
  • *"I cannot test on different OSes."* Neither can I.. That's one of the reasons why it makes good sense to leave it to layouts! – Andrew Thompson Oct 07 '15 at 10:29
  • @Andrew Thompson Well, a very good point. Hopefully Sean is helped :) – Rubydesic Oct 07 '15 at 10:30