原文:http://zhidao.baidu.com/question/159376210.html<wbr style="line-height:25px"><div style="line-height:25px"> <pre id="best-answer-content" style="line-height:22px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; font-family:Arial; white-space:pre-wrap; word-wrap:break-word; zoom:1"><span style="color:#3366ff; line-height:25px">示例1:</span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="font-family:Arial; line-height:25px"><span style="line-height:25px; white-space:pre-wrap"><span style="color:#3366ff; line-height:25px">import java.awt.*;</span></span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="font-family:Arial; line-height:25px"><span style="line-height:25px; white-space:pre-wrap"><span style="color:#3366ff; line-height:25px"> import javax.swing.*;</span></span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="font-family:Arial; line-height:25px"><span style="line-height:25px; white-space:pre-wrap"><span style="color:#3366ff; line-height:25px"> import javax.swing.event.*;</span></span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="font-family:Arial; line-height:25px"><span style="line-height:25px; white-space:pre-wrap"><span style="color:#3366ff; line-height:25px">public class Notepad1 { </span></span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="font-family:Arial; line-height:25px"><span style="line-height:25px; white-space:pre-wrap"><span style="color:#3366ff; line-height:25px">public static void main(String[] args) { </span><span style="color:#993300; line-height:25px"> </span></span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="font-family:Arial; line-height:25px"><span style="line-height:25px; white-space:pre-wrap"><span style="color:#993300; line-height:25px"> J</span></span></span><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#993300; line-height:25px">Frame jf=new JFrame();</span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#993300; line-height:25px"> JPanel jp=new JPanel();</span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#993300; line-height:25px"></span><span style="color:#3366ff; line-height:25px"> JMenuBar menuBar=new JMenuBar();</span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#3366ff; line-height:25px"> JMenu file=new JMenu("文件");</span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#3366ff; line-height:25px"> JMenuItem New=new JMenuItem("新建");</span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#3366ff; line-height:25px"> JMenuItem Open=new JMenuItem("打开");</span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#3366ff; line-height:25px"> JMenuItem Save=new JMenuItem("保存");</span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#3366ff; line-height:25px"> file.add(New);</span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#3366ff; line-height:25px"> file.add(Open);</span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#3366ff; line-height:25px"> file.add(Save);</span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#3366ff; line-height:25px"> JMenu edit=new JMenu("编辑");</span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#3366ff; line-height:25px"> JMenuItem Search=new JMenuItem("查找");</span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#3366ff; line-height:25px"> JMenuItem Change=new JMenuItem("替换");</span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#3366ff; line-height:25px"> edit.add(Search);</span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#3366ff; line-height:25px"> edit.add(Change);</span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#3366ff; line-height:25px"> JMenu g=new JMenu("格式");</span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#3366ff; line-height:25px"> JMenuItem Font=new JMenuItem("字体");</span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#3366ff; line-height:25px"> g.add(Font);</span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#3366ff; line-height:25px"> JMenu help=new JMenu("帮助");</span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#3366ff; line-height:25px"> JMenuItem About=new JMenuItem("关于");</span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#3366ff; line-height:25px"> JMenuItem Hel=new JMenuItem("帮助");</span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#3366ff; line-height:25px"> help.add(About);</span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#3366ff; line-height:25px"> help.add(Hel);</span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#3366ff; line-height:25px"> menuBar.add(file);</span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#3366ff; line-height:25px"> menuBar.add(edit);</span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#3366ff; line-height:25px"> menuBar.add(g);</span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#3366ff; line-height:25px"> menuBar.add(help);</span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#3366ff; line-height:25px"> </span><span style="color:#993300; line-height:25px"> JTextArea jt=new JTextArea();</span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#993300; line-height:25px"> jt.setLineWrap(true);</span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#993300; line-height:25px"> JScrollPane js=new JScrollPane(jt);</span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#993300; line-height:25px"> jp.setLayout(new GridLayout(1,1));//加上这句</span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#993300; line-height:25px"> jp.add(js);</span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#993300; line-height:25px"> jf.add(jp);</span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#993300; line-height:25px"></span><span style="color:#3366ff; line-height:25px"> jf.setJMenuBar(menuBar);</span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#3366ff; line-height:25px"> //jf.add(jt);去掉这句</span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#3366ff; line-height:25px"> jf.setSize(800,600);</span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#3366ff; line-height:25px"> jf.setVisible(true);</span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#3366ff; line-height:25px"> }</span></span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="line-height:25px; font-family:Arial; white-space:pre-wrap"><span style="color:#3366ff; line-height:25px"> }</span></span></pre> <pre id="best-answer-content" style="line-height:22px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; font-family:Arial; white-space:pre-wrap; word-wrap:break-word; zoom:1"><span style="line-height:25px">注1</span>:<span style="color:#000080; line-height:25px">在我的工具《</span><strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/171040579201132811432603/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">文件删除器</a></strong><span style="line-height:25px; color:rgb(0,0,128)">》中也使用到了它。可以进行参照。</span></pre> <pre id="best-answer-content" style="line-height:22px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; font-family:Arial; white-space:pre-wrap; word-wrap:break-word; zoom:1"><span style="line-height:25px">注2<span style="color:#000080; line-height:25px">:</span></span><span style="color:#000080; line-height:25px">自动移到最后,可以使用如下的代码:</span></pre> <pre id="best-answer-content" style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1"><span style="font-family:Arial; color:#ff6600; line-height:25px"><span style="line-height:25px; white-space:pre-wrap"> int len=jTextAreaTip.getText().length(); jTextAreaTip.setCaretPosition(len);</span></span></pre> </div> </wbr>