Оглавление |
Назад
// ==========================================
// GridBag2.java
//(C) Alexandr Frolov, 1998
// E-mail: frolov@glasnet.ru
// Web: http://www.glasnet.ru/~frolov
// ==========================================
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class GridBag2 extends Applet
implements ActionListener
{
TextField tfFirstName;
Label lbFirstName;
TextField tfMiddleName;
Label lbMiddleName;
TextField tfLastName;
Label lbLastName;
TextField tfZip;
Label lbZip;
TextField tfCountry;
Label lbCountry;
Button btnOK;
Button btnCancel;
// ============================================
// init
// ============================================
public void init()
{
tfFirstName = new TextField(20);
lbFirstName = new Label("First name");
tfMiddleName = new TextField(20);
lbMiddleName = new Label("Middle name");
tfLastName = new TextField(20);
lbLastName = new Label("Last name");
tfZip = new TextField(10);
lbZip = new Label("ZIP code");
tfCountry = new TextField(8);
lbCountry = new Label("Country");
btnOK = new Button("OK");
btnCancel = new Button("Cancel");
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints c =
new GridBagConstraints();
setLayout(gbl);
// -----------------------
// First name
// -----------------------
c.anchor = GridBagConstraints.NORTHWEST;
c.fill = GridBagConstraints.NONE;
c.gridheight = 1;
c.gridwidth = 1;
c.gridx = GridBagConstraints.RELATIVE;
c.gridy = GridBagConstraints.RELATIVE;
c.insets = new Insets(10, 10, 0, 0);
gbl.setConstraints(tfFirstName, c);
add(tfFirstName);
gbl.setConstraints(lbFirstName, c);
add(lbFirstName);
// -----------------------
// OK
// -----------------------
c.gridwidth = GridBagConstraints.REMAINDER;
c.ipadx = 32;
gbl.setConstraints(btnOK, c);
add(btnOK);
// -----------------------
// Middle name
// -----------------------
c.ipadx = 0;
c.gridwidth = 1;
gbl.setConstraints(tfMiddleName, c);
add(tfMiddleName);
gbl.setConstraints(lbMiddleName, c);
add(lbMiddleName);
// -----------------------
// Cancel
// -----------------------
c.gridwidth = GridBagConstraints.REMAINDER;
c.ipadx = 10;
c.weightx = 1.0;
gbl.setConstraints(btnCancel, c);
add(btnCancel);
// -----------------------
// Last name
// -----------------------
c.ipadx = 0;
c.gridwidth = 1;
c.weightx = 0.0;
gbl.setConstraints(tfLastName, c);
add(tfLastName);
c.gridwidth = GridBagConstraints.REMAINDER;
gbl.setConstraints(lbLastName, c);
add(lbLastName);
// -----------------------
// ZIP
// -----------------------
c.gridwidth = 1;
gbl.setConstraints(tfZip, c);
add(tfZip);
c.gridwidth = GridBagConstraints.REMAINDER;
gbl.setConstraints(lbZip, c);
add(lbZip);
// -----------------------
// Country
// -----------------------
c.gridwidth = 1;
gbl.setConstraints(tfCountry, c);
add(tfCountry);
c.weighty = 1.0;
c.gridwidth = GridBagConstraints.REMAINDER;
gbl.setConstraints(lbCountry, c);
add(lbCountry);
// -----------------------
btnOK.addActionListener(this);
btnCancel.addActionListener(this);
}
// ============================================
// actionPerformed
// ============================================
public void actionPerformed(ActionEvent e)
{
AppletMsgBox amsgbox;
String s = "<Personal information>";
if(e.getSource().equals(btnOK))
{
s = "First name: " +
tfFirstName.getText() +
"\nMiddle name: " +
tfMiddleName.getText() +
"\nLast name: " +
tfLastName.getText() +
"\nZIP code: " + tfZip.getText() +
"\nCountry: " + tfCountry.getText();
amsgbox = new AppletMsgBox(s,
"Information");
amsgbox.show();
}
else if(e.getSource().equals(btnCancel))
{
tfFirstName.setText("");
tfMiddleName.setText("");
tfLastName.setText("");
tfZip.setText("");
tfCountry.setText("");
}
}
// ============================================
// getAppletInfo
// ============================================
public String getAppletInfo()
{
return "Name: GridBag2";
}
}
// ============================================
// Class AppletMsgBox
// ============================================
class AppletMsgBox extends Frame
implements ActionListener
{
Button btnOK;
TextArea ta;
// ============================================
// AppletMsgBox
// ============================================
public AppletMsgBox(String msg, String title)
{
super(title);
setSize(400, 200);
btnOK = new Button("OK");
ta = new TextArea(msg, 5, 40);
ta.setEditable(false);
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints c =
new GridBagConstraints();
setLayout(gbl);
c.anchor = GridBagConstraints.CENTER;
c.fill = GridBagConstraints.BOTH;
c.gridheight = 1;
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridx = GridBagConstraints.RELATIVE;
c.gridy = GridBagConstraints.RELATIVE;
c.insets = new Insets(10, 0, 0, 0);
gbl.setConstraints(ta, c);
add(ta);
c.fill = GridBagConstraints.NONE;
c.ipadx = 35;
gbl.setConstraints(btnOK, c);
add(btnOK);
btnOK.addActionListener(this);
}
// ============================================
// actionPerformed
// ============================================
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(btnOK))
{
setVisible(false);
}
}
}
|