Only one component shows up in JFrame
The content pane of a JFrame
has a BorderLayout
. If you place a component in a BL with no constraints it ends up in the CENTER
. The center can only display one component.
For an immediate effect, I suggest:
f.add(top, BorderLayout.PAGE_START);
f.add(mid);
f.add(bot, BorderLayout.PAGE_END);
Other points.
- Take out
f.setSize(500, 500);
and callpack()
immediately beforesetVisible(true)
- For a better way to end the GUI, change
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tof.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
in.setVisible(true);
Except for the frame itself, take these out. A component automatically becomes visible when it is added to a top level container and that container is itself made visible.- Change
public class EncDecExample extends JFrame
topublic class EncDecExample
This code keeps a reference to a frame, and that is the right way to go.
Can't see one component in a JFrame
The problem is that you're using the default layout manager of a JFrame
which is a BorderLayout
. When you add
your second component, it will replace the first. (Both are added to the CENTER
cell.)
If you want to use components to show the boxes I suggest you lay them out without overlap.
Otherwise I'd suggest you draw all boxes on the same component.
Why is only 1 of my JButtons showing?
Many problems:
mainWindow.setVisible(true);
The frame should be made visible AFTER all the components are added to the frame, so this should be the last statement in the constructor.
con = getContentPane();
BorderLayout myLayout = new BorderLayout();
con.setLayout(myLayout);
The default layout manager for the content pane of a JFrame is a BorderLayout, so this code is unnecessary.
login.setSize(350, 100);
register.setSize(350, 100);
Don't try to set the size of a component. It is the job of the layout manager to set the size and location of each component.
JPanel loginPanel = new JPanel();
JPanel registerPanel = new JPanel();
loginPanel.add(login, BorderLayout.NORTH);
registerPanel.add(register, BorderLayout.SOUTH);
Why are you creating two panels? You can just add the buttons directly to the frame.
Also, the default layout manager for a JPanel is the FlowLayout. So you can't just a BorderLayout constraint and expect it to work.
loginPanel.setVisible(true);
registerPanel.setVisible(true);
All Swing component (except JFrame, JDialog etc) are visible by default, so the above code is unnecessary.
mainWindow.add(login);
mainWindow.add(register);
mainWindow.add(loginPanel);
mainWindow.add(registerPanel);
As mentioned earlier the default layout for the frame is a BorderLayout. If you don't specify a constraint, then the component goes to the "CENTER". But only a single component can be displayed in the center at one time.
Fix all the other problems and then try something like:
mainWindow.add(login, BorderLayout.NORTH);
mainWindow.add(register, BorderLayout.SOUTH);
mainWindow.add(loginPanel, BorderLayout.WEST);
mainWindow.add(registerPanel, BorderLayout.EAST);
to see the difference. Adjust the constraints as required.
I suggest you read the section from the Swing tutorial on Layout Manager for working examples to give you the basics of using each of the layout managers.
JPanel shows only the first custom JComponent added
JFrame frame = new JFrame();
First of all that statement in your View class is completely unnecessary. You would not create a JFrame instance in the constructor of a component. Also your code never references the variable which is a good indication it is not needed.
However, the main problem is your concept of creating custom components is wrong:
setPreferredSize(new Dimension(100, 100));
You attempt to set the preferred size of the component.
add(new Triangle(100, 200, Color.pink)); //this one doesn't appear
But then you attempt to do you custom painting at (100, 200) which is outside the size of the component. So the painting logic clipped at the size of component so you don't see anything being painted.
Custom painting should be done relative to (0, 0) of the component, not relative to the parent component.
If you you want to randomly position components on the parent panel then you need to:
- set the parent panel to use a null layout
- set the location of each component you add to the panel
- set the size of each component you add to the panel.
basically you need to take over the functionality of the layout manager.
Other problems with your current painting code:
Don't invoke repaint() in a painting method. This will essentially cause an infinite painting loop. If you need animation you use a Swing Timer to schedule the animation.
Don't invoke paintComponent(...) directly. Swing will invoke paintComponent() when a component needs to be repainted.
However, I would suggest that if you want to paint Shapes on a panel, Then you forget about creating custom components. Instead you keep an ArrayList of the Shapes you want to paint and then in the paintComponent() method of the panel you iterate through the ArrayList to paint each shape.
For an example of this approach take a look at the Draw On Component
example found in Custom Painting Approaches.
Note:
If you really want to be able to handle mouse events then you need to use a Shape object to represent your shapes to do proper hit detection. If you just display your shape as a component, then the mouse hit will be detected if you click anywhere in the rectangular area of the component, not just the triangle part that you actually paint. The Shape
class has a contains(...) method you can use to determine if you actually click in the Shape or not.
Check out Playing With Shapes for more information on this concept.
Related Topics
Quickly Read the Last Line of a Text File
How to Tackle Daylight Savings Using Timezone in Java
How to Find Difference Between Two Joda-Time Datetimes in Minutes
Java Sslhandshakeexception "No Cipher Suites in Common"
Why Do These Two Multiplication Operations Give Different Results
Java - Class.Getresource Returns Null
How to Handle Iframe in Selenium Webdriver Using Java
Garbage Collection of String Literals
Java Default Crypto/Aes Behavior
Runnable Jars Missing Images/Files (Resources)
How to Automatically Generate N "Distinct" Colors