How to Add Multiple Components to a Jframe

How to add multiple components to a JFrame?

You have 2 choices.

You can change the layout of your frame:

JFrame frame;
frame.setLayout(new FlowLayout());

Now, if you add more than one box, it will show up on the frame.

The other option is to do what you said you tried. (Adding a panel to the frame)

JPanel pane = new JPanel();
frame.add(pane);
(add the boxes to 'pane')

Also, you should be careful with the sizing of your Box. You will probably want a call to setPreferredSize() somewhere in the creation of the Box. This will tell Java what size to make the box when it is added to the layout.

You should also take a look at the Java Layout Manager Tutorials. There is lots of great info there.

And, one more thing. The reason only one box at a time was being displayed on the frame was because JFrame's layout manager is BorderLayout. And, when you call add on a component that has a BorderLayout, the component is automatically added to the center of the component. Subsequent calls to add will overwrite the center component, leaving only one component in the middle.

How to add multiple components to a JFrame

Your overall program design looks to be in error and needs to be re-done, and specifically I'm talking about having your drawable entity classes, Base, Enemy, and Player, each extend JComponent, since that allows these components to be drawn within their JComponent and their JComponent only and nowhere else. Instead:

  • Make your drawable entity classes, such as Player, Base, and Enemy implement a common interface, say Drawable, that has a painting method, say draw(Graphics g).
  • Do not have them extend JComponent, JPanel or any other Swing component.
  • Instead create one single class for graphics, say call it DrawingPanel, and have it extend JPanel or JComponent.
  • Within the single paintComponent method of this DrawingPanel, draw each entity, if it is located within the section of the game that requires drawing.
  • Strive to separate your view code, the GUI code, from your program model code, the code that governs your logical entity behaviors and locations, preferably having them in separate classes, and probably even separate packages.

Regarding questions:

I am currently trying to make Drawable interface, what should I put in the method?

The interface would not have any code within the method. Any classes that implement the interface would need to have code that allows the objects of that class to draw themselves.

If I have the DrawingPanel class, would I even need the Drawable interface?

Yes because DrawingPanel would hold a collection of your Drawable objects and would draw them within its paintComponent method, as already mentioned above.

Adding Multiple Components In Seperate Classes to JFrame In Another Class

Each component should be responsible for managing it's own size, you should start by overriding getPreferredSize of the panels and returning the size you would like to use.

You should also not rely on magic numbers, but instead should use actual physical values, for example, instead of

g.fillRect(0, 0, 600, 120);

You should use...

g.fillRect(0, 0, getWidth(), getHeight());

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test1 {

public static void main(String[] args) {
new Test1();
}

public Test1() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public static class TestPane extends JPanel {

public TestPane() {
setLayout(new BorderLayout());
add(new DrawBoard());
add(new QuestionBox(), BorderLayout.SOUTH);
}

}

public static class DrawBoard extends JPanel {

public static Color yellow = new Color(13816442);

@Override
public Dimension getPreferredSize() {
return new Dimension(600, 600);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(yellow);
g.fillRect(0, 0, 600, 600);
}
}

public static class QuestionBox extends JPanel {

@Override
public Dimension getPreferredSize() {
return new Dimension(600, 120);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(0, 0, 600, 120);
}

}
}

You should also know that Toolkit.getDefaultToolkit().getScreenSize(); is not the most reliable method for determining the visible screen area, as it does not take into account various OS elements, like the task bar or dock, which can take up screen space.

How to add multiple JComponents to a JPanel?

Try this

panel.add(new TextRect(content.get(0), 10, 10), BorderLayout.SOUTH);
panel.add(new TextRect(content.get(0), 100, 100), BorderLayout.CENTER);

panel.revalidate();
panel.repaint()

If you set the BorderLayout, you should use its properties. Also, after calling revalidate(), you should call repaint();

Another option is a GridLayout

panel = new JPanel(new GridLayout(1, 2)); // or 2, 1 depending if you want them 
// laid out vertically or horizontally

panel.add(new TextRect(content.get(0), 10, 10));
panel.add(new TextRect(content.get(0), 100, 100));


Related Topics



Leave a reply



Submit