1 Attachment(s)
Help me with alignment. GridBagLayout
hi. i want to align heading to the top centre and i want to align rest of my fields and labels to the left after heading. i have tried alot but cant do it.
i want to do it like this
Attachment 4047
Code:
public class Main {
public static void main(String[] args) {
Entry test = new Entry();
}
}
--------------------------
import java.awt.*;
import javax.swing.*;
public class Objects {
JLabel top;
// TextInput Fields
JTextField SerialNo;
JTextField RecDate;
JTextField GatePassNo;
JTextField BoxNo;
public GridBagConstraints gbc = new GridBagConstraints();
public JPanel EntryPanel = new JPanel(new GridBagLayout());
JPanel HeadingPanel= new JPanel();
Objects()
{
top = new JLabel("Text1");
BoxNo = new JTextField(10);
SerialNo = new JTextField(10);
}
}
--------------------------
import java.awt.*;
import javax.swing.*;
public class Entry extends Objects{
public Entry()
{
JFrame entry= new JFrame("Text1");
entry.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
entry.setLocationRelativeTo(null);
entry.setVisible(true);
top.setFont(new Font("Serif",Font.BOLD,30));
entry.getContentPane().add(HeadingPanel,BorderLayout.PAGE_START);
HeadingPanel.add(top);
entry.getContentPane().add(EntryPanel,BorderLayout.WEST);
gbc.gridx = 1;
gbc.gridy=5;
EntryPanel.add(BoxNo);
entry.setSize(600,600);
}
}
cross-posted at: Help me in alligning . GridBagLayout
Re: Help me with alignment. GridBagLayout
Do not use setSize(). Instead if possible let the components set their own sizes based on their need and the layout manager. You can use setPreferredSize(...) if you must set a size of a component. Always call pack() on the JFrame after adding all components which tells the layout managers to do their thing, then call setLocationXXX(...) then call setVisible(true) in that order.
Re: Help me with alignment. GridBagLayout
k thnx. but i like to make JFrame full screen. Thats why i was asking..
Re: Help me with alignment. GridBagLayout