⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 textpanel.java

📁 PEPA模型性能分析工具
💻 JAVA
字号:
package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.Action;
import javax.swing.Box;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.text.DefaultEditorKit;
import javax.swing.text.PlainDocument;

public class TextPanel extends JPanel{
	private JTextArea content;//用户输入文本
	private JTextField line;//当前行数
	private JTextField column;//当前列数
	
	public TextPanel()
	{
		super();
		this.setLayout(new BorderLayout());

		content=new JTextArea();
		line=new JTextField();
		column=new JTextField();
		
		content.setEditable(false);
		
		Action[] editActions = new Action[4];

        editActions[0] = content.getActionMap().get(DefaultEditorKit.cutAction);
        editActions[0].putValue(Action.NAME,"剪切");
        editActions[1] = content.getActionMap().get(DefaultEditorKit.copyAction);
        editActions[1].putValue(Action.NAME,"复制");
        editActions[2] = content.getActionMap().get(DefaultEditorKit.pasteAction);
        editActions[2].putValue(Action.NAME,"粘贴");
        editActions[3] = content.getActionMap().get(DefaultEditorKit.selectAllAction);
        editActions[3].putValue(Action.NAME,"全选");
        String[] editToolTips = new String[]{
                "剪切当前选择文本放入剪切板",
                "复制当前选择文本放入剪切板",
                "剪切剪切板文本放入当前编辑器",
                "选择编辑器所有文本"
            };
        final JPopupMenu edit=new JPopupMenu("Edit");
        edit.setToolTipText("模型编辑面板选项");
        for(int i = 0;i<editActions.length;i++){
            edit.add(createMenuItem(editActions[i],editToolTips[i]));
        }
        
        JMenuBar menuBar=new JMenuBar();
        //添加行列显示
        menuBar.add(Box.createHorizontalStrut(20));
        menuBar.add(new JLabel("行: "));
        line = new JTextField("1",3);
        line.setFocusable(false);
        line.setMaximumSize(new Dimension(30,line.getPreferredSize().height));
        menuBar.add(line);
        menuBar.add(Box.createHorizontalStrut(20));
        menuBar.add(new JLabel("列: "));
        column = new JTextField("0",3);
        column.setFocusable(false);
        column.setMaximumSize(new Dimension(30,column.getPreferredSize().height));
        menuBar.add(column);
        this.add(menuBar,BorderLayout.NORTH);
        

		
		content.addCaretListener(new CaretListener(){

			public void caretUpdate(CaretEvent e) {
				// TODO Auto-generated method stub
				int offset=e.getDot();
				line.setText(Integer.toString(1+content.getDocument().getDefaultRootElement().getElementIndex(offset)));
				column.setText(Integer.toString(offset-((PlainDocument)content.getDocument()).getParagraphElement(offset).getStartOffset()));
				
			}
			
		});
		content.addMouseListener(new MouseAdapter(){
			 public void mouseClicked(MouseEvent e){ check(e);}            //单击
			 public void mouseEntered(MouseEvent e){ check(e);}            //鼠标进入到组件(进入菜单)
			 public void mouseExited(MouseEvent e){ check(e);}              //鼠标离开组件
			 public void mousePressed(MouseEvent e){ check(e);}             // 鼠标在组件上按下
			 public void mouseReleased(MouseEvent e){ check(e);}            //鼠标按钮在组件上释放
			private void check(MouseEvent e)
			{
				if(e.isPopupTrigger())
				{
					edit.show(content,e.getX(),e.getY());
				}
			}
		});
		
		JScrollPane scrollPane=new JScrollPane(content);
		this.add(scrollPane,BorderLayout.CENTER);
	}
    private JMenuItem createMenuItem(Action a, String tooltip){
        JMenuItem menu = new JMenuItem(a);
        menu.setToolTipText(tooltip);
        return menu;
    }
    public void setEditable(boolean f)
    {
    	content.setEditable(f);
    }
    public String getText()
    {
    	return content.getText();
    }
    public void setText(String value)
    {
    	content.setText(value);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -