资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java如何写源代码,java源代码怎么用

JAVA源代码的编写

import java.util.Date;

成都创新互联公司长期为上千多家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为肥东企业提供专业的成都做网站、网站设计,肥东网站改版等技术服务。拥有十余年丰富建站经验和众多成功案例,为您定制开发。

public class Travel{

TravelLeg mid = new TravelLeg();

private String start;

private String end;

private Date startTime;

private Date endTime;

private String travelId;

private String comment;

public void add(TravelLeg mid){

this.start = mid.getStart();

this.mid = mid;

this.end = mid.getEnd();

}

public boolean check_date(){

if(!mid.getStartTime().before(mid.getEndTime())){

System.out.println("时间错误");

return false;

}else return true;

}

public void print(){

System.out.println("开始地点:"+this.start+"中间地点"+this.mid.getEnd()+this.end);

}

}

用JAVA写出源代码。运行结果如下?

您好:

代码及运行结果如下,供参考:

代码示意图

运行结果示意图

因输出的内容中包含特殊字符,所以在输出的时候,需要做转义,以上代码供参考。

java用什么软件编写代码

JAVA编程常用的软件:

1、Eclipse:

Eclipse 是一个开放源代码的、基于 Java 的可扩展开发平台。就其本身而言,它只是一个框架和一组服务,用于通过插件组件构建开发环境。幸运的是,Eclipse 附带了一个标准的插件集,包括 Java 开发工具(Java Development Tools,JDT)。

2、MyEclipse:

MyEclipse是Eclipse的升级版,也是一款功能强大的J2EE集成开发环境,由Genuitec公司发布,提供免费版和收费版。被誉为最好用的Java IDE之一。

MyEclipse 是对Eclipse IDE的扩展,利用它可以在数据库和JavaEE的开发、发布以及应用程序服务器的整合方面极大的提高工作效率。

3、IntelliJ IDEA:

IntelliJ IDEA是一款综合的Java 编程环境,被许多开发人员和行业专家誉为市场上最好用的IDE之一,与MyEclipse齐名。

它提供了一系列最实用的的工具组合:智能编码辅助和自动控制,支持J2EE,Ant,JUnit和CVS集成,非平行的编码检查和创新的GUI设计器。

4、NetBeans:

NetBeans IDE是一个屡获殊荣的集成开发环境,可以方便的在Windows、Mac、Linux和Solaris中运行。NetBeans包括开源的开发环境和应用平台,NetBeans IDE可以使开发人员利用Java平台能够快速创建Web、企业、桌面以及移动的应用程序。

5、BlueJ:

BlueJ是一款支持Java编程语言的集成开发环境(IDE)。它原本是为了教育目的而开发的,同时也适合于那些想做小型软件开发的开发人员。它的运行需要JDK(Java开发工具包)的帮助。BlueJ主要是为面向对象的程序设计教学而开发的,因此它的设计不同于其他的开发环境。

参考资料来源:百度百科-BlueJ

参考资料来源:百度百科-IntelliJ IDEA

参考资料来源:百度百科-Netbeans

参考资料来源:百度百科-eclipse

参考资料来源:百度百科-MyEclipse

用Java编写简易记事本源代码

