第七次JAVA作业.docx
- 文档编号:12643089
- 上传时间:2023-04-21
- 格式:DOCX
- 页数:29
- 大小:899.57KB
第七次JAVA作业.docx
《第七次JAVA作业.docx》由会员分享,可在线阅读,更多相关《第七次JAVA作业.docx(29页珍藏版)》请在冰豆网上搜索。
第七次JAVA作业
第七次JAVA程序设计作业测控一班
1.编程题:
假定一个数组中存放有你选定的任意10个英文单词,请编制一个图形界面程序来浏览这些单词。
要求用一个标签来显示当前的单词,并有标有“上一个”、“下一个”的两个按钮来实现单词的前后翻动显示。
importjava.awt.BorderLayout;
importjava.awt.CardLayout;
importjava.awt.Font;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
publicclassTheExam7_1{
publicstaticvoidmain(String[]args){
String[]str=newString[]{"appple","banana","car","doctor","easy","festival","good","happy","important","june","kill"};
newMyFrame7_1(str);
}
}
classMyFrame7_1{
JFrameframe=newJFrame("单词查看工具");
JPanelpleft=newJPanel();
JPanelpright=newJPanel();
JButtonb1=newJButton("上一个");
JButtonb2=newJButton("下一个");
CardLayoutleftCard;
Fontfont;
publicMyFrame7_1(String[]str){
font=newFont("宋体",Font.PLAIN,40);
frame.setLayout(newBorderLayout());
frame.add(pleft,BorderLayout.CENTER);
frame.add(pright,BorderLayout.EAST);
leftCard=newCardLayout();
pleft.setLayout(leftCard);
for(inti=0;i { JButtonjb=newJButton(str[i]); jb.setFont(font); pleft.add(jb); } pright.setLayout(newGridLayout(6,1,0,0)); b1.setSize(60,30); pright.add(b1); classB1_EventimplementsActionListener{ publicvoidactionPerformed(ActionEvente) { leftCard.next(pleft); } } b1.addActionListener(newB1_Event()); b2.setSize(60,30); pright.add(b2); classB2_EventimplementsActionListener{ publicvoidactionPerformed(ActionEvente) { leftCard.previous(pleft); } } b2.addActionListener(newB2_Event()); frame.setBounds(400,150,500,500); frame.setVisible(true); frame.addWindowListener(newWindowAdapter(){ publicvoidwindowClosing(WindowEvente){ System.exit(0); } }); } } 2.编程题: 请编写一个简单的计算器。 要求的图形用户界面如下: importjava.awt.BorderLayout; importjava.awt.GridLayout; importjava.awt.event.ActionEvent; importjava.awt.event.ActionListener; importjava.util.HashSet; importjavax.swing.JButton; importjavax.swing.JFrame; importjavax.swing.JPanel; importjavax.swing.JTextField; /** 请编写一个简单的计算器。 要求的图形用户界面如下: */ publicclassTheExam7_2{ publicstaticvoidmain(String[]args){ newMyCalculator(); } } classMyCalculator{ privatedoubleresult=0.0; privateStringstring=""; privateJTextFieldjtext=newJTextField("",15); String[]str=newString[]{"Clear","+","0","1","2","3","-","*",".","4","5","6","/","%","=","7","8","9"}; publicMyCalculator(){ JFrameframe=newJFrame("calculator"); frame.setBounds(400,200,450,250); JPaneljp=newJPanel(newGridLayout(3,6,10,10)); frame.setLayout(newBorderLayout()); frame.add(jtext,BorderLayout.NORTH); frame.add(jp,BorderLayout.CENTER); finalHashSethashSet=newHashSet(); classMyEventimplementsActionListener { publicvoidactionPerformed(ActionEvente){ for(Objectk: hashSet){ if(e.getSource()==k) { JButtonjb=(JButton)k; if(jb.getText()=="Clear") jtext.setText(""); else jtext.setText(jtext.getText()+jb.getText()); if(jb.getText()=="="){ char[]c=jtext.getText().toCharArray(); inti; for(i=0;i if(c[i]=='+'||c[i]=='-'||c[i]=='*'||c[i]=='/'||c[i]=='%') break; } Stringastr=String.copyValueOf(c,0,i); Stringbstr=String.copyValueOf(c,i+1,c.length-i-2); MyComputemycompute=newMyCompute(); result=pute(Double.parseDouble(astr),Double.parseDouble(bstr),c[i]); jtext.setText(""+result); } } } } } for(inti=0;i<18;i++){ JButtonb=newJButton(str[i]); jp.add(b); b.addActionListener(newMyEvent()); hashSet.add(b); } frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } classMyCompute{ privatedoubleresult=0.0; publicMyCompute(){ } publicdoublecompute(doublea,doubleb,charc){ if(c=='+') result=a+b; if(c=='-') result=a-b; if(c=='*') result=a*b; if(c=='/') result=a/b; if(c=='%') result=a%b; returnresult; } } 3.编程题: 请编写一个简单的用户登录程序。 要求使用图形用户界面,用户名和密码假定均为java,且密码输入时全部显示为星号(*)。 importjava.awt.*; importjava.awt.event.ActionEvent; importjava.awt.event.ActionListener; importjavax.swing.*; /** 3.编程题: 请编写一个简单的用户登录程序。 要求使用图形用户界面, *用户名和密码假定均为java,且密码输入时全部显示为星号(*)。 */ publicclassTheExam7_3{ publicstaticvoidmain(String[]args){ MyLandmy=newMyLand(); } } classMyLand{ inta=0; publicMyLand(){ JFrameframe=newJFrame(); frame.setBounds(450,300,370,220); Fontfont=newFont("宋体",Font.PLAIN,30); frame.setFont(font); frame.setLayout(newBorderLayout()); JPanelpaneltop=newJPanel(); paneltop.add(newJLabel("用户登陆",JLabel.CENTER)); frame.add(paneltop,BorderLayout.NORTH); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanelpanel_1=newJPanel(newGridLayout(3,1)); frame.add(panel_1,BorderLayout.CENTER); JPanelpanel_11=newJPanel(newFlowLayout()); JPanelpanel_12=newJPanel(newFlowLayout()); JPanelpanel_13=newJPanel(newGridLayout(1,5)); panel_1.add(panel_11); panel_1.add(panel_12); panel_1.add(panel_13); panel_11.add(newJLabel("用户名: ")); finalJTextFieldtext1=newJTextField("",10); panel_11.add(text1); panel_12.add(newJLabel("用户密码: ")); finalJPasswordFieldtext2=newJPasswordField("",10); text2.setEchoChar('*'); panel_12.add(text2); JButtonb=newJButton("确定"); classMyExam7_3implementsActionListener{ publicvoidactionPerformed(ActionEvente){ JDialogdialog=newJDialog(); dialog.setBounds(450,200,560,400); dialog.setVisible(true); Fontfont1=newFont("宋体",Font.BOLD,70); dialog.setFont(font1); if((text1.getText()).equalsIgnoreCase("java")&&String.valueOf(text2.getPassword()).equals("java")) { dialog.add(newJLabel("登陆成功! ",JLabel.CENTER)); //newMyCalculator(); } else dialog.add(newJLabel("密码或用户名错误! ",JLabel.CENTER)); } } b.addActionListener(newMyExam7_3()); panel_13.add(newLabel("")); panel_13.add(newLabel("")); panel_13.add(b); panel_13.add(newLabel("")); panel_13.add(newLabel("")); } } 4.编程题: 请编写图形界面程序,统计用户在一个文本域中输入的文本的行数、单词数和字符数。 提示: 可设置一个按钮、一个文本域和一个标签,文本域用于输入文本,点击按钮开始统计,并在标签中显示统计结果。 importjava.awt.BorderLayout; importjava.awt.FlowLayout; importjava.awt.event.ActionEvent; importjava.awt.event.ActionListener; importjava.util.StringTokenizer; importjavax.swing.*; /** 4.编程题: *请编写图形界面程序,统计用户在一个文本域中输入的文本的行数 *、单词数和字符数。 提示: 可设置一个按钮、一个文本域和一个标签, *文本域用于输入文本,点击按钮开始统计,并在标签中显示统计结果。 */ publicclassTheExam7_4{ publicstaticvoidmain(String[]args){ newMyTextArea(); } } classMyTextArea{ publicMyTextArea(){ JFrameframe=newJFrame(); frame.setBounds(400,130,600,400); frame.setLayout(newBorderLayout()); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanelpleft=newJPanel(newBorderLayout()); frame.add(pleft,BorderLayout.CENTER); JPanelpright=newJPanel(newFlowLayout()); frame.add(pright,BorderLayout.EAST); //左侧组件 finalJLabellabel=newJLabel("统计结果",JLabel.CENTER); finalJTextAreatext=newJTextArea(4,5); text.setEnabled(true); pleft.add(label,BorderLayout.NORTH); pleft.add(text,BorderLayout.CENTER); //右侧组件 JButtonb=newJButton("统计"); JButtonbclear=newJButton("清空"); pright.add(b); pright.add(bclear); classMyEvent7_4implementsActionListener{ publicvoidactionPerformed(ActionEvente){ Stringstr=text.getText(); StringTokenizerstringToken=newStringTokenizer(str); char[]b=str.toCharArray(); inthang=text.getLineCount(); intcharNumber=b.length; intwordNumber=stringToken.countTokens(); label.setText("输入的文本有: "+hang+"行"+wordNumber+"个单词"+charNumber+"个字符"); } } b.addActionListener(newMyEvent7_4()); classEvent7_4implementsActionListener{ publicvoidactionPerformed(ActionEvente){ text.setText(""); } } bclear.addActionListener(newEvent7_4()); } } 5.编程题: 用一个按钮控制一个Applet的背景颜色从7种常用颜色中随机变化。 importjava.awt.Color; importjava.awt.Container; importjava.awt.event.ActionEvent; importjava.awt.event.ActionListener; importjava.util.HashMap; importjavax.swing.JApplet; importjavax.swing.JButton; /** 5.编程题: 用一个按钮控制一个Applet的背景颜色从7种常用颜色中随机变化。 */ publicclassTheExam7_5extendsJApplet{ @Override publicvoidinit(){ finalHashMaphashMap=newHashMap(); hashMap.put(1,Color.red); hashMap.put(2,Color.orange); hashMap.put(3,Color.yellow); hashMap.put(4,Color.green); hashMap.put(5,Color.blue); hashMap.put(6,Color.cyan); hashMap.put(7,Color.pink); finalContainercontent=this.getContentPane(); JButtonb=newJButton("Change"); content.setLayout(null); b.setBounds(140,45,80,25); content.add(b); classChangeColorimplementsActionListener{ publicvoidactionPerformed(ActionEvente){ intrandom=(int)(Math.random()*7); content.setBackground((Color)(hashMap.get(random))); } } b.addActionListener(newChangeColor()); } } 6.编程题: 请编写图形界面程序进行数字的中英文翻译。 提示: 可安排一个文本框、一个按钮和一个标签,从文本框输入一个数字(0~9),点击按钮将其对应的英文单词显示在标签中。 importjava.awt.event.ActionEvent; importjava.awt.event.ActionListener; importjavax.swing.JButton; importjavax.swing.JFrame; importjavax.swing.JLabel; importjavax.swing.JTextField; /*6.编程题: *请编写图形界面程序进行数字的中英文翻译。 提示: 可安排一个文本框 *、一个按钮和一个标签,从文本框输入一个数字(0-9),点击按钮将其对应的 *英文单词显示在标签中。 */ publicclassTheExam7_6{ publicstaticvoidmain(String[]args){ newMyTrainslation(); } } classMyTrainslation{ publicMyTrainslation(){ JFrameframe=newJFrame(); frame.setBounds(400,300,400,300); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CL
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 第七 JAVA 作业