java实验图形用户界面答案.docx
试验十二 图形用户界面(一)一、试验时间: 姓名: 学号: 二、 试验目的1、 驾驭常见容器类的用法2、 驾驭各种布局的用法;3、 驾驭常见组件的用法;4、 理解Java的事务处理机制;三、学问点1、常用容器;2、布局;3、常用组件;4、事务处理机制;四、试验内容与步骤1、程序功能是窗体中有一个按钮与一个文本框。当单击按钮时,文本框显示按钮的名字。如图1所示,请编程实现该功能。图1import javax.swing.*;import java.awt.event.*;import java.awt.*;public class xt0 public static void main(String args) new SimpleWin();class SimpleWin extends JFrame implements ActionListenerJTextField jf;JButton b;SimpleWin()jf=new JTextField();b=new JButton("确定");setLayout(new GridLayout(1,2);add(b);add(jf);b.addActionListener(this);setVisible(true);setBounds(100,100,200,100);setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);public void actionPerformed(ActionEvent e)String s=b.getActionCommand();jf.setText(s);2、程序功能是依据下拉列表所选定的字体大小与字体名称来设置文本框中的字体格式。如图2所示,此时文本框中的字体大小为20,名称为Arial Black。请编程实现该功能。图2import javax.swing.*;import java.awt.*;import java.awt.event.*;public class FontChange implements ItemListener JFrame jf=new JFrame("字体设置程序"); JTextField tf=new JTextField("how do you do!"); JComboBox cb1; JComboBox cb2; String name="Times New Roman" int fontsize=12;public void init()String fontSize="8","10","12","14","16","18","20","22","26","32","36","40"cb1=new JComboBox(fontSize);GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();String fontName=ge.getAvailableFontFamilyNames();cb2=new JComboBox(fontName);cb1.addItemListener(this);cb2.addItemListener(this);JPanel p=new JPanel();p.setLayout(new GridLayout(1,2);p.add(cb1);p.add(cb2);jf.setLayout(new GridLayout(2,1);jf.add(tf);jf.add(p);jf.setBounds(300, 300, 400, 150);jf.setVisible(true);public void itemStateChanged(ItemEvent e)if(e.getSource()=cb1)if(e.getStateChange()=ItemEvent.SELECTED)String s=cb1.getSelectedItem().toString();fontsize=Integer.parseInt(s);tf.setFont(new Font(name,Font.BOLD,fontsize);else if(e.getSource()=cb2)if(e.getStateChange()=ItemEvent.SELECTED)name=cb2.getSelectedItem().toString();tf.setFont(new Font(name,Font.BOLD,fontsize);public static void main(String args) new FontChange().init();3、程序功能是依据所输入的长、宽数值,计算长方形的周长与面积,结果保存3位小数。假设输入非数字,则提示输入错误,重新输入。如图3所示。请编程实现该功能。图3/实体类public class Rec private double chang;private double kuan;public double getChang() return chang;public void setChang(double chang) this.chang = chang;public double getKuan() return kuan;public void setKuan(double kuan) this.kuan = kuan;/业务类public class RecYW public double getArea(Rec c)return c.getChang()*c.getKuan();public double getZC(Rec c)return (c.getChang()+c.getKuan()*2;/制作窗体类import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.text.NumberFormat;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JTextField;public class WinRec implements ActionListenerJButton b1=new JButton("确定"); JButton b2=new JButton("退出"); JFrame jf=new JFrame("长方形计算器"); JTextField tf1=new JTextField(); JTextField tf2=new JTextField(); JLabel jl3=new JLabel();/用于存储周长 JLabel jl5=new JLabel();/用于存储面积public void init() jf.setLayout(new GridLayout(5,2); JLabel jl1=new JLabel("输入长:"); JLabel jl6=new JLabel("输入宽:"); JLabel jl2=new JLabel("圆的周长:"); JLabel jl4=new JLabel("圆的面积:"); b1.addActionListener(this); b2.addActionListener(this); jf.add(jl1); jf.add(tf1); jf.add(jl6); jf.add(tf2); jf.add(jl2); jf.add(jl3); jf.add(jl4); jf.add(jl5); jf.add(b1); jf.add(b2); jf.setBounds(300,300,250,200); jf.setVisible(true);public void actionPerformed(ActionEvent e)if(e.getSource()=b1)String chang=tf1.getText().trim();String kuan=tf2.getText().trim();trydouble chang_1=Double.parseDouble(chang);double kuan_1=Double.parseDouble(kuan);Rec c=new Rec();c.setChang(chang_1);c.setKuan(kuan_1);RecYW mac=new RecYW();double zhouchang,mianji;zhouchang=mac.getArea(c); mianji=mac.getZC(c); NumberFormat f=NumberFormat.getInstance(); f.setMaximumFractionDigits(3); jl3.setText(f.format(zhouchang); jl5.setText(f.format(mianji);catch(Exception e1)JOptionPane.showMessageDialog(jf, "请输入一个数字!");tf1.setText(null);tf2.setText(null);jl3.setText(null);jl5.setText(null);else if(e.getSource()=b2)System.exit(0);/测试类public class TestRecWin public static void main(String args) new WinRec().init();4、程序功能是当点击菜单项“翻开文件”时,弹出文件对话框,用户从文件对话框中选择须要翻开的文件,然后在文本区中显示文件内容。如图4所示。请编程实现该功能。图4import java.awt.event.*;import javax.swing.*;import java.io.*;public class xt2 public static void main(String args) OpenWindow win=new OpenWindow();class OpenWindow extends JFrame implements ActionListenerJMenuBar menubar;JMenu menu;JMenuItem itemOpen;JTextArea text1;OpenWindow()menubar=new JMenuBar();menu=new JMenu("翻开");itemOpen=new JMenuItem("翻开文件");menu.add(itemOpen);menubar.add(menu);setJMenuBar(menubar);text1=new JTextArea();add(new JScrollPane(text1);setSize(300,300);setVisible(true);validate();itemOpen.addActionListener(this);public void actionPerformed(ActionEvent e)text1.setText(null);J new J("d:/lesson");int n=(this);if(n=J.APPROVE_OPTION)try read (file);BufferedReader in=new BufferedReader(readfile);String s=null;while(s=in.readLine()!=null)text1.append(s+"n");catch(IOException e1)e1.getMessage();五、试验后记收获与体会出现的错误与解决(概述)老师评语第 10 页