boolean flag;
MainPanel()
{
Panel ScreenPanel = new Panel(); //Screen Panel
ScreenPanel.setLayout(new BorderLayout());//Screen in the north
ScreenPanel.add(scn,BorderLayout.CENTER);
scn.disable(); //can not receive event
Panel ButtonPanel = new Panel(); //Button Panel
ButtonPanel.setLayout(new GridLayout(4,4));
for (int i=0;i<16;i++)
{
btn[i]=new Button(str[i]); //new Button
ButtonPanel.add(btn[i]); //add to panel
}
Panel ButtonPane2 = new Panel(); //Button Panel
ButtonPane2.add(clear);
setLayout(new BorderLayout()); //Main panel
add(ScreenPanel,BorderLayout.NORTH);
add(ButtonPanel,BorderLayout.CENTER);
add(ButtonPane2,BorderLayout.SOUTH);
}
}
class CountFrame extends Frame //construct a frame {
public CountFrame(String title)
{
super(title);
MainPanel t=new MainPanel(); //Main Panel
add(t); //add MainFrame
addWindowListener(new end()); //add listener
}
class end extends WindowAdapter //close the window,inner class
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
}
class Count //main() {
public static void main(String args[])
{
CountFrame frm=new CountFrame(\计算器\
frm.setSize(220,180); //new frame,go above
frm.show();
} }
程序的运行结果为:
7.试设计一个电话簿程序的界面。
//PhoneBook.java
import javax.swing.*;
import java.awt.*;
public class PhoneBook extends JFrame {
void go()
{
this.setTitle(\电话簿程序\
this.setSize(400,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = this.getContentPane();
String[] data = {\张三\李四\赵二\王五\
JList dataList = new JList(data);
dataList.setSelectedIndex(1); // select \
dataList.getSelectedValue(); // returns \
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
p1.setSize(200,400);
contentPane.add(p1,BorderLayout.WEST);
contentPane.add(p2,BorderLayout.CENTER);
contentPane.add(p3,BorderLayout.SOUTH);
p1.add(dataList);
p2.setLayout(new GridLayout(6,1));
JLabel l1=new JLabel(\姓名:\
JTextField t1=new JTextField(\李四\
JLabel l2=new JLabel(\联系电话:\
JTextField t2=new JTextField(\
p2.add(l1);
p2.add(t1);
p2.add(l2);
p2.add(t2);
p3.add(new JButton(\添加\
p3.add(new JButton(\删除\
p3.add(new JButton(\修改\
this.show();
}
public static void main(String[] g) {
PhoneBook h=new PhoneBook();
h.go(); } }
8.为6和7题的添加事件处理功能。
第6题加事件处理:
//Count.java