《Java程序设计》实 验 报 告(五)
实验名称:java编程基础 | 实验地点:10-306 |
所使用的工具软件及环境: JDK1.7或1.8与Eclipse | |
1.了解图形用户界面基本组件窗口、按钮、文本框、选择框、滚动条等的使用方法, 2.了解如何使用布局管理器对组件进行管理,以及如何使用Java 的事件处理机制。 | |
二、实验内容: 1.在JFrame 窗口中添加组件 (1)程序功能:创建JFrame 窗口,并在其中添加工具栏。 (2)准备图片文件: 在当前目录下建立一个image 文件夹, 存放3 个图片文件, 例如1.jpg。 (3)提示:工具条: JToolBar toolBar; 带图标的按钮:button1=new JButton(new ImageIcon("images/Line.jpg")); 产生的窗口界面如图所示。 | |
三、源代码 //LX5_1.java package 实验5; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class LX5_1 { public static void main(String[] args) { PictureFrame g=new PictureFrame(); g.setVisible(true); } } class PictureFrame extends JFrame{ JButton a,b,c;//三个按钮 JToolBar toolbar;//工具栏 JTextArea textarea;//文本域 JScrollPane scrollpane;//滚动窗口 JPanel panel; PictureFrame(){ super("带有工具栏按钮的窗口"); //setTitle("带有工具栏按钮的窗口"); //setBounds(200,300,370,280);//框架大小 //setLocation(300,200);//设置框架在屏幕上的显示位置 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭 a=new JButton(new ImageIcon("image/1.jpg")); b=new JButton(new ImageIcon("image/2.jpg")); c=new JButton(new ImageIcon("image/3.jpg")); toolbar=new JToolBar();//创建工具栏,把按钮添加到工具栏 toolbar.add(a); toolbar.add(b); toolbar.add(c); textarea=new JTextArea(8,70); scrollpane=new JScrollPane(textarea); panel=new JPanel(); setContentPane(panel); panel.setLayout(new BorderLayout()); panel.setPreferredSize(new Dimension(300,150)); panel.add(toolbar,BorderLayout.NORTH); panel.add(scrollpane,BorderLayout.CENTER); pack(); } } 四、实验结果测试、收获与体会: 学习了Java布局管理,同时也学习到在Jframe中添加图片按钮,和滚动窗口并使用边界布局。对图形用户界面有了进一步的学习与收获。 |
实验名称:java编程基础 | 实验地点:10-306 |
所使用的工具软件及环境: JDK1.7或1.8与Eclipse | |
1.了解图形用户界面基本组件窗口、按钮、文本框、选择框、滚动条等的使用方法, 2.了解如何使用布局管理器对组件进行管理,以及如何使用Java 的事件处理机制。 | |
二、实验内容: 2.对话框实现: 可以使用JSwing中的JOptionPane类。 编写LX5_3.java 程序文件,选择单选按钮,跳出相应对话框. | |
三、源代码 //LX5_2.java package 实验5; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class LX5_2 { public static void main(String[] args) { OptionFrame g=new OptionFrame(); g.setVisible(true); } } class OptionFrame extends JFrame{ OptionFrame(){ JRadioButton[] radioBtn; String[] radiobtnName= {"错误对话框","内部信息对话框","选择对话框","警告对话框"}; JPanel panel=new JPanel(); setTitle("对话框演示"); setBounds(200,300,500,400);//框架大小 setLocation(300,200);//设置框架在屏幕上的显示位置 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭 radioBtn=new JRadioButton[4]; ButtonGroup btnGrp=new ButtonGroup(); panel.setLayout(new GridLayout(4,1,10,10)); ActionListener messageListener=new ActionListener() { public void actionPerformed(ActionEvent e) { if(radioBtn[0].isSelected()) { JOptionPane.showMessageDialog(null,"程序错误","错误",JOptionPane.YES_OPTION); } if(radioBtn[1].isSelected()) { JOptionPane.showConfirmDialog(null,"请选择一个","选择",JOptionPane.YES_NO_CANCEL_OPTION); } if(radioBtn[2].isSelected()) { Object[] p= {"金牌","银牌","铜牌"}; Object select=JOptionPane.showInputDialog(null, "选择一个","输入", JOptionPane.INFORMATION_MESSAGE, null, p, p[1]); JOptionPane.showMessageDialog(null,"您的选择是:"+select.toString()); } if(radioBtn[3].isSelected()) { String[] p={"确认","取消"}; JOptionPane.showOptionDialog(null,"点击“确认”继续","警告",0,JOptionPane.YES_NO_OPTION,null,p,p[0]); } } }; for(int i=0;i<radioBtn.length;i++) { radioBtn[i]=new JRadioButton(radiobtnName[i]); radioBtn[i].addActionListener(messageListener); btnGrp.add(radioBtn[i]); panel.add(radioBtn[i]); } add(panel); } } 四、实验结果测试、收获与体会: 学习了在Jframe中添加面板容器,并分别在Jframe、面板容器中添加组件并使用布局管理方式,通过单选按钮进行插入监听器,对按钮做出反应,并且使用对话框来进行来弹出信息等。进一步学习了对话框与单选按钮和Jframe的使用,对图形用户界面的编程有了进一步的提升。 |
实验名称:java编程基础 | 实验地点:10-306 |
所使用的工具软件及环境: JDK1.7或1.8与Eclipse | |
一、实验目的: 1.了解图形用户界面基本组件窗口、按钮、文本框、选择框、滚动条等的使用方法, 2.了解如何使用布局管理器对组件进行管理,以及如何使用Java 的事件处理机制。 | |
二、实验内容: 3.在例8.17的基础上,扩充文本编辑器的功能。 | |
三、源代码 //Note.java package 实验5; import java.io.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; class NoteFrame extends JFrame implements ActionListener{ private JTextArea ta; private String fn = null; int mode=0;//粗体 int fontSize=12;//字体大小
NoteFrame(){ setTitle("记事本"); setBounds(0,0,600,500); setDefaultCloseOperation(EXIT_ON_CLOSE); ta = new JTextArea(); JMenuBar menubar = new JMenuBar(); JMenu fileMenu = new JMenu("文件"); JMenu editMenu = new JMenu("编辑"); JMenu fontMenu = new JMenu("文字编辑");// JMenuItem newMenu = new JMenuItem("新建"); JMenuItem openMenu = new JMenuItem("打开..."); JMenuItem closeMenu = new JMenuItem("关闭"); JMenuItem saveMenu = new JMenuItem("保存"); JMenuItem savetoMenu = new JMenuItem("另存为..."); JMenuItem exitMenu = new JMenuItem("退出"); JMenuItem cutMenu = new JMenuItem("剪切"); JMenuItem copyMenu = new JMenuItem("复制"); JMenuItem pasteMenu = new JMenuItem("粘帖"); JMenuItem sizeMenu = new JMenuItem("改变字体大小");// JMenuItem styleMenu =new JMenuItem("改变字体样式");
newMenu.addActionListener(this); openMenu.addActionListener(this); closeMenu.addActionListener(this); saveMenu.addActionListener(this); savetoMenu.addActionListener(this); exitMenu.addActionListener(this); cutMenu.addActionListener(this); copyMenu.addActionListener(this); pasteMenu.addActionListener(this); sizeMenu.addActionListener(this);// styleMenu.addActionListener(this);//
setJMenuBar(menubar); fileMenu.add(newMenu); fileMenu.add(openMenu); fileMenu.add(closeMenu); fileMenu.addSeparator(); //添加分割条 fileMenu.add(saveMenu); fileMenu.add(savetoMenu); fileMenu.addSeparator(); fileMenu.add(exitMenu); editMenu.add(cutMenu); editMenu.add(copyMenu); editMenu.add(pasteMenu); fontMenu.add(sizeMenu);// fontMenu.add(styleMenu);//
menubar.add(fileMenu); menubar.add(editMenu); menubar.add(fontMenu);
add(new JScrollPane(ta,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED)); } public void actionPerformed(ActionEvent e){
String s = e.getActionCommand(); //方法getSource返回Object类的对象,并将其强制类型转化为JMenuItem if(s.equals("关闭")) System.exit(0); if(s.equals("打开...")){ try{ JFrame f = new JFrame(); FileDialog fd = new FileDialog(f,"打开文件",FileDialog.LOAD); fd.setVisible(true); String path = fd.getDirectory(); String fileName = fd.getFile(); BufferedReader br = new BufferedReader(new FileReader(path+fileName)); fn = path + fileName; ta.setText(""); String str = br.readLine(); while(str!=null){ ta.append(str+"\n"); str = br.readLine(); } br.close(); }catch(IOException ex) { //open file error } return; } if(s.equals("保存")){ try{ if(fn == null) { JFrame f = new JFrame("存为"); FileDialog fd = new FileDialog(f,"文件保存为",FileDialog.SAVE); fd.setFile("*.txt"); fd.setVisible(true); String path = fd.getDirectory(); String fileName = fd.getFile(); if(fileName == null) return; fn = path + fileName; } PrintWriter pw = new PrintWriter(new FileWriter(fn)); String content = ta.getText().replaceAll("\n", "\r\n"); pw.write(content,0,content.length()); pw.flush(); pw.close(); }catch(IOException ex) { //save file failed } return; } if(s.equals("新建")){ ta.setText(""); fn = null; return; } if(s.equals("另存为...")){ JFrame f = new JFrame("另存为"); FileDialog fd = new FileDialog(f,"文件另存为",FileDialog.SAVE); fd.setFile("*.txt"); fd.setVisible(true); String path = fd.getDirectory(); String fileName = fd.getFile(); if(fileName == null) return; try{
PrintWriter pw = new PrintWriter(new FileWriter(path+fileName)); fn = path + fileName; String content = ta.getText().replaceAll("\n", "\r\n"); pw.write(content,0,content.length()); pw.flush(); pw.close(); }catch(IOException ex) { //save file failed } return; } if(s.equals("关闭")){ ta.setText(""); fn = null; return; } if(s.equals("剪切")){ ta.cut(); return; } if(s.equals("复制")){ ta.copy(); return; } if(s.equals("粘帖")){ ta.paste(); return; } if(s.equals("改变字体大小")) { Object[] p= {"16","24","36"}; Object select=JOptionPane.showInputDialog(null, "您将要改变的字体为","输入", JOptionPane.INFORMATION_MESSAGE, null, p, p[1]); JOptionPane.showMessageDialog(null,"您的选择是:"+select.toString()); fontSize=Integer.parseInt(select.toString()); ta.setFont(new Font("Serif",mode,fontSize)); repaint();
} if(s.equals("改变字体样式")) { Object[] p= {"加粗","斜体","斜体并加粗","还原"}; Object select=JOptionPane.showInputDialog(null, "您将要改变的字体样式为","输入", JOptionPane.INFORMATION_MESSAGE, null, p, p[0]); JOptionPane.showMessageDialog(null,"您的选择是:"+select.toString()); mode=0; if(select.toString().equals("加粗")) mode +=Font.BOLD; if(select.toString().equals("斜体")) mode +=Font.ITALIC; if(select.toString().equals("斜体并加粗")) { mode +=Font.BOLD; mode +=Font.ITALIC; } if(select.toString().equals("还原")) mode=0; ta.setFont(new Font("Serif",mode,fontSize)); repaint(); } } } class Note{ public static void main(String[] args){ NoteFrame frm = new NoteFrame(); frm.setVisible(true); } } 四、实验结果测试、收获与体会:
在原有的基础上加入了对文本域文字编辑的菜单栏,在菜单栏中又加入改变字体大小和样式二个菜单项,并且通过监听器对二个菜单项做出有对话框的反应,然后进行输出,从该实验中学习到了swing高级组件的使用,对文本域,对话框等有了深度的学习。 |