📄 j10_15.java
字号:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class j10_12 extends JFrame implements ActionListener
{
JTextArea tf=new JTextArea( );
JMenuBar bar=new JMenuBar( ); //创建JMenuBar对象
JMenu menu=new JMenu("文件");
JMenu menu2=new JMenu("图形");//创建JMenu对象
JMenuItem newf=new JMenuItem("新建"); //创建JMenuItem对象
JMenuItem open=new JMenuItem("打开");
JMenuItem close=new JMenuItem("关闭");
JMenuItem quit=new JMenuItem("退出");
JMenuItem circle=new JMenuItem("圆形");
JMenuItem rope=new JMenuItem("直线");
JTextArea theArea = new JTextArea( );
public j10_12( )
{ super("j10_12");
getContentPane( ).setLayout(new GridLayout(1,2));
getContentPane( ).add(new JScrollPane(tf)); //创建JFrame的容器对象
tf.setEditable(false); //设置文本区域不可编辑
bar.setOpaque(true); //设置bar为不透明,若设置bar为透明,则在选择菜单时
//会有残影存留在JMenuBar上
theArea.setEditable(true); //设置对多行编辑器类对象允许还是不允许修改。
this.getContentPane().add(new JScrollPane(theArea));//以文本域对象创建滚动条面板类的匿名实例,并将其加到框架的内容面板上。
setJMenuBar(bar); //加入bar到Jframe中
menu.add(newf); //加入JMenuItem对象到menu中
menu.add(open);
menu.add(close);
menu.addSeparator( ); //在JMenu中加入一分隔线
menu.add(quit);
menu2.add(circle);
menu2.add(rope);
bar.add(menu);
bar.add(menu2);//将menu加载到bar上
newf.addActionListener(this); //注册JMenuItem对象给监听者对象
open.addActionListener(this);
close.addActionListener(this);
quit.addActionListener(this);
circle.addActionListener(new ActionListener() { /*36-39行为实例open安装监视器,并实现接口的方法,当事件发生时执行该方法。*/
public void actionPerformed(ActionEvent e) {
theArea.append("circle");
}});
rope.addActionListener(new ActionListener() { //同上
public void actionPerformed(ActionEvent e) {
theArea.append("-rope -\n");
}});
addWindowListener(new WinLis( ));
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource( )==newf)tf.setText("新建");
if(e.getSource( )==open)tf.setText("打开");
if(e.getSource( )==close)tf.setText("关闭");
if(e.getSource( )==quit)System.exit(0);
}
class WinLis extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{ System.exit(0); }
}
public static void main(String[ ] args)
{
JFrame f = new j10_12( );
f.setSize(1000,200);
f.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -