How do i use border layouts? - java

I'm currently working on Layouts in Java. I'm trying to combine different layouts. So i have created a login Screen using Spring Layout, GridLayout and Border Layout.
The MainFrame (JFrame) uses GridLayout. The GridLayout consists of 2 Panel (North Panel and Main Panel). The Main Panel consists of the Jlabel, JTextfield and JButton of which I have no problem of. My problem is in the North Panel which uses Border Layout. It contains a JLabel (lblWelcome). I have been trying to bring the label to the center of the panel using Border Layout but it still aligns to the left. This is the below code:
JLabel lblWelcome = new JLabel("Welcome To The Login Screen");
JPanel northPanel = new JPanel(new BorderLayout());
northPanel.setBackground(Color.green);
northPanel.add(lblWelcome, BorderLayout.CENTER);
Login Screen :

Your JLabel is actually correctly centered, but its text isn't.
Simply change its creation to :
JLabel lblWelcome = new JLabel("Welcome To The Login Screen", SwingConstants.CENTER);

Related

How can I add a jpanel to an jpanel that is on a jscrollpane when I click a jlabel?

I have got a JScrollPane that is called incomeScroll and on this Scroll Pane is a JPanel that is called inscrollPanel.
If the user clicks on the plus label
then a new jpanel should be added to inscrollPanel. I called revalidate and repaint on the inscrollPanel and on the incomeScroll Panel, but it did nothing. I designed the window with NetBeans UI Designer. And I tried different layouts such as BoxLayout and FlowLayout. The JPanels should be vertical aligned. You can see two jpanels in each jscrollpane and my question is how can I add another jpanel to inscrollPanel which is the child of incomeScroll?
Here is the code I put in the mouselistener when you click on the plus label so far:
`javax.swing.JPanel panel = new javax.swing.JPanel();
panel.setPreferredSize( new Dimension(359, 149));
panel.setBackground( new java.awt.Color( 119, 255, 144));
inscrollPanel.add(panel);
inscrollPanel.revalidate();
inscrollPanel.repaint();
incomeScroll.revalidate();`

Aligning the JButton and JLabel on JFrame

I am trying to create a JFrame and in which I want the button (Select the Device) to be on top and a text message (Active) which is in the form of Label at the bottom. I am unable to do that and they are all coming up in the same line next to each other.
JFrame f= new JFrame("AutoV");
f.setVisible(true);
f.setSize(600,400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p=new JPanel();
p.setBackground(Color.gray);
JButton b=new JButton("Select the Device");
JLabel lab=new JLabel("Active");
lab.setVerticalAlignment(SwingConstants.BOTTOM);
//p.add(b);
p.add(lab);
p.setBorder(BorderFactory.createLineBorder(Color.black));
f.add(p);
Dimension dim1 = Toolkit.getDefaultToolkit().getScreenSize();
f.setLocation(dim1.width/2-f.getSize().width/2, dim1.height/2-f.getSize().height/2);
You should look up different layouts. The default layout of many components is FlowLayout, witch just aligns all elements horizontally, and as small as possible. Setting the panels layout to box or grid layout should do the trick.
https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

Java: How do i set my background as a picture with my buttons showing [duplicate]

This question already has answers here:
How to set an image as a background for Frame in Swing GUI of java?
(8 answers)
Closed 7 years ago.
Alright, I figured out how to add a background picture but how do i make the buttons show. I'm making pong if you're interested.
Here is my Code:
public class Gui extends JFrame{
private JButton JB;
private JButton EB;
public Gui(){
super("Pong");
JPanel outside = new JPanel();
JPanel inside = new JPanel();
setLayout(new BorderLayout());
this.setContentPane(new JLabel(new ImageIcon("S:\\Music\\Pong title pic.jpg")));
outside.setLayout(new BoxLayout(outside, BoxLayout.LINE_AXIS));
inside.setLayout(new BoxLayout(inside, BoxLayout.PAGE_AXIS));
outside.add(Box.createHorizontalStrut(280));
outside.add(inside);
outside.add(Box.createHorizontalStrut(20));
inside.add(Box.createVerticalStrut(20));
JLabel title = new JLabel("Pong");
inside.add(title);
inside.add(Box.createVerticalStrut(20));
JButton btt1 = new JButton("Start");
Dimension d = new Dimension(200,40);
btt1.setSize(d);
btt1.setMinimumSize(d);
btt1.setMaximumSize(d);
btt1.setPreferredSize(d);
JButton btt2 = new JButton("Credits");
btt2.setSize(d);
btt2.setMinimumSize(d);
btt2.setMaximumSize(d);
btt2.setPreferredSize(d);
JButton btt3 = new JButton("Exit");
btt3.setSize(d);
btt3.setMinimumSize(d);
btt3.setMaximumSize(d);
btt3.setPreferredSize(d);
inside.add(btt1);
inside.add(Box.createVerticalStrut(5));
inside.add(btt2);
inside.add(Box.createVerticalStrut(5));
inside.add(btt3);
inside.add(Box.createVerticalStrut(20));
add(outside);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800,600);
this.setVisible(true);
this.setResizable(false);
}
}
Thanks for responding.
//setLayout(new BorderLayout());
this.setContentPane(new JLabel(new ImageIcon("S:\\Music\\Pong title pic.jpg")));
setLayout(new BorderLayout());
You set the layout but then you replace the content pane of the frame so you loose the layout manager.
You need to set the layout after you set the content pane.
Note you can only use the label as the background is the image of the label is larger than the components being added to the label.
Also, don't attempt to manipulate the size of the buttons by using setSize(), setPreferredSize(), setMinimumSize() and setMaximumSize(). Let the button display at its preferred size.
If you want the button to all be the same size, then add the buttons to a panel using a GridLayout first. You can specify the spacing between components when you create the GridLayout. Or you can use a GridBagLayout, which will allow you so specify a constraint that "fills" the width of each cell.