importjava.awt.BorderLayout;importjava.awt.Container;importjava.awt.Font;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.InputEvent;importjava.awt.event.KeyAdapter;importjava.awt.event.KeyEvent;importjava.awt.event.MouseAdapter;importjava.awt.event.MouseEvent;importjava.awt.event.WindowAdapter;importjava.awt.event.WindowEvent;importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;importjavax.swing.BorderFactory;importjavax.swing.JFileChooser;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JMenu;importjavax.swing.JMenuBar;importjavax.swing.JMenuItem;importjavax.swing.JOptionPane;importjavax.swing.JPopupMenu;importjavax.swing.JScrollPane;importjavax.swing.JTextArea;importjavax.swing.KeyStroke;importjavax.swing.ScrollPaneConstants;importjavax.swing.SwingConstants;publicclassJNotePadUIextendsJFrame{privateJMenuItemmenuOpen;privateJMenuItemmenuSave;privateJMenuItemmenuSaveAs;privateJMenuItemmenuClose;privateJMenueditMenu;privateJMenuItemmenuCut;privateJMenuItemmenuCopy;privateJMenuItemmenuPaste;privateJMenuItemmenuAbout;privateJTextAreatextArea;privateJLabelstateBar;privateJFileChooserfileChooser;privateJPopupMenupopUpMenu;publicJNotePadUI(){super("新建文本文件");setUpUIComponent();setUpEventListener();setVisible(true);}privatevoidsetUpUIComponent(){setSize(640,480);//菜单栏JMenuBarmenuBar=newJMenuBar();//设置「文件」菜单JMenufileMenu=newJMenu("文件");menuOpen=newJMenuItem("打开");//快捷键设置menuOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));menuSave=newJMenuItem("保存");menuSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));menuSaveAs=newJMenuItem("另存为");menuClose=newJMenuItem("关闭");menuClose.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,InputEvent.CTRL_MASK));fileMenu.add(menuOpen);fileMenu.addSeparator();//分隔线fileMenu.add(menuSave);fileMenu.add(menuSaveAs);fileMenu.addSeparator();//分隔线fileMenu.add(menuClose);//设置「编辑」菜单JMenueditMenu=newJMenu("编辑");menuCut=newJMenuItem("剪切");menuCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));menuCopy=newJMenuItem("复制");menuCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));menuPaste=newJMenuItem("粘贴");menuPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));editMenu.add(menuCut);editMenu.add(menuCopy);editMenu.add(menuPaste);//设置「关于」菜单JMenuaboutMenu=newJMenu("关于");menuAbout=newJMenuItem("关于JNotePad");aboutMenu.add(menuAbout);menuBar.add(fileMenu);menuBar.add(editMenu);menuBar.add(aboutMenu);setJMenuBar(menuBar);//文字编辑区域textArea=newJTextArea();textArea.setFont(newFont("宋体",Font.PLAIN,16));textArea.setLineWrap(true);JScrollPanepanel=newJScrollPane(textArea,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);ContainercontentPane=getContentPane();contentPane.add(panel,BorderLayout.CENTER);//状态栏stateBar=newJLabel("未修改");stateBar.setHorizontalAlignment(SwingConstants.LEFT);stateBar.setBorder(BorderFactory.createEtchedBorder());contentPane.add(stateBar,BorderLayout.SOUTH);popUpMenu=editMenu.getPopupMenu();fileChooser=newJFileChooser();}privatevoidsetUpEventListener(){//按下窗口关闭钮事件处理addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){closeFile();}});//菜单-打开menuOpen.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){openFile();}});//菜单-保存menuSave.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){saveFile();}});//菜单-另存为menuSaveAs.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){saveFileAs();}});//菜单-关闭文件menuClose.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){closeFile();}});//菜单-剪切menuCut.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){cut();}});//菜单-复制menuCopy.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){copy();}});//菜单-粘贴menuPaste.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){paste();}});//菜单-关于menuAbout.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){//显示对话框JOptionPane.showOptionDialog(null,"程序名称:\nJNotePad\n"+"程序设计:\n\n"+"简介:\n一个简单的文字编辑器\n"+"可作为验收Java的实现对象\n"+"欢迎网友下载研究交流\n\n"+"/","关于JNotePad",JOptionPane.DEFAULT_OPTION,JOptionPane.INFORMATION_MESSAGE,null,null,null);}});//编辑区键盘事件textArea.addKeyListener(newKeyAdapter(){publicvoidkeyTyped(KeyEvente){processTextArea();}});//编辑区鼠标事件textArea.addMouseListener(newMouseAdapter(){publicvoidmouseReleased(MouseEvente){if(e.getButton()==MouseEvent.BUTTON3)popUpMenu.show(editMenu,e.getX(),e.getY());}publicvoidmouseClicked(MouseEvente){if(e.getButton()==MouseEvent.BUTTON1)popUpMenu.setVisible(false);}});}privatevoidopenFile(){if(isCurrentFileSaved()){//文件是否为保存状态open();//打开}else{//显示对话框intoption=JOptionPane.showConfirmDialog(null,"文件已修改,是否保存?","保存文件?",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null);switch(option){//确认文件保存caseJOptionPane.YES_OPTION:saveFile();//保存文件break;//放弃文件保存caseJOptionPane.NO_OPTION:open();break;}}}privatebooleanisCurrentFileSaved(){if(stateBar.getText().equals("未修改")){returnfalse;}else{returntrue;}}privatevoidopen(){//fileChooser是JFileChooser的实例//显示文件选取的对话框intoption=fileChooser.showDialog(null,null);//使用者按下确认键if(option==JFileChooser.APPROVE_OPTION){try{//开启选取的文件BufferedReaderbuf=newBufferedReader(newFileReader(fileChooser.getSelectedFile()));//设定文件标题setTitle(fileChooser.getSelectedFile().toString());//清除前一次文件textArea.setText("");//设定状态栏stateBar.setText("未修改");//取得系统相依的换行字符StringlineSeparator=System.getProperty("line.separator");//读取文件并附加至文字编辑区Stringtext;while((text=buf.readLine())!=null){textArea.append(text);textArea.append(lineSeparator);}buf.close();}catch(IOExceptione){JOptionPane.showMessageDialog(null,e.toString(),"开启文件失败",JOptionPane.ERROR_MESSAGE);}}}privatevoidsaveFile(){//从标题栏取得文件名称Filefile=newFile(getTitle());//若指定的文件不存在if(!file.exists()){//执行另存为saveFileAs();}else{try{//开启指定的文件BufferedWriterbuf=newBufferedWriter(newFileWriter(file));//将文字编辑区的文字写入文件buf.write(textArea.getText());buf.close();//设定状态栏为未修改stateBar.setText("未修改");}catch(IOExceptione){JOptionPane.showMessageDialog(null,e.toString(),"写入文件失败",JOptionPane.ERROR_MESSAGE);}}}privatevoidsaveFileAs(){//显示文件对话框intoption=fileChooser.showSaveDialog(null);//如果确认选取文件if(option==JFileChooser.APPROVE_OPTION){//取得选择的文件Filefile=fileChooser.getSelectedFile();//在标题栏上设定文件名称setTitle(file.toString());try{//建立文件file.createNewFile();//进行文件保存saveFile();}catch(IOExceptione){JOptionPane.showMessageDialog(null,e.toString(),"无法建立新文件",JOptionPane.ERROR_MESSAGE);}}}privatevoidcloseFile(){//是否已保存文件if(isCurrentFileSaved()){//释放窗口资源,而后关闭程序dispose();}else{intoption=JOptionPane.showConfirmDialog(null,"文件已修改,是否保存?","保存文件?",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null);switch(option){caseJOptionPane.YES_OPTION:saveFile();break;caseJOptionPane.NO_OPTION:dispose();}}}privatevoidcut(){textArea.cut();stateBar.setText("已修改");popUpMenu.setVisible(false);}privatevoidcopy(){textArea.copy();popUpMenu.setVisible(false);}privatevoidpaste(){textArea.paste();stateBar.setText("已修改");popUpMenu.setVisible(false);}privatevoidprocessTextArea(){stateBar.setText("已修改");}publicstaticvoidmain(String[]args){newJNotePadUI();}}

求编写一个超级简单的Java的程序源代码

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class ConstructFrame extends JFrame

{

private static final long serialVersionUID = 1L;

String value1="",result,value2="";

int flag=0,fix=0,sum=1;

Boolean happy;

JTextField text=new JTextField(30);

int flagsum=0;

Container c=getContentPane();

JButton buttonx;

ConstructFrame()

{ super("计算器");

c.setLayout(null);

c.setBackground(Color.blue);

this.setSize(400, 400);

c.add(text);

text.setHorizontalAlignment(JTextField.RIGHT);

final JButton buttonx=new JButton("BackSpace");

c.add(buttonx);

buttonx.addMouseListener(new MouseAdapter()

{

public void mousePressed(MouseEvent e)

{

int count=0;

String temp;

if(flag==0)

{

count=value1.length();

if(count!=1)

temp=value1.substring(0, count-1);

else

temp="0";

value1=temp;

}

else

{

count=value2.length();

if(count!=1)

temp=value2.substring(0, count-1);

else

temp="0";

value2=temp;

}

text.setText(temp);

}

});

final JButton buttony=new JButton("CE");

c.add(buttony);

buttony.addMouseListener(new MouseAdapter()

{

public void mousePressed(MouseEvent e)

{

value1="";

value2="";

flag=0;

text.setText("0");

}

});

final JButton button1=new JButton("1");

c.add(button1);

button1.addMouseListener(new MouseAdapter()

{

public void mousePressed(MouseEvent e)

{

String temp;

if(flag==0)

{

value1=value1+1;

temp=value1;

}

else

{

value2=value2+1;

temp=value2;

}

text.setText(temp);

}

});

final JButton button2=new JButton(" 2 ");

c.add(button2);

button2.addMouseListener(new MouseAdapter()

{

public void mousePressed(MouseEvent e)

{

String temp;

if(flag==0)

{

value1=value1+2;

temp=value1;

}

else

{

value2=value2+2;

temp=value2;

}

text.setText(temp);

}

});

final JButton button3=new JButton("3");

c.add(button3);

button3.addMouseListener(new MouseAdapter()

{

public void mousePressed(MouseEvent e)

{

String temp;

if(flag==0)

{

value1=value1+3;

temp=value1;

}

else

{

value2=value2+3;

temp=value2;

}

text.setText(temp);

}

});

final JButton button4=new JButton("4");

c.add(button4);

button4.addMouseListener(new MouseAdapter()

{

public void mousePressed(MouseEvent e)

{

String temp;

if(flag==0)

{

value1=value1+4;

temp=value1;

}

else

{

value2=value2+4;

temp=value2;

}

text.setText(temp);

}

});

final JButton button5=new JButton("5");

c.add(button5);

button5.addMouseListener(new MouseAdapter()

{

public void mousePressed(MouseEvent e)

{

String temp;

if(flag==0)

{

value1=value1+5;

temp=value1;

}

else

{

value2=value2+5;

temp=value2;

}

text.setText(temp);

}

});

final JButton button6=new JButton("6");

c.add(button6);

button6.addMouseListener(new MouseAdapter()

{

public void mousePressed(MouseEvent e)

{

String temp;

if(flag==0)

{

value1=value1+6;

temp=value1;

}

else

{

value2=value2+6;

temp=value2;

}

text.setText(temp);

}

});

final JButton button7=new JButton("7");

c.add(button7);

button7.addMouseListener(new MouseAdapter()

{

public void mousePressed(MouseEvent e)

{

String temp;

if(flag==0)

{

value1=value1+7;

temp=value1;

}

else

{

value2=value2+7;

temp=value2;

}

text.setText(temp);

}

});

final JButton button8=new JButton("8");

c.add(button8);

button8.addMouseListener(new MouseAdapter()

{

public void mousePressed(MouseEvent e)

{

String temp;

if(flag==0)

{

value1=value1+8;

temp=value1;

}

else

{

value2=value2+8;

temp=value2;

}

text.setText(temp);

}

});

final JButton button9=new JButton("9");

c.add(button9);

button9.addMouseListener(new MouseAdapter()

{

public void mousePressed(MouseEvent e)

{

String temp;

if(flag==0)

{

value1=value1+9;

temp=value1;

}

else

{

value2=value2+9;

temp=value2;

}

text.setText(temp);

}

});

final JButton button0=new JButton("0");

c.add(button0);

button0.addMouseListener(new MouseAdapter()

{

public void mousePressed(MouseEvent e)

{

String temp;

if(flag==0)

{

value1=value1+0;

temp=value1;

}

else

{

value2=value2+0;

temp=value2;

}

text.setText(temp);

}

});

final JButton buttonadd=new JButton(" + ");

c.add(buttonadd);

buttonadd.addMouseListener(new MouseAdapter()

{

public void mousePressed(MouseEvent e)

{

flag=1;

fix=1;

flagsum=0;

}

});

final JButton buttonsubtract=new JButton(" - ");

c.add(buttonsubtract);

buttonsubtract.addMouseListener(new MouseAdapter()

{

public void mousePressed(MouseEvent e)

{

flag=1;

fix=2;

flagsum=0;

}

});

final JButton buttoncheng=new JButton(" * ");

c.add(buttoncheng);

buttoncheng.addMouseListener(new MouseAdapter()

{

public void mousePressed(MouseEvent e)

{

flag=1;

fix=3;

flagsum=0;

}

});

final JButton buttonchu=new JButton(" / ");

c.add(buttonchu);

buttonchu.addMouseListener(new MouseAdapter()

{

public void mousePressed(MouseEvent e)

{

flag=1;

flagsum=0;

fix=4;

}

});

final JButton buttonequal=new JButton(" = ");

c.add(buttonequal);

buttonequal.addMouseListener(new MouseAdapter()

{

public void mousePressed(MouseEvent e)

{

double temp1,temp2;

double temp=0;

flagsum=0;

temp1=Double.parseDouble(value1);

temp2=Double.parseDouble(value2);

flag=0;

switch(fix)

{

case 1: temp=temp1+temp2;break;

case 2: temp=temp1-temp2;break;

case 3: temp=temp1*temp2;break;

case 4: temp=temp1/temp2;break;

}

result=Double.valueOf(temp).toString();

value1=result;

value2="";

flag=1;

text.setText(result);

}

});

final JButton buttonpoint=new JButton(".");

c.add(buttonpoint);

buttonpoint.addMouseListener(new MouseAdapter()

{

public void mousePressed(MouseEvent e)

{ if(flagsum==0)

{

String temp;

if(flag==0 )

{

value1=value1+".";

temp=value1;

}

else

{

value2=value2+".";

temp=value2;

}

flagsum=1;

text.setText(temp);

}

}

});

JButton buttonz=new JButton("Start");

c.add(buttonz);

buttonz.addMouseListener(new MouseAdapter()

{

public void mousePressed(MouseEvent e)

{ if(sum%2==1)

{

happy=true;

text.setText("0.");

flag=0;

}

else

{

happy=false;

value1="";

value2="";

text.setText("");

}

text.setEnabled(happy);

button1.setEnabled(happy);

button2.setEnabled(happy);

button3.setEnabled(happy);

button4.setEnabled(happy);

button5.setEnabled(happy);

button6.setEnabled(happy);

button7.setEnabled(happy);

button8.setEnabled(happy);

button9.setEnabled(happy);

button0.setEnabled(happy);

buttonx.setEnabled(happy);

buttony.setEnabled(happy);

buttonadd.setEnabled(happy);

buttonsubtract.setEnabled(happy);

buttonpoint.setEnabled(happy);

buttonequal.setEnabled(happy);

buttoncheng.setEnabled(happy);

buttonchu.setEnabled(happy);

sum++;

}

});

button1.setEnabled(false);

button2.setEnabled(false);

button3.setEnabled(false);

button4.setEnabled(false);

button5.setEnabled(false);

button6.setEnabled(false);

button7.setEnabled(false);

button8.setEnabled(false);

button9.setEnabled(false);

button0.setEnabled(false);

buttonx.setEnabled(false);

buttony.setEnabled(false);

buttonadd.setEnabled(false);

buttonsubtract.setEnabled(false);

buttonpoint.setEnabled(false);

buttonequal.setEnabled(false);

buttoncheng.setEnabled(false);

buttonchu.setEnabled(false);

text.setEnabled(false);

text.setBounds(20, 20, 200, 40);

buttonx.setBounds(20, 60,100, 40);

buttony.setBounds(140, 60,100, 40);

buttonz.setBounds(260, 60,80, 40);

button1.setBounds(20, 120,60, 40);

button2.setBounds(100, 120,60, 40);

button3.setBounds(180, 120,60, 40);

buttonadd.setBounds(260, 120,60, 40);

button4.setBounds(20, 180,60, 40);

button5.setBounds(100, 180,60, 40);

button6.setBounds(180, 180,60, 40);

buttonsubtract.setBounds(260, 180,60, 40);

button7.setBounds(20, 240,60, 40);

button8.setBounds(100, 240,60, 40);

button9.setBounds(180, 240,60, 40);

buttoncheng.setBounds(260,240,60,40);

button0.setBounds(20, 300,60, 40);

buttonpoint.setBounds(100, 300, 60, 40);

buttonequal.setBounds(180,300,60, 40);

buttonchu.setBounds(260, 300,60, 40);

setVisible(true);

}

class MYMouseEvent extends MouseAdapter

{

public void mousePressed(MouseEvent e)

{

value1=e.toString();

text.setText(value1);

}

}

}

class Calutator

{

public static void main(String[] args)

{

new ConstructFrame();

}

}

你自己慢慢的看吧!


网页标题:java如何写源代码,java源代码怎么用
网站地址:http://cdkjz.cn/article/hechsg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220