java基础记事本代码 java简易记事本( 六 )


* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setBounds(new Rectangle(15, 46, 371, 225));
jScrollPane.setViewportView(getJTextArea());
}
return jScrollPane;
}
/**
* This method initializes jTextArea
*
* @return javax.swing.JTextArea
*/
private JTextArea getJTextArea() {
if (jTextArea == null) {
jTextArea = new JTextArea();
}
return jTextArea;
}
/**
* This method initializes jMenuItem4
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem4() {
if (jMenuItem4 == null) {
jMenuItem4 = new JMenuItem();
jMenuItem4.setText("复制");
jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
MainFrame.this.setClipboardText(MainFrame.this.getToolkit().getSystemClipboard(),jTextArea.getSelectedText());
}
});
}
return jMenuItem4;
}
/**
* This method initializes jMenuItem5
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem5() {
if (jMenuItem5 == null) {
jMenuItem5 = new JMenuItem();
jMenuItem5.setText("剪切");
jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
MainFrame.this.setClipboardText(MainFrame.this.getToolkit().getSystemClipboard(),jTextArea.getSelectedText());
jTextArea.setText(jTextArea.getText().substring(0,jTextArea.getSelectionStart()));
}
});
}
return jMenuItem5;
}
/**
* This method initializes jMenuItem6
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem6() {
if (jMenuItem6 == null) {
jMenuItem6 = new JMenuItem();
jMenuItem6.setText("黏贴");
jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
try{
jTextArea.setText(jTextArea.getText().substring(0,jTextArea.getSelectionStart()));
jTextArea.setText(jTextArea.getText()+(MainFrame.this.getClipboardText(MainFrame.this.getToolkit().getSystemClipboard())));
}
catch(Exception ee){
ee.printStackTrace();
}
}
});
}
return jMenuItem6;
}
/**
* This method initializes jMenuItem7
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem7() {
if (jMenuItem7 == null) {
jMenuItem7 = new JMenuItem();
jMenuItem7.setText("关于记事本");
jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
////暂无代码?。?
}
});
}
return jMenuItem7;
}
public static void main(String args[]){
new MainFrame();
}
public static MainFrame getMyMainFrame() {
return myMainFrame;
}
public static void setMyMainFrame(MainFrame myMainFrame) {
MainFrame.myMainFrame = myMainFrame;
}
public static String getTextstr() {
return textstr;
}
public static void setTextstr(String textstr) {
MainFrame.textstr = textstr;
}
protected static String getClipboardText(Clipboard clip) throws Exception{
Transferable clipT = clip.getContents(null);// 获取剪切板中的内容
if (clipT != null) {
if (clipT.isDataFlavorSupported(DataFlavor.stringFlavor)) // 检查内容是否是文本类型
return (String)clipT.getTransferData(DataFlavor.stringFlavor);
}
return null;
}
protected static void setClipboardText(Clipboard clip, String writeMe) {

推荐阅读