Panel over a GridLayout Panel

Is it possible to show a Panel (e.g. Panel-A which is nearly transparent) over a panel that has a GridLayout of images?
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel gridOfImages = new JPanel(new GridLayout(3,3));//Panel with a grid of images
JPanel nearlyOpaquePanel = new JPanel(); //A panel that is almost transparent.
// assuming that all already have the required properties like size and colour.
mainPanel.add(gridOfImages,Borderlayout.CENTER);
mainPanel.add(nearlyOpaquePanel,BorderLayout.CENTER);
My thought with this idea is that the panels will stack together and the gridOfImages will be shown through the nearlyOpaquePanel, but my result is that I only got the nearlyOpaquePanel to show and I can't see the gridOfImages through it.
See How to Decorate Components with the JLayer Class

JTabbedPane in JPanel?

I have a simple problem when I want to add tabs in my jpanel. The alignment of the tabs get horizontal instead of vertical, wich looks like crap =/.
It looks like this:
If I discard the panel instead and add the tabbedPane directly to the frame, everything works fine.
If you uncomment the three lines of code and remove the getContentPane().add(jtp); you can reproduce my probleme.
working Code:
public class TabbedPane extends JFrame
{
public TabbedPane()
{
setTitle("Tabbed Pane");
setSize(300, 300); // set size so the user can "see" it
JTabbedPane jtp = new JTabbedPane();
// JPanel panel = new JPanel();//uncomment all three lines
// panel.add(jtp);
// getContentPane().add(panel);
getContentPane().add(jtp);//remove me
JPanel jp1 = new JPanel();// This will create the first tab
JPanel jp2 = new JPanel();// This will create the second tab
JLabel label1 = new JLabel();
label1.setText("This is Tab 1");
jp1.add(label1);
jtp.addTab("Tab1", jp1);
jtp.addTab("Tab2", jp2);
JButton test = new JButton("Press");
jp2.add(test);
setVisible(true); // otherwise you won't "see" it
}
public static void main(String[] args)
{
TabbedPane tab = new TabbedPane();
}
}
Thanks a lot!
If I discard the panel instead and add the tabbedPane directly to the frame, everything works fine.
The default layout of JPanel is FlowLayout, which "lets each component assume its natural (preferred) size." The default layout of JFrame is BorderLayout, the CENTER of which ignores preferred size. In either case, invoking setSize() precludes the layout from functioning initially; re-size the frame to see the effect. Instead, use pack(), which "Causes this Window to be sized to fit the preferred size and layouts of its subcomponents."
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true); // otherwise you won't "see" it
There are many things I would change in that code, starting with the recommendations of #trashgod. OTOH this is the minimal change needed in order to stretch the tabbed pane to the width/height of the parent container.
// give the panel a layout that will stretch components to available space
JPanel panel = new JPanel(new GridLayout());//uncomment all three lines
panel.add(jtp);
getContentPane().add(panel);
//getContentPane().add(jtp);//remove me
For more details see this answer.
Well firstly you can try this:
JPanel panel = new JPanel();//uncomment all three lines
panel.setLayout(new GridLayout());
JPanel jp1 = new JPanel();// This will create the first tab
JPanel jp2 = new JPanel();// This will create the second tab
JLabel label1 = new JLabel();
label1.setText("This is Tab 1");
jp1.add(label1);
jtp.addTab("Tab1", jp1);
jtp.addTab("Tab2", jp2);
JButton test = new JButton("Press");
jp2.add(test);
getContentPane().add(jtp);
and in the main:
TabbedPane tab = new TabbedPane();
tab.pack();
tab.setVisible(true);
May I suggest using MigLayout to set layouts, it will make your life easier. Hope it helps.
Try GridbagLayout. Once you have mastered it, you can design UI of any sort with this layout.
I agree with prasanth regarding the use of GridBagLayout
I have gone through this problem once and I solved it by adding the JTabbedPaneto the panel via GridBagLayout, make sure you add the JTabbedPane using the ipadx and ipady according to your requirements in your GridBagConstraints object
e.g.
JPanel myPanel=new JPanel();
myPanel.setLayout(new GridBagLayout());
JTabbedPane jTP=new JTabbedPane();
jTP.add("Tab1",new JPanel());//substitute your component instead of "new JPanel"
GridBagConstraints myConstraints=new GridBagConstraints();
myConstraints.ipadx=400;//streches the component being added along x axis - 200 px on both sides
myConstraints.ipady=600;//streches the component being added along y axis - 200 px on both sides
myPanel.add(jTP,myConstraints);
You can adjust both these properties according to what is perfect for your need

Categories