今天复习了下java,别说java写UI的功能就是强,完成下面这个界面代码不到50行:
package MyApplet;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GridLayoutDemo extends JApplet
{
private static final long serialVersionUID = 1L;
private JTextField t = null;
private Container cp = null;
private JButton bt = null;
@Override
public void init()
{
cp = getContentPane();
Container cp1 = new Container();
cp1.setLayout(new GridLayout(4,3));
t = new JTextField(12);
t.setHorizontalAlignment(JTextField.RIGHT);
cp.add(BorderLayout.NORTH, t);
for(int i = 1; i < 10; ++i)
{
bt = new JButton(""+i);
bt.addActionListener(new Bul());
cp1.add(bt);
}
bt = new JButton("*");
bt.addActionListener(new Bul());
cp1.add(bt);
bt = new JButton("0");
bt.addActionListener(new Bul());
cp1.add(bt);
bt = new JButton(".");
bt.addActionListener(new Bul());
cp1.add(bt);
cp.add(BorderLayout.CENTER,cp1);
super.init();
}
private class Bul implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
t.setText(((JButton)e.getSource()).getText());
}
}
}