GridBagLayout Component Problem...
well i finally selected GridBagLayout for my purpose...as it suits for it best than any layout as all other layouts wont allow us for placing components in required place in JFrame..
But..still components are not getting placed in position which i set...all text fied,button,label are appearing in center at same place even after placing with gridx and grid y..so once pls check my code and tell me where is the problem..
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class lms extends JFrame implements ActionListener{
static JFrame frm;
JPanel pan;
JPanel pan1;
JPanel pan2;
JTextField name,pswd;
JLabel lbl1,lbl2,lbl3,lbl4,lbl5;
JButton b1;
JComponent ac;
public lms(){
GridBagLayout g=new GridBagLayout();
GridBagConstraints c=new GridBagConstraints();
frm=new JFrame("LMS");
pan=new JPanel();
pan1=new JPanel();
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.getContentPane().add(pan,BorderLayout.NORTH);
frm.getContentPane().add(pan1);
pan.setLayout(g);
pan1.setLayout(g);
lbl1=new JLabel("Name : ",JLabel.LEFT);
c.gridx=1;
c.gridy=0;
c.gridwidth = 1;
c.gridheight = 1;
c.weighty = 1.0;
c.weightx = 1.0;
c.fill=GridBagConstraints.EAST;
c.insets=new Insets(2,5,5,5);
g.setConstraints(lbl1, c);
lbl2=new JLabel("Password : ");
c.gridx=2;
c.gridy=0;
c.gridwidth = 1;
c.gridheight = 1;
c.fill=GridBagConstraints.EAST;
g.setConstraints(lbl2, c);
lbl3=new JLabel("Welcome To LMS");
lbl4=new JLabel();
c.gridx=5;
c.gridy=0;
c.gridwidth = 1;
c.gridheight = 1;
name=new JTextField(10);
c.gridx=1;
c.gridy=3;
c.gridwidth = 1;
c.gridheight = 1;
pswd=new JTextField(10);
c.gridx=2;
c.gridy=3;
c.gridwidth = 1;
c.gridheight = 1;
b1=new JButton("OK");
c.gridx=6;
c.gridy=0;
c.gridwidth = 1;
c.gridheight = 1;
name.setBackground(Color.pink);
name.setForeground(Color.blue);
Font f=new Font("Arial",Font.BOLD,15);
Font z=new Font("Arial",Font.ITALIC+Font.BOLD,15);
name.setFont(f);
lbl1.setFont(f);
lbl2.setFont(f);
lbl3.setFont(z);
lbl4.setFont(f);
lbl4.setForeground(Color.RED);
pswd.setFont(f);
b1.setFont(f);
}
public void actionPerformed(ActionEvent ae){
String s1=name.getText();
String s2=pswd.getText();
if(ae.getSource()=="OK" && s1.isEmpty() && s2.isEmpty())
{
lbl4.setText("Please Enter Username and Password!!!");
}
else if(s1.equals(s2)){
GetFrame2 f2=new GetFrame2();
f2.setSize(500,500);
f2.setVisible(true);
frm.setVisible(false);}
else
{
lbl4.setText("Enter Correct Username and password!!!");
}
}
public static void main(String args[]){
lms l1=new lms();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frm.setSize(screenSize.width, screenSize.height); //This one is for full screen
frm.setVisible(true);
}
}
only lbl3 is appearing perfectly in NORTH as it is set by me..all other labels,button and textfields are getting placed in center of frame even though i used gridx and gridy...
Please help me....
Re: GridBagLayout Component Problem...
Code Conventions for the Java Programming Language: Contents
-- Use consistent indents
-- Remove excess vertical whitespace (empty lines)
Also remove multiple lines that are commented out, and any code such as listeners that isn't relevant to the layout.
In other words, to get better help sooner post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.
db
Re: GridBagLayout Component Problem...
yah removed unrequired code...but i used white spaces..just to show various fields in code...now please help me on my problem...leaving other things in code....
Re: GridBagLayout Component Problem...
In future, please don't edit a post after there are responses, as that makes the thread difficult to follow. Just post a new response instead.
Your indenting is still all over the place, so I'm not going to try to read your code. If you're lucky, someone else may step in to help.
db
Re: GridBagLayout Component Problem...
hope this makes u read my code...
Code:
class lms extends JFrame implements ActionListener{
static JFrame frm;
JPanel pan;
JPanel pan1;
JPanel pan2;
JTextField name,pswd;
JLabel lbl1,lbl2,lbl3,lbl4,lbl5;
JButton b1;
JComponent ac;
public lms(){
GridBagLayout g=new GridBagLayout();
GridBagConstraints c=new GridBagConstraints();
frm=new JFrame("LMS");
pan=new JPanel();
pan1=new JPanel();
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.getContentPane().add(pan,BorderLayout.NORTH);
frm.getContentPane().add(pan1);
pan.setLayout(g);
pan1.setLayout(g);
lbl1=new JLabel("Name : ",JLabel.LEFT);
c.gridx=1;
c.gridy=0;
c.gridwidth = 1;
c.gridheight = 1;
c.weighty = 1.0;
c.weightx = 1.0;
c.fill=GridBagConstraints.EAST;
c.insets=new Insets(2,5,5,5);
g.setConstraints(lbl1, c);
lbl2=new JLabel("Password : ");
c.gridx=2;
c.gridy=0;
c.gridwidth = 1;
c.gridheight = 1;
c.fill=GridBagConstraints.EAST;
g.setConstraints(lbl2, c);
lbl3=new JLabel("Welcome To LMS");
lbl4=new JLabel();
c.gridx=5;
c.gridy=0;
c.gridwidth = 1;
c.gridheight = 1;
name=new JTextField(10);
c.gridx=1;
c.gridy=3;
c.gridwidth = 1;
c.gridheight = 1;
pswd=new JTextField(10);
c.gridx=2;
c.gridy=3;
c.gridwidth = 1;
c.gridheight = 1;
b1=new JButton("OK");
c.gridx=6;
c.gridy=0;
c.gridwidth = 1;
c.gridheight = 1;
}
Re: GridBagLayout Component Problem...
Your indentation is still quite random and distracting making your code very hard to read and easily understand. Consider reading or re-reading the link that Darryl provided that explains what your code should look like. Best of luck!