}//JMenuItem1类结束
25设计一程序,实现一个对文件进行操作的窗口,在该窗口中提供一个菜单栏和工具栏,在菜单栏中要求有对文件的新建、打开、关闭、保存、另存和退出,在工具栏中要求有新建、打开、关闭和字符加重、倾斜、下划线和字体选择等图形按钮工具。(菜单和工具栏中的事件处理只要求在按钮被按下之后在窗口给出提示。)
import javax.swing.*; import java.awt.*;
import java.awt.event.*;
public class MyJToolBar extends JFrame{ //声明实现的类,继承父类JFrame.
JTextArea theArea = null; //声明一文本域类对象并赋与空值。 static final String ComboStr[] = {\New Roman\宋体\黑体\楷体\
//定义一个静态final型的字符串数组,并赋值。该数组是工具栏中的下拉式列表中内容
public MyJToolBar(){ //定义该类的构造方法
super(\ //调用副类的方法 在窗口标题栏中显示该字符串
theArea = new JTextArea(); //创建多行文本编辑器(文本域)类的实例。
theArea.setEditable(true); //设置对多行编辑器类对象允许还是不允许修改。 this.getContentPane().add(new JScrollPane(theArea));//以文本域对象创建滚动条面板类的匿名实例,并将其加到框架的内容面板上。
JMenuBar MBar = new JMenuBar(); //声明创建菜单栏实例 MBar.setOpaque(true); //设置菜单栏为白底色
JMenu mfile = buildFileMenu(); //声明创建菜单实例 JMenu mfile1 = buildFileMenu(); //声明创建菜单实例 //菜单
JToolBar theBar = buildToolBar(); //声明创建菜单项实例 //工具条
this.getContentPane().add(theBar,BorderLayout.SOUTH); //将菜单栏家道内容面板中
MBar.add(mfile); //将菜单mfile加到菜单栏上。 MBar.add(mfile1); //将菜单mfile加到菜单栏上。 setJMenuBar(MBar);//必须调用该方法才能将菜单显示 } //JToolBar1()构造方法结束
31
public JMenu buildFileMenu() { //构造菜单类的文件菜单的创建方法 JMenu thefile = new JMenu(\
thefile.setMnemonic('e'); //设置菜单的快捷方式为按F键 Alt+F //Mnemonic---助记
JMenuItem newf = new JMenuItem(\创建图形菜单项
JMenuItem open = new JMenuItem(\ JMenuItem close= new JMenuItem(\
JMenuItem quit = new JMenuItem(\
newf.setMnemonic('N'); //28-31行为菜单项设置快捷键 open.setMnemonic('O'); close.setMnemonic('L');
quit.setMnemonic('X');//32-35行在菜单项旁说明什么组合键激活该菜单项
newf.setAccelerator(KeyStroke.getKeyStroke('N', java.awt.Event.CTRL_MASK, false) ); open.setAccelerator(KeyStroke.getKeyStroke('O', java.awt.Event.CTRL_MASK, false) ); close.setAccelerator(KeyStroke.getKeyStroke('L', java.awt.Event.CTRL_MASK, false) ); quit.setAccelerator( KeyStroke.getKeyStroke('X', java.awt.Event.CTRL_MASK, false) );
newf.addActionListener(new ActionListener() { /*36-39行为实例newf安装监视器,并实现接口的方法,当事件发生时执行该方法。*/
public void actionPerformed(ActionEvent e) { theArea.setText(\ } });
open.addActionListener(new ActionListener() { /*36-39行为实例open安装监视器,并实现接口的方法,当事件发生时执行该方法。*/
public void actionPerformed(ActionEvent e) {
theArea.append(\您选择了\\\打开\\\文件菜单项-\\n\ }});
close.addActionListener(new ActionListener() { //同上 public void actionPerformed(ActionEvent e) {
theArea.append(\ }});
quit.addActionListener(new ActionListener() { //同上 public void actionPerformed(ActionEvent e) { System.exit(0); }});
32
thefile.add(newf); thefile.add(open); thefile.add(close); thefile.addSeparator();
thefile.add(quit); //52-52行将各菜单项加到菜单实例中 return thefile; //返回菜单的值
} //buildFileMenu()方法结束
public JToolBar buildToolBar() { //构造工具栏创建方法 JToolBar toolBar = new JToolBar();
toolBar.setFloatable(true);//设置工具栏为浮动。
/*60-62行创建3个工具栏动作事件处理类的组件,该类可以由读者自己来写,为该题编写的该类和接口、方法在103-111行,这个类继承了AbstractAction抽象类,实现接口Action interface,而Action interface又继承ActionListener 接口,因此可以在该类
中直接覆写actionPerformed()方法来处理事件。*/
JToolBarAction tba_new = new JToolBarAction(\ImageIcon(\
JToolBarAction tba_open = new JToolBarAction(\ImageIcon(\
JToolBarAction tba_close = new JToolBarAction(\ //64-70
JButton JB; //声明JB是按钮类的对象
JB = toolBar.add(tba_new); // 将tba_new实例加到工具栏上,并将该值赋给按钮对象 JB.setActionCommand(\//为工具栏按钮设置执行动作行为的名称。 JB = toolBar.add(tba_open);
JB.setActionCommand(\
JB = toolBar.add(tba_close);
JB.setActionCommand(\
toolBar.addSeparator(); //加入一个分隔符
JToolBarAction tba_B = new JToolBarAction(\ JToolBarAction tba_I = new JToolBarAction(\ JToolBarAction tba_U = new JToolBarAction(\
ImageIcon(\行创建ToolBarAction三个实例 JB = toolBar.add(tba_B);
JB.setActionCommand(\你选择了将文字格式化为粗体字!\ JB = toolBar.add(tba_I);
JB.setActionCommand(\你选择了将文字格式化为斜体字!\ JB = toolBar.add(tba_U);
33
JB.setActionCommand(\你选择了将文字格式化为带下划线的文字!\
toolBar.addSeparator(); //加入分隔符
JLabel JLfont = new JLabel(\ //创建标签实例JLfort
toolBar.add(JLfont); //将标签加到工具栏上去 toolBar.addSeparator();
JComboBox jcb = new JComboBox(ComboStr); //创建下拉列表实例
jcb.addActionListener(new ActionListener() { //为下拉列表安装监视器
public void actionPerformed(ActionEvent e){ //用匿名方式实现接口方法 theArea.append(\
\ //在窗口中显示工具栏或菜单栏中动作行为的信息。
}}); //得到选择的项
toolBar.add(jcb); //把jcb加到工具栏上 return toolBar; //返回tooBar方法的值
} //buildToolBar()方法结束 public static void main(String[] args){ //声明主方法 JFrame F = new MyJToolBar(); //创建容器框架实例 F.setSize(430,200); //设置窗口大小 F.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); }});
F.setVisible(true);
} //main()方法结束
class JToolBarAction extends AbstractAction{ //声明JtoolBarAction类和接口 public JToolBarAction(String name,Icon icon){ //构造该类的构造方法
super(name,icon);} //调用父类的方法,传递的参数是字符串name和图形icon
public void actionPerformed(ActionEvent e){ try{
theArea.append(e.getActionCommand()+\
// 得到动作命令字符串 }
catch(Exception ex){} }
} // JtoolBarAction类结束
34
} // JToolBar1类结束
26. 设计一程序,创建一个用户界面,设计一块显示图片的空间及“开始”与“停止”两个按钮,当你按下“开始”按钮时连续显示10次图片,同时有一个进度条显示进度,显示的速度可以通过滑杆调节。
import javax.swing.*;
import javax.swing.border.*; import java.awt.*;
import java.awt.event.*; import javax.swing.event.*;
public class MyProgressMonitor implements ActionListener,ChangeListener {
JFrame f = null; //创建全局的类对象和变量,先赋空值,以备在各个方法中使用; Timer timer = null;//计时器 ImageIcon[] icons = null;
JSlider slider = null;//滑动杆 JLabel label = null;
int index=0;//图片数组的索引 int total=0;//图片的总数
ProgressMonitor pMonitor;
public MyProgressMonitor() { //写该类的构造方法: f = new JFrame(\进度条例题\
Container contentPane = f.getContentPane();
icons = new ImageIcon[10]; for (int i=0 ; i<10 ; i++)
icons[i] = new ImageIcon((i+1)+\ label = new JLabel(icons[0]);
//实例化滑动杆对象:为水平放置,最小值0,最大值100,初始设置为50; slider = new JSlider(JSlider.HORIZONTAL,0, 100,50);
slider.setPaintTicks(true); //进度条刻度显示方式为True,下面的两个语句才起作用
slider.setMajorTickSpacing(20); slider.setMinorTickSpacing(5); slider.setPaintLabels(true); slider.addChangeListener(this); JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1,2)); JButton b1 = new JButton(\
35
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库《Java程序设计》复习题(7)在线全文阅读。
相关推荐: