实验十一 Java的GUI编程初步程序清单11-1//ButtonCounter.javaimport javax.swing.*;import java.awt.*;public class ButtonCounter extends JFrame {public ButtonCounter() {super("显示按钮窗口");ImageIcon Icon = new ImageIcon("image/image.gif");JButton jbt1 = new JButton("按钮",Icon);JButton jbt2 = new JButton("按钮",Icon);JButton jbt3 = new JButton("按钮",Icon);JPanel p1 = new JPanel();p1.add(jbt1);JPanel p2 = new JPanel();p2.add(jbt2);JPanel p3 = new JPanel();p2.add(jbt3);JPanel p4=new JPanel();p4.add(jbt2);JPanel p5=new JPanel();p5.add(jbt3);getContentPane().setLayout(new FlowLayout());getContentPane().add(p1);getContentPane().add(p2);getContentPane().add(p3);getContentPane().add(p4);getContentPane().add(p5);}public static void main(String[] args) {// Create a frame and set its propertiesJFrame frame = new ButtonCounter();frame.pack();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);}}程序清单11-2://PasswordVerification.java1import javax.swing.*;2import java.awt.event.*;3import java.awt.*;4public class PasswordVerification extends JFrame implements ActionListener{5JLabel userLabel;//定义用户标签提示6JLabel passwordLabel;//定义密码标签提示7JTextField userText;//定义用户文本框8JPasswordField passwordText;//定义密码文本框9int count=1;//统计输入信息的次数10public PasswordVerification() {11super("请输入信息");12得到容器对象container;13container.setLayout(new FlowLayout());//设置默认布局1415创建密码标签1617创建密码输入文本框18//注册事件监听者;19container.add(userLabel);20container.add(userText);21container.add(passwordLabel);22container.add(passwordText);23setSize(240,100);24setVisible(true);25}26事件处理27String userName=new String("陈三");//假设为正确的用户名;28String password=new String("12345678");//假设为正确的密码;29if(e.getSource()==passwordText){30count++;31char[] passwords=passwordText.getPassword();32if(userText.getText().equals(userName)&&password.equals(new String(passwords)))33{34JOptionPane.showMessageDialog(null, "欢迎您:" + userName);35System.exit(0);36}37else if(count>3)38System.exit(0);39else{40JOptionPane.showMessageDialog(null,userText.getText()+"请输入正确信息");41}42}43}44public static void main(String args[]){45PasswordVerification pv=new PasswordVerification();46pv.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);47}48}程序清单11-3://CardManager.java1import javax.swing.*;2import java.awt.*;3import java.awt.event.*;4public class CardManager extends JFrame{5JLabel nameLabel,sexLabel,titleLabel,unitLabel;6JLabeladdressLabel,telLabel1,telLabel2,mobileLabel,faxLabel,emailLabel;7JTextField nameTxt,unitTxt,addressTxt;8JTextField telTxt1,telTxt2,mobileTxt,faxTxt,emailTxt;9JRadioButton sexBtn1,sexBtn2;10JComboBox titleBx;11JButton okBtn,cancelBtn;12public CardManager() {13super("名片录入管理");14Container c=getContentPane();15JPanel centerPanel=new JPanel();//输入面板16nameLabel=new JLabel("姓名");//定义姓名标签17nameTxt=new JTextField(10);//定义姓名文本输入框18centerPanel.add(nameLabel);19centerPanel.add(nameTxt);20sexLabel=new JLabel("性别");//定义性别标签21定义性别单选按钮122;//定义性别单选按钮223ButtonGroup group=new ButtonGroup();24group.add(sexBtn1);25group.add(sexBtn2);26centerPanel.add(sexLabel);27centerPanel.add(sexBtn1);28centerPanel.add(sexBtn2);29titleLabel=new JLabel("称谓");//定义称谓标签30String title[]={"总裁","总经理","经理","主任","处长","院长","校长","科长","教授","讲师"};//定义称谓内容31创建称谓文本输入框32titleBx.setMaximumRowCount(5);33centerPanel.add(titleLabel);34centerPanel.add(titleBx);35unitLabel=new JLabel("工作单位");//定义工作单位标签36unitTxt=new JTextField(30);//定义工作单位输入框37centerPanel.add(unitLabel);38centerPanel.add(unitTxt);39addressLabel=new JLabel("工作地址");//定义工作地址标签40addressTxt=new JTextField(30); //定义工作地址输入框41centerPanel.add(addressLabel);42centerPanel.add(addressTxt);43telLabel1=new JLabel("电话");//定义电话1标签44telTxt1=new JTextField(15);//定义电话1输入框45centerPanel.add(telLabel1);46centerPanel.add(telTxt1);47telLabel2=new JLabel("电话");//定义电话2标签48telTxt2=new JTextField(15); //定义电话2输入框49centerPanel.add(telLabel2);50centerPanel.add(telTxt2);51mobileLabel=new JLabel("手机");//定义手机标签52mobileTxt=new JTextField(15); //定义手机输入框53centerPanel.add(mobileLabel);54centerPanel.add(mobileTxt);55faxLabel=new JLabel("传真");//定义传真标签56faxTxt=new JTextField(15);//定义传真输入框57centerPanel.add(faxLabel);58centerPanel.add(faxTxt);59emailLabel=new JLabel("E-mail");//定义email标签60emailTxt=new JTextField(32);//定义email输入框61centerPanel.add(emailLabel);62centerPanel.add(emailTxt);63JPanel sPanel=new JPanel();//按钮面板64okBtn=new JButton("确定");//定义确定按钮65cancelBtn=new JButton("取消");//定义取消按钮66监视cancelBtn的动作67public void actionPerformed(ActionEvent e){68判断事件源是否是cancelBtn;69System.exit(0);70}71}72});73sPanel.add(okBtn);74sPanel.add(cancelBtn);75 c.setLayout(new BorderLayout());76将centerPanel放置c中的中间位置;77sPanel 放置c中的南面的位置;78setSize(418,250);79setVisible(true);80}81public static void main(String args[]){82CardManager cm=new CardManager();83cm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);84}85}程序清单11-4//ArtFont.javaimport javax.swing.*;import java.awt.*;import java.awt.event.*;public class ArtFont extends JFrame implements ActionListener{JComboBox fontType,fontSize;JCheckBox boldBx;//粗体按钮JCheckBox italicBx;//斜体按钮JButton colorBtn;//颜色按钮;String[] fontNames;//字体名称;String[] fontSizes;//字体尺寸;JLabel label;//输入提示标签;JTextField inputText;//文字输入框;JTextArea txtArea;//文字显示区;JPanel fontPanel;//字体设置;JPanel showPanel;//显示效果区Font font;int boldStyle,italicStyle,underlineStyle;int fontSizeStyle;String fontNameStyle;Color colorStyle=Color.black;//设置字体的默认颜色为黑色;public ArtFont() {super("字体设置");//设置默认字体boldStyle=0;italicStyle=0;underlineStyle=0;fontSizeStyle=10;fontNameStyle="宋体";font=new Font(fontNameStyle,boldStyle+italicStyle,fontSizeStyle);fontPanel=new JPanel();fontPanel.setLayout(new FlowLayout());//设置字体名字GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment(); fontNames=ge.getAvailableFontFamilyNames();//获得系统中所有字体的名字;fontType=new JComboBox(fontNames);fontType.setEditable(false);fontType.setMaximumRowCount(10);fontType.addItemListener(new ItemListener(){public void itemStateChanged(ItemEvent e){ //实现监听字体名字改变的事件}});//设置字体大小fontSizes=new String[63];for(int i=0;i<63;i++){fontSizes[i]=Integer.toString((i+10));}fontSize=new JComboBox(fontSizes);fontSize.setEditable(false);fontSize.setMaximumRowCount(10);fontSize.addItemListener(new ItemListener(){public void itemStateChanged(ItemEvent e){ //实现监听字体大小改变的方法}});//设置粗体选择按钮;boldBx=new JCheckBox("粗体");boldBx.addItemListener(new ItemListener(){public void itemStateChanged(ItemEvent e){ //实现监听选择粗体状态改变的方法}});//设置斜体选择按钮;italicBx=new JCheckBox("斜体");italicBx.addItemListener(new ItemListener(){public void itemStateChanged(ItemEvent e){//实现监听选择斜体状态改变的方法}}});//设置颜色选择;colorBtn=new JButton("颜色");colorBtn.addActionListener(this);//监听颜色按钮 //设置字体面板;fontPanel.add(fontType);fontPanel.add(fontSize);fontPanel.add(boldBx);fontPanel.add(italicBx);fontPanel.add(colorBtn);//设置输入提示标签label=new JLabel("输入");//设置文本输入框;inputText=new JTextField(30);inputText.addActionListener(this);//设置文本显示区;txtArea=new JTextArea(10,80);//20行80列;txtArea.setFont(font);//设置文本面板;showPanel=new JPanel();showPanel.add(label);showPanel.add(inputText);showPanel.setLayout(new FlowLayout());showPanel.add(new JScrollPane(txtArea));//设置容器;Container container=getContentPane();container.setLayout(new BorderLayout());container.add(fontPanel,BorderLayout.NORTH);container.add(showPanel,BorderLayout.CENTER);setSize(500,300);setVisible(true);}public void actionPerformed(ActionEvent e){if(e.getSource()==colorBtn){//改变颜色colorStyle=JColorChooser.showDialog(this,"选择字体颜色",colorStyle); colorBtn.setForeground(colorStyle);txtArea.setForeground(colorStyle);}else if(e.getSource()==inputText){//将输入文字在文字显示区表示;txtArea.setText(inputText.getText());}}public static void main(String args[]){ArtFont artFont=new ArtFont();artFont.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}}程序清单11-5//Knight.javapublic class Knight {protected int x;//x坐标;protected int y;//y坐标;protected pos;//移动的方向;public Knight() {this.x=0;this.y=0;this.pos=0;}public Knight(int curX,int curY,int curDir){//设置马的属性; this.x=curX;this.y=curY;this.pos=curDir;}public void setX(int curX){//设置x坐标this.x=curX;}public void setY(int curY){//设置y坐标this.y=curY;}public void setPos(int curDir){//设置移动方向this.pos=curDir;}public int getX(){//获得x坐标return this.x;}public int getY(){//获得y坐标return this.y;}public int getPos(){//获得当前的移动方向;return this.pos;}}程序清单11-6//KnightTourGame.javaimport javax.swing.*;import java.awt.event.*;public class KnightTourGame extends JFrame implements ActionListener{private JDesktopPane jDeskPanel;//菜单定义;private JMenuBar gameBar;//菜单条private JMenu gameMenu,helpMenu;//游戏菜单和帮助菜单private JMenuItem startItem,exitItem,helpItem,versionItem;//startItem开始菜单项、exitItem退出菜单项、helpItem关于菜单项//versionItem版本菜单项public KnightTourGame(){super("Knight Tour 问题");createMenu();//创建菜单;setJMenuBar(gameBar);//设置菜单;jDeskPanel=new JDesktopPane();//设置多文档的桌面getContentPane().add(jDeskPanel);//添加多文档的桌面setSize(700,600);setVisible(true);}public void createMenu(){//创建菜单创建菜单条创建菜单项”游戏”创建子菜单项“开始”创建子菜单项“退出”startItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));//设置快捷键Alt+1;exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2,ActionEvent.ALT_MASK));//设置快捷键Alt+2;添加开始菜单到游戏菜单添加退出菜单到游戏菜单;startItem.addActionListener(this);exitItem.addActionListener(this);创建菜单项“帮助”创建子菜单项“版本”helpItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_3,ActionEvent .ALT_MASK));//设置快捷键Alt+3versionItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_4,ActionEvent.ALT_MASK));//设置快捷键Alt+4添加关于子菜单到帮助菜单添加版本子菜单到帮助菜单helpItem.addActionListener(this);versionItem.addActionListener(this);gameBar.add(gameMenu); //添加游戏菜单到菜单条gameBar中gameBar.add(helpMenu); //添加帮助菜单到菜单条gameBar中}public void actionPerformed(ActionEvent e){if(e.getSource()==startItem){JInternalFrame mainFrame=new JInternalFrame("Knight Tour游戏",true,true,true,true);//定义一个游戏运行窗口KnightTourPanel mainPanel=new KnightTourPanel();//创建游戏控制面板对象mainPanel,KnightTourPanel类需要用户自己定义;mainFrame.getContentPane().add(mainPanel);//将游戏控制面板添加到游戏运行窗口mainFrame.setSize(460,530);jDeskPanel.add(mainFrame);//添加游戏运行窗口到桌面中;mainFrame.setVisible(true);}else if(e.getSource()==exitItem){System.exit(0);}else if(e.getSource()==helpItem){JOptionPane.showMessageDialog(null,“暂时没有”);}else if(e.getSource()==versionItem){JOptionPane.showMessageDialog(null," 作者:陈轶\n时间:2005年"); }}public static void main(String args[]){KnightTourGame ktg=new KnightTourGame();ktg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}}。