代码如下,分两部分来捕捉异常
public class demo
{
public static void main(String args[])
{
int numbers[5];
for(int i=0;i<5;i++)
{
String str;
try // one
{
str=args[i];
}
catch(IndexOutOfBoundsException e)
{
Systemoutprintln("请你输入至少5个整数");
}
try //two
{
numbers[i]=IntegerparseInt(str);
}
catch(NumberFormatException e)
{
Systemoutprintln("请输入的全部都是数字串");
}
}
}
}
/以面向过程的程序设计方式思考:
1确定长方形的周长和面积的算法;
2编写两个方法(函数)分别计算长方形的周长和面积;
3求周长的方法(函数)和求面积的方法(函数)需要两个参数,
分别是长方形的长和宽/
//面向过程的程序设计代码:
/classRectangle
{
static
float
perimeter(float
length,floatwidth)
{
return
2(length+width);
}
static
float
area(float
length,
float
width)
{
return
lengthwidth;
}
public
static
void
main(String[]
args)
{
Systemoutprintln("the
perimeter
is
="
+perimeter(3123f,4267f));
Systemoutprintln("the
area
is="
+area(3123f,4267f));//使用f来表示浮点
}
}/
/以面向对象的程序设计方式思考:
1一个长方形可以看成是一个长方形对象;
2一个长方形对象有两个状态(长和宽)和两个行为(求周长和面积)
3将所有长方形的共性抽取出来,设计一个长方形的类
4通过长方形对象的行为,就可以求出某个具体的长方形的周长和面积/
//面向对象的程序设计代码:
classRectangle
{
float
l,h;//有两个状态,即两个成员变量
float
perimeter()
{
return
2(l+h);
}
float
area()
{
return
lh;
}
public
static
void
main(String[]
args)
{
Rectangle
rect1=newRectangle();
Rectangle
rect2=newRectangle();
rect1l=1013f;
rect1h=574f;
rect2l=1221f;
rect2h=2413f;
Systemoutprintln("Theperimeter
is
="
+
rect1perimeter());
Systemoutprintln("The
area
is="
+
rect1area());
Systemoutprintln("Theperimeter
is
="
+
rect2perimeter());
Systemoutprintln("The
area
is="
+
rect2area());
}
/K:\Java\try\lesson3>javacRectanglejava
K:\Java\try\lesson3>java
Rectangle
The
perimeter
is
=3174
The
area
is
=581462
The
perimeter
is
=7268
The
area
is
=2946273
/
}
一个简单的java程序由java包(如:import javaawt)、类声明 、变量、构造方法(可缺省)、方法 这几部分构成。
java程序中至少要有一个类被声明为public。
import javaawtFlowLayout;
import javaawteventActionEvent;
import javaawteventActionListener;
import javaxswingJButton;
import javaxswingJFrame;
import javaxswingJLabel;
import javaxswingJOptionPane;
import javaxswingJPasswordField;
import javaxswingJTextField;
public class Login {
public static void main(String args[]) {
LoginFrm frame = new LoginFrm();
}
}
class LoginFrm extends JFrame implements ActionListener{
JLabel nameLabel=new JLabel("用户名:");
JLabel pwdLabel=new JLabel("密码:");
JTextField name=new JTextField(10);
JPasswordField password=new JPasswordField(10);
JButton butnSure=new JButton("确定");
JButton butnCancel=new JButton("取消");
public LoginFrm() {
super("登陆");
setBounds(500, 200, 280, 220);
setVisible(true);
setLayout(null);
nameLabelsetBounds(45, 20, 100, 25);
add(nameLabel);
add(name);
namesetBounds(105, 20, 110, 25);
add(pwdLabel);
pwdLabelsetBounds(45, 60, 100, 25);
add(password);
passwordsetBounds(105, 60, 110, 25);
add(butnSure);
butnSuresetBounds(45, 100, 80, 25);
add(butnCancel);
butnCancelsetBounds(135, 100, 80, 25);
butnSureaddActionListener(this);
butnCanceladdActionListener(this);
setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
validate();//刷新
}
public void actionPerformed(ActionEvent e) {
if (egetSource() ==butnSure){
Systemoutprintln("用户名:"+namegetText());
Systemoutprintln("密码:"+namegetText());
if("admin"equals(namegetText()trim())&&"123"equals(passwordgetText()trim())){
thisdispose();
new MainFrm("用户界面",namegetText()trim(),passwordgetText()trim());
}else {
JOptionPaneshowMessageDialog(this, "用户不存在");
}
}else if(egetSource()==butnCancel){
Systemexit(1);
}
}
class MainFrm extends JFrame{
private JLabel info;
public MainFrm(String s,String name,String password) {
super(s);
setBounds(400, 200, 500, 400);
setLayout(new FlowLayout());
info=new JLabel("登陆成功,用户名:"+name+",密码:"+password);
add(info);
setVisible(true);
setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
validate();
}
}
}
是记事本的样式吗?就要这些要求吗?
下面这些代码的功能挺全的,可以参考一下,不用的可以删掉。
import javaio;
import javautilCalendar;
import javaawt;
import javaawtevent;
import javaxswing;
import javaxswingevent;
public class NoteBook extends JFrame implements ActionListener,ItemListener,WindowListener,MouseListener
{ Container c = thisgetContentPane();
JMenuBar jmb = new JMenuBar();
JColorChooser jcl = new JColorChooser();
JToolBar jToolBar = new JToolBar();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
JButton jButton4 = new JButton();
JMenu jm1 = new JMenu("文件(F)");
JMenu jm2 = new JMenu("编辑(E)");
JMenu jm4 = new JMenu("格式(O)");
JMenu jm5 = new JMenu("查看(V)");
JMenu jm3 = new JMenu("帮助(H)");
JMenuItem jmi1 = new JMenuItem("新建(N)");
JMenuItem jmi2 = new JMenuItem("打开(O)");
JMenuItem jmi3 = new JMenuItem("保存(S)");
JMenuItem jmi4 = new JMenuItem("退出(X)");
JMenu jmd4=new JMenu("颜色");
JMenu jmd0=new JMenu("字体");
JMenuItem aa=new JMenuItem("复制(C)");
JMenuItem bb=new JMenuItem("剪切(X)");
JMenuItem cc=new JMenuItem("粘贴(V)");
JCheckBoxMenuItem jcuti1 = new JCheckBoxMenuItem("粗体");
JCheckBoxMenuItem jxieti1 = new JCheckBoxMenuItem("斜体");
JMenuItem jmp1 = new JMenuItem("日期和时间");
JMenuItem jmp2 = new JMenuItem("背景颜色(H)");
JMenuItem jmi5 = new JMenuItem("删除内容(S)");
JMenuItem jmi7=new JMenuItem("关于自己");
JTextArea jta = new JTextArea(23,15);
JScrollPane jsp = new JScrollPane(jta);
JMenuItem jmdb1=new JMenuItem("**");//定义各种颜色
JMenuItem jmdb2=new JMenuItem("红色");
JMenuItem jmdb3=new JMenuItem("绿色");
JMenuItem jmdb4=new JMenuItem("蓝色");
JMenuItem jmdb5=new JMenuItem("黑色");
NoteBook()
{ thisaddWindowListener(this);
setBounds(200,200,200,200);
cadd(jmb,BorderLayoutNORTH);
validate();
setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
thissetResizable(false);
thissetTitle("记事本");
jmbadd(jm1);//文件
jm1setMnemonic('f');
jm1addActionListener(this);
jmbadd(jm2);//编辑
jm2setMnemonic('e');
jmbadd(jm4);//格式
jm4setMnemonic('o');
jm4addActionListener(this);
jmbadd(jm5);
jm5setMnemonic('v'); //查看
jmbadd(jm3);//帮助
jm3setMnemonic('h');
jmbsetLocation(0,0);
jm1add(jmi1);
jmi1addActionListener(this);//新建
jmi1setAccelerator(KeyStrokegetKeyStroke('N',EventCTRL_MASK,false));
jm1addSeparator();
jm1add(jmi2);
jmi2setAccelerator(KeyStrokegetKeyStroke('O',EventCTRL_MASK,false));
jmi2addActionListener(this);//打开
jm1addSeparator();
jm1add(jmi3);
jmi3setAccelerator(KeyStrokegetKeyStroke('S',EventCTRL_MASK,false));
jmi3addActionListener(this);//保存
jm1addSeparator();
jm1add(jmi4);
jmi1setMnemonic('n');
jmi2setMnemonic('o');
jmi3setMnemonic('s');
jmi4setMnemonic('x');
jmi4addActionListener(this);//退出
jm5add(jmi5); //删除
jm5addSeparator();
jmi5addActionListener(this);
jm4addSeparator();
jm4add(jmd4);
jm4add(jmd0);
jmd4add(jmdb1);
jmd4add(jmdb2);
jmd4add(jmdb3);
jmd4add(jmdb4);
jmd4add(jmdb5);
jmd4addActionListener(this); //添加监视器
jmdb1addActionListener(this);
jmdb2addActionListener(this);
jmdb3addActionListener(this);
jmdb4addActionListener(this);
jmdb5addActionListener(this);
jm5add(jmp1);
jm5addSeparator();
jm5add(jmp2);
jmp1addActionListener(this);//日期时间
jmp2addActionListener(this);//背景颜色
jm2add(aa);
jm2addSeparator();
jm2add(bb);
jm2addSeparator();
jm2add(cc);
jm2addSeparator();
jmd0add(jcuti1);
jmd0addSeparator();
jmd0add(jxieti1);
aasetMnemonic('C');
aasetAccelerator(KeyStrokegetKeyStroke(KeyEventVK_C,InputEventCTRL_MASK));
bbsetMnemonic('x');
bbsetAccelerator(KeyStrokegetKeyStroke(KeyEventVK_X,InputEventCTRL_MASK));
ccsetMnemonic('V');
ccsetAccelerator(KeyStrokegetKeyStroke(KeyEventVK_V,InputEventCTRL_MASK));
aaaddActionListener(this);//复制
bbaddActionListener(this);//剪切
ccaddActionListener(this);//粘贴
jmd4addActionListener(this);
jcuti1addItemListener(this);//粗体
jxieti1addItemListener(this);//斜体
jm3add(jmi7);
jmi7addActionListener(this);//关于自己
cadd(jToolBar,BorderLayoutCENTER); //工具条
jButton2setIcon(new ImageIcon("image\\newJPG"));//新建
jToolBaradd(jButton2);
jButton2setToolTipText("新建");
jButton2addActionListener(this);
jButton1setIcon(new ImageIcon("image\\openJPG"));//打开
jButton1setToolTipText("打开");
jToolBaradd(jButton1);
jButton1addActionListener(this);
jButton3setIcon(new ImageIcon("image\\saveJPG"));//保存
jToolBaradd(jButton3);
jButton3setToolTipText("保存");
jButton3addActionListener(this);
jButton4setIcon(new ImageIcon("image\\qqJPG"));
jButton4addActionListener(this); //退出
jToolBaradd(jButton4);
jButton4setToolTipText("退出");
cadd(jsp,BorderLayoutSOUTH);
thissetDefaultCloseOperation(JFrameDISPOSE_ON_CLOSE);
thissetSize(500,500);
thissetVisible(true);
jmdb1addActionListener(new ActionListener() //**
{ public void actionPerformed(ActionEvent e)
{ jtasetForeground(Coloryellow);
} } );
jmdb2addActionListener(new ActionListener() //红色
{ public void actionPerformed(ActionEvent e)
{ jtasetForeground(Colorred);
} } );
jmdb3addActionListener(new ActionListener() //绿色
{ public void actionPerformed(ActionEvent e)
{ jtasetForeground(Colorgreen);
} } );
jmdb4addActionListener(new ActionListener() //蓝色
{
public void actionPerformed(ActionEvent e)
{
jtasetForeground(Colorblue);
} } );
jmdb5addActionListener(new ActionListener() //黑色
{
public void actionPerformed(ActionEvent e)
{
jtasetForeground(Colorblack);
} } );
}
public void actionPerformed(ActionEvent e)
{ if(egetSource()==jmi1)//新建
{jtasetText("");
thissetTitle("无标题 - 记事本");
}
if(egetSource()==jmi2)//打开
{File f1;
JFileChooser jfc1 = new JFileChooser();
int num1 = jfc1showOpenDialog(this);
if(num1==JFileChooserAPPROVE_OPTION)
{ try {
f1 = jfc1getSelectedFile();
thissetTitle(f1getName());
FileReader fr = new FileReader(f1);
BufferedReader br = new BufferedReader(fr);
String str;
while((str = brreadLine())!=null)
{jtasetText(str);}
frclose();
brclose(); }
catch(FileNotFoundException e1)
{e1printStackTrace();}
catch(IOException e2){e2printStackTrace();}
}}
if(egetSource()==jmi3)//保存
{File f2 = null;
JFileChooser jfc2 = new JFileChooser();
int num2 = jfc2showSaveDialog(this);
if(num2==JFileChooserAPPROVE_OPTION){
f2=jfc2getSelectedFile();
thissetTitle(f2getName());
try{
FileWriter fw = new FileWriter(f2);
BufferedWriter bw = new BufferedWriter(fw);
bwwrite(jtagetText());
bwclose();
fwclose();
}catch(IOException e2){
e2printStackTrace();
}}}
if(egetSource()==jmi4)//退出
{
int a = JOptionPaneshowConfirmDialog(this,"文件已被改变,是否要保存?","提示",JOptionPaneYES_NO_CANCEL_OPTION);
if(a==1){
thisdispose();
}else if(a==0){
File f2 = null;
JFileChooser jfc2 = new JFileChooser();
int num2 = jfc2showSaveDialog(this);
if(num2==JFileChooserAPPROVE_OPTION){
f2=jfc2getSelectedFile();
thissetTitle(f2getName());
try{
FileWriter fw = new FileWriter(f2);
BufferedWriter bw = new BufferedWriter(fw);
bwwrite(jtagetText());
bwclose();
fwclose();
}catch(IOException e2){
e2printStackTrace();
}
thisdispose();
}}}
if(egetSource()==jmp1)//日期时间
{
Calendar c1 =CalendargetInstance();
int y = c1get(CalendarYEAR);
int m = c1get(CalendarMONTH);
int d = c1get(CalendarDATE);
int h = c1get(CalendarHOUR);
int m1 = c1get(CalendarMINUTE);
int m2 = m+1;
jtasetText(y+"年"+m2+"月"+d+"日"+h+":"+m1);
}
if(egetSource()==jmp2)//背景色
{ Color ccc = JColorChoosershowDialog(this,"color",ColorBLACK);
jtasetSelectedTextColor(ccc);
jtasetBackground(ccc);
}
if(egetSource()==jButton1)//打开
{File f1;
JFileChooser jfc1 = new JFileChooser();
int num1 = jfc1showOpenDialog(this);
if(num1==JFileChooserAPPROVE_OPTION)
{
try
{
f1 = jfc1getSelectedFile();
thissetTitle(f1getName());
FileReader fr = new FileReader(f1);
BufferedReader br = new BufferedReader(fr);
String str;
while((str = brreadLine())!=null)
{jtasetText(str);}
frclose();
brclose(); }
catch(FileNotFoundException e1)
{e1printStackTrace();}
catch(IOException e2){e2printStackTrace();}
} }
if(egetSource()==jButton2)
{jtasetText("");
thissetTitle("文本编辑器");
}
if(egetSource()==jmi5){
jtareplaceRange("",jtagetSelectionStart(),jtagetSelectionEnd()); //删除
}
if(egetSource()==jButton3)
{File f2 = null;
JFileChooser jfc2 = new JFileChooser();
int num2 = jfc2showSaveDialog(this);
if(num2==JFileChooserAPPROVE_OPTION){
f2=jfc2getSelectedFile();
thissetTitle(f2getName());
try{
FileWriter fw = new FileWriter(f2);
BufferedWriter bw = new BufferedWriter(fw);
bwwrite(jtagetText());
bwclose();
fwclose();
}catch(IOException e2){
e2printStackTrace();
}}}
if(egetSource()==aa) //复制
{jtacopy();}
else if(egetSource()==bb)//剪切
{ jtacut(); }
else if(egetSource()==cc)//粘贴
{jtapaste();}
if(egetSource()==jButton4)
{int a = JOptionPaneshowConfirmDialog(this,"文件已被改变,是否要保存?","提示",JOptionPaneYES_NO_CANCEL_OPTION);
if(a==1){
thisdispose();
}else if(a==0){
File f2 = null;
JFileChooser jfc2 = new JFileChooser();
int num2 = jfc2showSaveDialog(this);
if(num2==JFileChooserAPPROVE_OPTION){
f2=jfc2getSelectedFile();
thissetTitle(f2getName());
try{
FileWriter fw = new FileWriter(f2);
BufferedWriter bw = new BufferedWriter(fw);
bwwrite(jtagetText());
bwclose();
fwclose();
}catch(IOException e2){
e2printStackTrace();
}
thisdispose();
}}}
//if(egetSource()==jmi7)//关于我
//{About1 a=new About1();//实现跳转
// asetVisible(true);
//}
}
public void itemStateChanged(ItemEvent e) //字体
{Font c=new Font("Serif",FontPLAIN,14);
if(egetItemSelectable()==jcuti1)
{if(jcuti1getState()==true)
{ Font f=new Font("Serif",FontBOLD,20); //粗体
jtasetFont(f);}
else{jtasetFont(c);}
}
else if(egetItemSelectable()==jxieti1)
{if(jxieti1getState()==true)
{ Font b=new Font("Serif",FontITALIC,20);//斜体
jtasetFont(b);}
else{jtasetFont(c);
} } }
public static void main(String[] args)//入口
{ new NoteBook(); }
public void windowOpened(WindowEvent arg0) {}
public void windowClosing(WindowEvent arg0) {
int a = JOptionPaneshowConfirmDialog(this,"文件已被改变,是否要保存?","提示",JOptionPaneYES_NO_CANCEL_OPTION);
if(a==1){
thisdispose();
}else if(a==0){
File f2 = null;
JFileChooser jfc2 = new JFileChooser();
int num2 = jfc2showSaveDialog(this);
if(num2==JFileChooserAPPROVE_OPTION){
f2=jfc2getSelectedFile();
thissetTitle(f2getName());
try{
FileWriter fw = new FileWriter(f2);
BufferedWriter bw = new BufferedWriter(fw);
bwwrite(jtagetText());
bwclose();
fwclose();
}catch(IOException e2){
e2printStackTrace();
}
}
if(a==2){}
}
}
public void windowClosed(WindowEvent arg0) {}
public void windowIconified(WindowEvent arg0) {}
public void windowDeiconified(WindowEvent arg0) {}
public void windowActivated(WindowEvent arg0) {}
public void windowDeactivated(WindowEvent arg0) {}
public void mouseClicked(MouseEvent arg0) {}
public void mousePressed(MouseEvent arg0) {}
public void mouseReleased(MouseEvent arg0) {}
public void mouseEntered(MouseEvent arg0) {}
public void mouseExited(MouseEvent arg0) {}
}
public class TestStar {
public static void main(String[] args) {
String star = "";
for (int i = 0; i < 5; i++) {
if (i == 0) {
Systemoutprint(" " + star);
Systemoutprintln();
}
if (i == 1) {
for (int z = 0; z < 4; z++) {
Systemoutprint(" " + star);
}
Systemoutprintln();
}
if (i == 2) {
Systemoutprint(" ");
for (int x = 0; x < 3; x++) {
Systemoutprint(" " + star);
}
Systemoutprintln();
}
if (i == 3) {
for (int y = 0; y < 2; y++) {
Systemoutprint(" " + star + " ");
}
}
}
}
}
是好使的 但是我没找到画五角星有什么规律(五角星好象不是正规图形吧?)如果还有什么要求的话 补充问题(如果是用填充所有的东西 不包括 “ ”的话 我可以重新再给你写一个)
class Ball {
public void play() {
Systemoutprintln("玩球儿");
}
}
class Football extends Ball {
public void play() {
Systemoutprintln("使用足球运动");
}
}
class Basketball extends Ball {
public void play() {
Systemoutprintln("使用篮球运动");
}
}
public class TestMain {
public static void main(String[] args) {
TestMain tm = new TestMain();
tmtestPlay();
}
public void testPlay() {
Ball ball = new Football();
ballplay();
ball = new Basketball();
ballplay();
}
}
/
D:\>javac TestMainjava
D:\>java TestMain
使用足球运动
使用篮球运动
/
以上就是关于一个简单的java编程全部的内容,包括:一个简单的java编程、写个简单的java程序!在线等。、一个Java源程序一般是由哪三个基本部分组成的等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)