GridLayoutTest
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class GridBagLayoutTest extends JFrame{
//main
public static void main(String[] args){
GridBagLayoutTest gridbag = new GridBagLayoutTest();
gridbag.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gridbag.setSize(500,150);
gridbag.setVisible(true);
gridbag.pack();
}
//constructor
public GridBagLayoutTest(){
super("Using GridBagLayout");
buildLayout();
}
void buildLayout(){
Container pane = getContentPane();
GridBagConstraints constraints = new GridBagConstraints();
GridBagLayout layout = new GridBagLayout();
pane.setLayout(layout);
constraints.anchor = GridBagConstraints.WEST;
constraints.gridy = 0;
JLabel label = new JLabel("Firstname:");
label.setFont(new Font("Arial", Font.PLAIN,14));
pane.add(label,constraints);
JTextField textfield = new JTextField(15);
textfield.setFont(new Font("Arial",Font.PLAIN,14));
pane.add(textfield, constraints);
//add label2 and textfield2
label = new JLabel("LastName:");
label.setFont(new Font("Arial",Font.PLAIN,14));
pane.add(textfield,constraints);
//position next textfield
constraints.gridx = 0;
constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.fill = GridBagConstraints.HORIZONTAL;
//add spaces between horizontalfields
constraints.gridy = 1;
label = new JLabel("Address:");
label.setFont(new Font("Arial",Font.PLAIN,14));
pane.add(label,constraints);
constraints.gridx = 1;
textfield = new JTextField(30);
textfield.setFont(new Font("Arial", Font.PLAIN,14));
pane.add(textfield,constraints);
constraints.gridy++;
JButton buttonOk = new JButton("Ok");
buttonOk.setBorder(BorderFactory.createRaisedBevelBorder());
buttonOk.setBackground(Color.orange);
buttonOk.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
buttonOk.setToolTipText("...clickable, but actionless button");
buttonOk.setFont(new Font("Lucida HandWriting", Font.BOLD,14));
pane.add(buttonOk, constraints);
}
}
No comments:
Post a Comment