在实验课上留了一个小作业,要求完成一个类似文本编辑器的小窗口。
效果如下:
代码如下:
package HomeWork;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Label;
import java.awt.TexturePaint;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.jws.soap.SOAPBinding.Style;
import javax.swing.*;
import java.awt.*;
public class Menu extends JFrame implements ActionListener,MouseListener{
JMenuBar jMenuBar1; // 菜单栏
JMenu jMenu1,jMenu2; // 菜单
JMenuItem jMenuItem1,jMenuItem2,jMenuItem3; //菜单项
JTextArea jTextArea; // 编辑文本
JComboBox<Integer> jComboBox; // 数字
JToolBar jToolBar; // 工具栏
JCheckBox jCheckBox1,jCheckBox2; // 复选框
JRadioButton jRadioButton1,jRadioButton2 , jRadioButton3; // 单选框
Label label ;
JPopupMenu jPopupMenu ; // 快捷菜单栏
public Menu() {
// TODO Auto-generated constructor stub
this.setSize(600,500);
this.setLocationRelativeTo(null);
Container container = this.getContentPane();
Font font = new Font("楷体",Font.PLAIN,30);
jMenuBar1 = new JMenuBar();
this.setJMenuBar(jMenuBar1); // 添加菜单栏用set不用add
// 将菜单添加到菜单栏中
jMenu1 = new JMenu("文件");
jMenuBar1.add(jMenu1);
jMenu2 = new JMenu("编辑");
jMenuBar1.add(jMenu2);
// 在第一个菜单中添加菜单项
jMenuItem1 = new JMenuItem("打开");
jMenu1.add(jMenuItem1);
jMenuItem1.addActionListener(this);
jMenuItem2 = new JMenuItem("新建");
jMenu1.add(jMenuItem2);
jMenuItem3 = new JMenuItem("退出");
jMenuItem3.addActionListener(this);
jMenu1.add(jMenuItem3);
jToolBar = new JToolBar(); // 新建一个工具栏
label = new Label("字体大小:");
jToolBar.add(label);
// 新建一个数字框
jComboBox = new JComboBox<Integer>();
for(int i= 10;i<=100;i+=10)
jComboBox.addItem(i);
jComboBox.addActionListener(this);
jToolBar.add(jComboBox);
container.add(jToolBar,"North"); // 设置工具栏的位置Frame 默认为边界布局
// 将字体的形状添加的工具栏中
jCheckBox1 = new JCheckBox("粗体");
jToolBar.add(jCheckBox1);
jCheckBox1.setFont(new Font("楷体",Font.BOLD,20));;
jCheckBox1.addActionListener(this);
jCheckBox2 = new JCheckBox("斜体");
jCheckBox2.setFont(new Font("楷体",Font.ITALIC,20));;
jToolBar.add(jCheckBox2);
jCheckBox2.addActionListener(this);
// 新建一个按钮组来使一些单选
ButtonGroup buttonGroup = new ButtonGroup();
jRadioButton1 = new JRadioButton("红色");
jRadioButton1.setForeground(Color.RED.brighter());
jRadioButton1.setFont(font);
jToolBar.add(jRadioButton1);
jRadioButton1.addActionListener(this);
buttonGroup.add(jRadioButton1);
jRadioButton2 = new JRadioButton("绿色");
jRadioButton2.setForeground(Color.GREEN.brighter());
jRadioButton2.setFont(font);
jToolBar.add(jRadioButton2);
jRadioButton2.addActionListener(this);
buttonGroup.add(jRadioButton2);
jRadioButton3 = new JRadioButton("蓝色");
jRadioButton3.setForeground(Color.BLUE.brighter());
jRadioButton3.setFont(font);
jToolBar.add(jRadioButton3);
jRadioButton3.addActionListener(this);
buttonGroup.add(jRadioButton3);
jTextArea = new JTextArea();
JScrollPane jScrollPane = new JScrollPane(); // 新建一个滚动条
jTextArea.setFont(font);
jScrollPane.setViewportView(jTextArea); // 添加textarea 但是不能用add 用set
jScrollPane.setSize(10,20);
container.add(jScrollPane); // 最后直接将滚动条添加到容器中
jTextArea.setLineWrap(true);
jPopupMenu = new JPopupMenu();
jMenuItem1 = new JMenuItem("复制");
jMenuItem2 = new JMenuItem("粘贴");
jMenuItem3 = new JMenuItem("剪切");
jPopupMenu.add(jMenuItem1);
jMenuItem1.addActionListener(this);
jPopupMenu.add(jMenuItem2);
jMenuItem2.addActionListener(this);
jPopupMenu.add(jMenuItem3);
jMenuItem3.addActionListener(this);
jTextArea.add(jPopupMenu);
jTextArea.addMouseListener(this);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Menu();
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
//TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
if(e.getButton()==3)
{
jPopupMenu.show(jTextArea, e.getX(), e.getY());
}
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand().equals("退出"))
System.exit(0);
if(e.getActionCommand().equals("打开"))
{
JFileChooser jFileChooser = new JFileChooser();
jFileChooser.showOpenDialog(this);
}
if(e.getSource() instanceof JComboBox)
{
Integer integer = (Integer)jComboBox.getSelectedItem();
Font font = jTextArea.getFont();
jTextArea.setFont(new Font(font.getFontName(),font.getStyle(),integer));
}
if(e.getSource() instanceof JCheckBox)
{
Font font = jTextArea.getFont();
int style = font.getStyle();
if(e.getActionCommand().equals("粗体"))
style = style ^ Font.BOLD;
if(e.getActionCommand().equals("斜体"))
style = style ^ Font.ITALIC;
jTextArea.setFont(new Font(font.getFontName(),style,font.getSize()));
}
if(e.getActionCommand().equals("红色"))
jTextArea.setForeground(Color.red);
if(e.getActionCommand().equals("蓝色"))
jTextArea.setForeground(Color.blue);
if(e.getActionCommand().equals("绿色"))
jTextArea.setForeground(Color.green); // 部分程序未添加事件监听器
if(e.getActionCommand().equals("复制"))
jTextArea.copy();
if(e.getActionCommand().equals("粘贴"))
jTextArea.paste();
if(e.getActionCommand().equals("剪切"))
jTextArea.cut();
}
}