blueprintchris
blueprintchris

Reputation: 1105

How can I correctly place a JPanel onto a JFrame?

I'm currently studying Software Engineering at Uni and I'm struggling to come to terms with how to add a JPanel onto a JFrame properly. My JPanel has a couple of buttons as well as a JLabel which changes by clicking on one of the buttons and using an ActionListener.

I know there are several ways to do it, and here is what I've been trying but I can't for the life of me figure it out!

I know I am doing loads wrong but what is it?

Here is my code:

UIPanelOne:

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


public class UIPanelOne extends JPanel implements ActionListener {

private JButton yes, no, cancel;
private JPanel panel;
private JLabel l1, l2;

UIPanelOne() {

    super();
    setVisible(true);

    //label dec
    panel = new JPanel();

    //buttons
    yes = new JButton("Yes");
    no = new JButton("No");
    cancel = new JButton("Cancel");

    //label dec
    l1 = new JLabel("Hello");

    //button dec
    panel.setLayout(new BorderLayout());
    panel.add(yes);
    panel.add(no);
    panel.add(cancel);
    panel.add(l1);

}

public void actionPerformed(ActionEvent e) {

    if (e.getSource() == yes) {

        l1.setText("OK then!");
    }else if (e.getSource() == no){

        l1.setText("Goodbye then!");


    }else if(e.getSource() == cancel){

        System.exit(0);
    }


}

public JComponent getGUI(){

    return panel;
}
}

UIFrame:

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


public class UIFrame extends JFrame{


//constructor
public UIFrame(){

    //layout
    super("Can I help you?");
    setSize(400,600);
    setLayout(new BorderLayout());
    setVisible(true);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    UIPanelOne hello = new UIPanelOne();

    getContentPane().add(hello.getGUI());
   }
}

UITest:

public class UITest {

public static void main(String[] args){

UIFrame frame = new UIFrame();
frame.pack();

}

}

I know alot of this is probably wrong and I'm a total amateur but I'm hoping to get better with some help!

Upvotes: 1

Views: 154

Answers (1)

Branislav Lazic
Branislav Lazic

Reputation: 14806

Since your UIPanelOne extends JPanel class you can add your component's on that panel (no need for creation of new panel), create a new instance and pass that instance to add method of JFrame:

add(new UIPanelOne());

or

UIPanelOne hello = new UIPanelOne();
add(hello,BorderLayout.CENTER);

or

setContentPane(hello);

Avoid extending your classes with swing component's unless you want to define new method's for them (creation of custom component's) or if you wan't to override some of their method's.

You don't have to set BorderLayout for JFrame since it's default layout for JFrame (aleady set).

Call setVisible method for JFrame AFTER you add component's.

Also, read about Concurrency in Swing

Upvotes: 3

Related Questions