Reputation: 2533
I want two images from two different classes that extend JPanel to be side by side.
The problem that I'm having is that the two JPanels should be inside the JFrame but when I do framename.add(panel) it replaces the other one instead of adding two of them side by side.
I have tried adding flowlayout and other layouts inside the main class, but none of the images showed up.
So my question is, if I have two classes that extend Jpanel, how do i add those two panels inside a Jframe so that they'll be side by side (next to each other) without replacing the other panel?
Any suggestions would be greatly appreciated.
Edit: If I extend JFrame to a class, does that class automatically become a JPanel itself? I know what extends means, but I'm not sure how it works in regards to a Jframe.
Main.java
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class Main
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
Panel1 s = new Panel1(); //picture 1
Panel2 n = new Panel2(); //picture 2
frame.add(n);
frame.add(s); //here is the problem, it replaces the previous panel
f.setSize(200,100);
f.setLocation(0,400);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Panel1.java
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class image2 extends JPanel
{
ImageIcon anotherIcon;
public image2() //constructor
{
URL imageURL = Panel1.class.getResource("images/puppy.png");
anotherIcon = new ImageIcon(imageURL);
}
public void paint(Graphics g)
{
super.paintComponent(g);
anotherIcon.paintIcon(this, g, 0, 0);
}
}
Panel2.java
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class Panel2 extends JPanel
{
ImageIcon anotherIcon2;
public Panel2() //constructor
{
URL imageURL = Panel2.class.getResource("images/puppy2.png");
anotherIcon = new ImageIcon(imageURL);
}
public void paint(Graphics g)
{
super.paintComponent(g);
anotherIcon2.paintIcon(this, g, 0, 0);
}
}
Upvotes: 2
Views: 7468
Reputation: 59
You can try this. Set Layout of your Frame as: setLayout(null) and then can add Bounds to your panel ie. setBounds() and then add these panels to frame
Upvotes: 0
Reputation: 497
I corrected both Panel1.java and Panel2.java of minor mistakes and added an example of how you'd place them side by side using GridBagLayout
/GridBagConstraints
combo:
Main.java
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class Main {
public static void main(String[] args) {
GridBagConstraints gbc = new GridBagConstraints();
JFrame frame = new JFrame();
frame.setLayout(new GridBagLayout());
Panel1 s = new Panel1(); //picture 1
Panel2 n = new Panel2(); //picture 2
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
frame.add(n, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.gridwidth = GridBagConstraints.REMAINDER;
frame.add(s, gbc); //here is the problem, it replaces the previous panel
frame.setSize(200,100);
frame.setLocation(0,400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Panel1.java
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class Panel1 extends JPanel {
ImageIcon anotherIcon;
public Panel1 ( ) {
URL imageURL = Panel1.class.getResource("images/puppy.png");
anotherIcon = new ImageIcon(imageURL);
}
public void paint(Graphics g) {
super.paintComponent(g);
anotherIcon.paintIcon(this, g, 0, 0);
}
}
Panel2.java
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class Panel2 extends JPanel {
ImageIcon anotherIcon;
public Panel2 ( ) {
URL imageURL = Panel2.class.getResource("images/puppy2.png");
anotherIcon = new ImageIcon(imageURL);
}
public void paint(Graphics g) {
super.paintComponent(g);
anotherIcon.paintIcon(this, g, 0, 0);
}
}
The general idea for your code was not wrong, just that the panels were placed one on top of the other, causing one to hide the other....
Upvotes: 0
Reputation: 758
You are adding the panels directly to the JFrame, you have to adds the panels in the contentPane.
It's also recomended create a panel to be the contentPane frame.setContentPane(myPanel);
and configure it with some layout (BorderLayout, GridBagLayout, whatever...)
Upvotes: 0
Reputation: 15628
Try frame.getContentPane().add(...)
?
And set the layout of the contentpane to FlowLayout (or other)
JFrame f = new JFrame("This is a test");
f.setSize(400, 150);
Container content = f.getContentPane();
content.setLayout(new FlowLayout());
content.add(new JButton("Button 1"));
content.add(new JButton("Button 2"));
f.setVisible(true);
Upvotes: 0
Reputation: 347184
The JFrame's default layout manager is BorderLayout. Try changing it to use something like GridLayout instead
Take a look at A visual guide to layout managers for more ideas
Upvotes: 1
Reputation: 109815
JFrame has implemented BorderLayout in the API
only one JComponent can be placed to the one of 5th. areas in BorderLayout
I want two images from two different classes that extend JPanel to be side by side.
or
Upvotes: 2