大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
天啊, 冖_Na0 为什么会有我编的记事本代码呢???呵呵……你肯定是“请教”过我的吧??
成都创新互联公司为企业级客户提高一站式互联网+设计服务,主要包括网站设计制作、成都网站建设、成都app软件开发、重庆小程序开发公司、宣传片制作、LOGO设计等,帮助客户快速提升营销能力和企业形象,创新互联各部门都有经验丰富的经验,可以确保每一个作品的质量和创作周期,同时每年都有很多新员工加入,为我们带来大量新的创意。
呵呵……我自己编了一个,不过呢,没有windows那么多的功能啊。
涉及到两个文件:
第一个文件中的代码:
package MyProject;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.datatransfer.*;
import java.io.*;
public class MainFrame extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JMenuBar jJMenuBar = null;
private JMenu jMenu = null;
private JMenu jMenu1 = null;
private JMenu jMenu2 = null;
private JMenuItem jMenuItem = null;
private JMenuItem jMenuItem1 = null;
private JMenuItem jMenuItem2 = null;
private JMenuItem jMenuItem3 = null;
private JLabel jLabel = null;
private JScrollPane jScrollPane = null;
private JTextArea jTextArea = null;
private JMenuItem jMenuItem4 = null;
private JMenuItem jMenuItem5 = null;
private JMenuItem jMenuItem6 = null;
private JMenuItem jMenuItem7 = null;
private static MainFrame myMainFrame=null;
private static String textstr="";///用于记录文本文件的路径
private File myFile=null;
private FileReader myrder=null;
private FileWriter mywr=null;
/**
* This is the default constructor
*/
public MainFrame() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(412, 350);
this.setJMenuBar(getJJMenuBar());
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowActivated(java.awt.event.WindowEvent e) {
if(!textstr.equals("")){
try{
myFile=new File(textstr);
if(!myFile.exists()){
myFile.createNewFile();
}
myrder=new FileReader(myFile);
char[] mychar=new char[(int)myFile.length()];
myrder.read(mychar);
String tmp=new String(mychar);
jTextArea.setText(tmp);
myrder.close();
}
catch(Exception ee){
ee.printStackTrace();
}
}
}
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
this.setVisible(true);
myMainFrame=this;
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(15, 18, 65, 18));
jLabel.setText("文件内容:");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jLabel, null);
jContentPane.add(getJScrollPane(), null);
}
return jContentPane;
}
/**
* This method initializes jJMenuBar
*
* @return javax.swing.JMenuBar
*/
private JMenuBar getJJMenuBar() {
if (jJMenuBar == null) {
jJMenuBar = new JMenuBar();
jJMenuBar.add(getJMenu());
jJMenuBar.add(getJMenu1());
jJMenuBar.add(getJMenu2());
}
return jJMenuBar;
}
/**
* This method initializes jMenu
*
* @return javax.swing.JMenu
*/
private JMenu getJMenu() {
if (jMenu == null) {
jMenu = new JMenu();
jMenu.setText("文件");
jMenu.add(getJMenuItem());
jMenu.add(getJMenuItem1());
jMenu.add(getJMenuItem2());
jMenu.add(getJMenuItem3());
}
return jMenu;
}
/**
* This method initializes jMenu1
*
* @return javax.swing.JMenu
*/
private JMenu getJMenu1() {
if (jMenu1 == null) {
jMenu1 = new JMenu();
jMenu1.setText("编辑");
jMenu1.add(getJMenuItem4());
jMenu1.add(getJMenuItem5());
jMenu1.add(getJMenuItem6());
}
return jMenu1;
}
/**
* This method initializes jMenu2
*
* @return javax.swing.JMenu
*/
private JMenu getJMenu2() {
if (jMenu2 == null) {
jMenu2 = new JMenu();
jMenu2.setText("帮助");
jMenu2.add(getJMenuItem7());
}
return jMenu2;
}
/**
* This method initializes jMenuItem
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem() {
if (jMenuItem == null) {
jMenuItem = new JMenuItem();
jMenuItem.setText("打开");
jMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
MainFrame.this.myMainFrame.setEnabled(false);
SelectTextFile mysl=new SelectTextFile();
mysl.setVisible(true);
}
});
}
return jMenuItem;
}
/**
* This method initializes jMenuItem1
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem1() {
if (jMenuItem1 == null) {
jMenuItem1 = new JMenuItem();
jMenuItem1.setText("关闭");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
try{
myFile=null;
}
catch(Exception ee){
ee.printStackTrace();
}
jTextArea.setText("");
}
});
}
return jMenuItem1;
}
/**
* This method initializes jMenuItem2
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem2() {
if (jMenuItem2 == null) {
jMenuItem2 = new JMenuItem();
jMenuItem2.setText("保存");
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
try{
String tmp=jTextArea.getText();
char[] mychar=tmp.toCharArray();
myFile.delete();
myFile.createNewFile();
mywr=new FileWriter(myFile);
mywr.write(mychar);
mywr.close();
}
catch(Exception ee){
ee.printStackTrace();
}
}
});
}
return jMenuItem2;
}
/**
* This method initializes jMenuItem3
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem3() {
if (jMenuItem3 == null) {
jMenuItem3 = new JMenuItem();
jMenuItem3.setText("退出");
jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.exit(0);
}
});
}
return jMenuItem3;
}
/**
* 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) {
Transferable tText = new StringSelection(writeMe);
clip.setContents(tText, null);
}
} // @jve:decl-index=0:visual-constraint="10,10"
第二个文件中的代码:
(太长了,贴不上来)
整个效果就是实现了基本的打开、关闭、保存、退出的功能。
效果如图:
package threadgroup;
class ThreadDemo3 extends Thread {
private String name;
private int delay;
public ThreadDemo3(String sname, int i_delay) {
name = sname;
delay = i_delay;
}
public void run() {
try {
sleep(delay);
} catch (InterruptedException e) {
}
System.out.println("多线程测试!\n" + name + "\n" + delay);
}
}
public class testMyThread {
public static void main(String[] args) {
ThreadDemo3 th1,th2,th3;
th1 = new ThreadDemo3("线程1", (int) (Math.random() * 900));
th2 = new ThreadDemo3("线程2", (int) (Math.random() * 900));
th3 = new ThreadDemo3("线程3", (int) (Math.random() * 900));
th1.start();
th2.start();
th3.start();
}
}
package threadgroup;
public class threadDemo {
public static void main(String[] args) {
Thread t = Thread.currentThread();
t.setName("你好吗?");
System.out.println("正在进行的Thread是:" + t);
try {
for (int i = 0; i 5; i++) {
System.out.println("我不叫穆继超" + i);
Thread.sleep(3000);
}
} catch (Exception e) {
// TODO: handle exception
System.out.println("Thread has wrong" + e.getMessage());
}
}
}
package threadgroup;
public class threadDemo2 implements Runnable {
public threadDemo2() {
Thread t1 = Thread.currentThread();
t1.setName("第一个主进程");
System.out.println("正在运行" + t1);
Thread t2 = new Thread(this, "");
System.out.println("在创建一个进程");
t2.start();
try {
System.out.println("使他进入第一个睡眠状态");
Thread.sleep(2000);
} catch (InterruptedException e) {
System.out.println("Thread has wrong" + e.getMessage());
}
System.out.println("退出第一个进程");
}
public void run() {
try {
for (int i = 0; i 5; i++) {
System.out.println("进程" + i);
Thread.sleep(3000);
}
} catch (InterruptedException e) {
// TODO: handle exception
System.out.println("Thread has wrong" + e.getMessage());
}
System.out.println("退出第二个进程");
}
public static void main(String[] args) {
new threadDemo2();
}
}
import java.util.Scanner;
public class colPrice{
public static void main(String[] args){
double[] price = new double[10];
double totalPrice = 0;
System.out.println("请输入商品价格:");
for(int i=0;i10;i++){
System.out.print(i+1+": ");
Scanner in = new Scanner( System.in);
price[i] = in.nextDouble();
}
System.out.println("");
for(int i= 0;iprice.length;i++){
totalPrice+=price[i];
}
System.out.println("商品总价格是:"+totalPrice);
}
}
1、Eclipse - IBM甩出来给开源社区的IDE,其本身就是使用Java开发的。\x0d\x0a2、MyEclipse - 把Eclipse包装了一下,加了J2EE企业开发的许多功能强大的插件。\x0d\x0a3、NetBeans - SUN公司自己开发的Java开发环境,功能挺多的。\x0d\x0a4、还有一些超喜欢手工编写Java代码的牛人喜欢使用带语法着色功能的纯文本编辑器编写Java程序,代码写完后在命令提示符下编译运行。这种方式只适用写少量代码或作为学习使用,开发大型项目不适用,因为效率很低。