java画萝卜代码,java代码画樱花
急求这个十分简单的JAVA小代码详细注释!!
几年没有碰swing了,给你详细注释了。仔细看。希望对你有所帮助。
成都创新互联始终坚持【策划先行,效果至上】的经营理念,通过多达10多年累计超上千家客户的网站建设总结了一套系统有效的全网推广解决方案,现已广泛运用于各行各业的客户,其中包括:成都门窗定制等企业,备受客户称赞。
import java.awt.*;//java抽象窗口工具包
import java.awt.event.*;//java抽象窗口工具包组件所激发的各类事件的接口和类
public class Test5{//类名
Frame f;//定义一个Frame窗体
TextArea center;//文本域
Label la1,la2,la3;//三个标签
Panel east,south,north;//三块面板
Button b1,b2,b3,b4;//四个按钮
Choice l1;//下拉单选框
TextField t1;//文本域
// textfield只有一行可写
// textarea是一个区域,可以有很多行
public static void main(String[] args){//主函数,程序入口
Test mb = new Test();
mb.go();//调用go方法,初始化界面
}
private void go(){
f = new Frame("留言版程序");//标题
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt){
f.setVisible(false);
System.exit(0);
//System.exit(0) 0--正常结束程序 1--异常关闭程序
}
});
f.setBounds(0,0,600,400);//布局大小
f.setLayout(new BorderLayout());//显示方式
f.setResizable(false);
//下面都将控件按钮初始化,你懂得
north = new Panel();
south = new Panel();
east = new Panel();
center = new TextArea("留言内容:");
center.setEditable(false);
b1 = new Button("清屏");
b2 = new Button("至顶");
b3 = new Button("至尾");
la1 = new Label("留言版");
la2 = new Label("你");
la3 = new Label(" 地说:");
t1 = new TextField(20);
b4 = new Button("提交");
l1 =new Choice();
l1.add("微笑 ");
l1.add("生气 ");
l1.add("伤心 ");
f.add(BorderLayout.NORTH,north);//布局
f.add(BorderLayout.SOUTH,south);//布局
f.add(BorderLayout.EAST,east);//布局
f.add(BorderLayout.CENTER,center);//布局
north.add(BorderLayout.CENTER,la1);
south.add(la2);//把东西加到面板上
south.add(l1);
south.add(la3);
south.add(t1);//把东西加到面板上
south.add(b4);
east.setLayout(new GridLayout(9,0,1,10));
east.add(b1);
east.add(b2);
east.add(b3);
f.setVisible(true);//可视化
b4.addActionListener(new ActionListener(){//点击提交按钮产生事件
public void actionPerformed(ActionEvent e){
String text1,text2;
text1 = l1.getSelectedItem();
text2 = t1.getText();
t1.setText(null);
if(t1.getText() != ""){//将下拉单选框的内容和你输入在文本框的内容显示在中间面板上
center.append("\n");
center.append(text1);
center.append(text2);
}
}
});
b1.addActionListener(new ActionListener(){//清屏按钮事件,只留下中间面板显示:“留言内容:”,其余清楚
public void actionPerformed(ActionEvent e){
center.setText("留言内容:");
}
});
b2.addActionListener(new ActionListener(){//至顶按钮事件,光标焦点至顶
public void actionPerformed(ActionEvent e){
center.requestFocusInWindow();
center.setCaretPosition(8);
}
});
b3.addActionListener(new ActionListener(){//至尾按钮事件,光标焦点至尾
public void actionPerformed(ActionEvent e){
center.requestFocusInWindow();
center.setCaretPosition(center.getText().length());
}
});
}
}
明天要考java,有几个知识点不理解,求大佬可以把以下的题目代码发一下,能做几个都好,只要注释全就行~
还是耐着性子给你做完了望采纳。。。
第七题
/**
* 动物抽象类
*/
public abstract class Animal {
//颜色
private String color;
//类别
private String type;
//吃饭
public abstract void eat();
//叫
public abstract void cry();
//get set方法省略。。。
}
/**
* 游泳的接口
*/
public interface Swimable {
//游泳
public void swim();
}
/**
* 兔子类
*/
public class Rabbit extends Animal {
@Override
public void eat() {
System.out.println("小兔几吃萝卜。。。");
}
@Override
public void cry() {
System.out.println("小兔几呵呵哒。。。");
}
}
/**
* 青蛙类
*/
public class Frog extends Animal implements Swimable {
public void swim() {
System.out.println("青蛙会蛙泳。。。");
}
@Override
public void eat() {
System.out.println("青蛙吃昆虫。。。");
}
@Override
public void cry() {
System.out.println("青蛙呱呱叫。。。");
}
}
public class Test {
public static void main(String[] args) {
//兔子
Rabbit rabbit = new Rabbit();
rabbit.eat();
rabbit.cry();
//青蛙
Frog frog = new Frog();
frog.eat();
frog.cry();
frog.swim();
}
}
第八题
/**
* 学生类
*/
public class Student {
//学号
private String id;
//姓名
private String name;
//性别
private char gender;
//年龄
private int age;
public Student(String id, String name, char gender, int age) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
}
//获得学号、姓名、性别、年龄
public String toString() {
return "学号:"+id +" 姓名:" + name + " 性别:" + gender + " 年龄:" + age;
}
//修改年龄
public void changeAge(int range) {
age = range;
}
//get set方法省略。。。
}
public class Test {
public static void main(String[] args) {
Student s = new Student("alibb008", "杰克马", '公', 18) ;
System.out.println(s.toString());
s.changeAge(50);
System.out.println(s.toString());
}
}
感慨啊。。。
JAVA编译简易画图板代码
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import java.awt.geom.*;
import java.io.*;
class Point implements Serializable
{
int x,y;
Color col;
int tool;
int boarder;
Point(int x, int y, Color col, int tool, int boarder)
{
this.x = x;
this.y = y;
this.col = col;
this.tool = tool;
this.boarder = boarder;
}
}
class paintboard extends Frame implements ActionListener,MouseMotionListener,MouseListener,ItemListener
{
int x = -1, y = -1;
int con = 1;//画笔大小
int Econ = 5;//橡皮大小
int toolFlag = 0;//toolFlag:工具标记
//toolFlag工具对应表:
//(0--画笔);(1--橡皮);(2--清除);
//(3--直线);(4--圆);(5--矩形);
Color c = new Color(0,0,0); //画笔颜色
BasicStroke size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);//画笔粗细
Point cutflag = new Point(-1, -1, c, 6, con);//截断标志
Vector paintInfo = null;//点信息向量组
int n = 1;
FileInputStream picIn = null;
FileOutputStream picOut = null;
ObjectInputStream VIn = null;
ObjectOutputStream VOut = null;
// *工具面板--画笔,直线,圆,矩形,多边形,橡皮,清除*/
Panel toolPanel;
Button eraser, drLine,drCircle,drRect;
Button clear ,pen;
Choice ColChoice,SizeChoice,EraserChoice;
Button colchooser;
Label 颜色,大小B,大小E;
//保存功能
Button openPic,savePic;
FileDialog openPicture,savePicture;
paintboard(String s)
{
super(s);
addMouseMotionListener(this);
addMouseListener(this);
paintInfo = new Vector();
/*各工具按钮及选择项*/
//颜色选择
ColChoice = new Choice();
ColChoice.add("black");
ColChoice.add("red");
ColChoice.add("blue");
ColChoice.add("green");
ColChoice.addItemListener(this);
//画笔大小选择
SizeChoice = new Choice();
SizeChoice.add("1");
SizeChoice.add("3");
SizeChoice.add("5");
SizeChoice.add("7");
SizeChoice.add("9");
SizeChoice.addItemListener(this);
//橡皮大小选择
EraserChoice = new Choice();
EraserChoice.add("5");
EraserChoice.add("9");
EraserChoice.add("13");
EraserChoice.add("17");
EraserChoice.addItemListener(this);
////////////////////////////////////////////////////
toolPanel = new Panel();
clear = new Button("清除");
eraser = new Button("橡皮");
pen = new Button("画笔");
drLine = new Button("画直线");
drCircle = new Button("画圆形");
drRect = new Button("画矩形");
openPic = new Button("打开图画");
savePic = new Button("保存图画");
colchooser = new Button("显示调色板");
//各组件事件监听
clear.addActionListener(this);
eraser.addActionListener(this);
pen.addActionListener(this);
drLine.addActionListener(this);
drCircle.addActionListener(this);
drRect.addActionListener(this);
openPic.addActionListener(this);
savePic.addActionListener(this);
colchooser.addActionListener(this);
颜色 = new Label("画笔颜色",Label.CENTER);
大小B = new Label("画笔大小",Label.CENTER);
大小E = new Label("橡皮大小",Label.CENTER);
//面板添加组件
toolPanel.add(openPic);
toolPanel.add(savePic);
toolPanel.add(pen);
toolPanel.add(drLine);
toolPanel.add(drCircle);
toolPanel.add(drRect);
toolPanel.add(颜色); toolPanel.add(ColChoice);
toolPanel.add(大小B); toolPanel.add(SizeChoice);
toolPanel.add(colchooser);
toolPanel.add(eraser);
toolPanel.add(大小E); toolPanel.add(EraserChoice);
toolPanel.add(clear);
//工具面板到APPLET面板
add(toolPanel,BorderLayout.NORTH);
setBounds(60,60,900,600); setVisible(true);
validate();
//dialog for save and load
openPicture = new FileDialog(this,"打开图画",FileDialog.LOAD);
openPicture.setVisible(false);
savePicture = new FileDialog(this,"保存图画",FileDialog.SAVE);
savePicture.setVisible(false);
openPicture.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{ openPicture.setVisible(false); }
});
savePicture.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{ savePicture.setVisible(false); }
});
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{ System.exit(0);}
});
}
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
Point p1,p2;
n = paintInfo.size();
if(toolFlag==2)
g.clearRect(0,0,getSize().width,getSize().height);//清除
for(int i=0; in ;i++){
p1 = (Point)paintInfo.elementAt(i);
p2 = (Point)paintInfo.elementAt(i+1);
size = new BasicStroke(p1.boarder,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
g2d.setColor(p1.col);
g2d.setStroke(size);
if(p1.tool==p2.tool)
{
switch(p1.tool)
{
case 0://画笔
Line2D line1 = new Line2D.Double(p1.x, p1.y, p2.x, p2.y);
g2d.draw(line1);
break;
case 1://橡皮
g.clearRect(p1.x, p1.y, p1.boarder, p1.boarder);
break;
case 3://画直线
Line2D line2 = new Line2D.Double(p1.x, p1.y, p2.x, p2.y);
g2d.draw(line2);
break;
case 4://画圆
Ellipse2D ellipse = new Ellipse2D.Double(p1.x, p1.y, Math.abs(p2.x-p1.x) , Math.abs(p2.y-p1.y));
g2d.draw(ellipse);
break;
case 5://画矩形
Rectangle2D rect = new Rectangle2D.Double(p1.x, p1.y, Math.abs(p2.x-p1.x) , Math.abs(p2.y-p1.y));
g2d.draw(rect);
break;
case 6://截断,跳过
i=i+1;
break;
default :
}//end switch
}//end if
}//end for
}
public void itemStateChanged(ItemEvent e)
{
if(e.getSource()==ColChoice)//预选颜色
{
String name = ColChoice.getSelectedItem();
if(name=="black")
{c = new Color(0,0,0); }
else if(name=="red")
{c = new Color(255,0,0);}
else if(name=="green")
{c = new Color(0,255,0);}
else if(name=="blue")
{c = new Color(0,0,255);}
}
else if(e.getSource()==SizeChoice)//画笔大小
{
String selected = SizeChoice.getSelectedItem();
if(selected=="1")
{
con = 1;
size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
}
else if(selected=="3")
{
con = 3;
size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
}
else if(selected=="5")
{con = 5;
size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
}
else if(selected=="7")
{con = 7;
size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
}
else if(selected=="9")
{con = 9;
size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
}
}
else if(e.getSource()==EraserChoice)//橡皮大小
{
String Esize = EraserChoice.getSelectedItem();
if(Esize=="5")
{ Econ = 5*2; }
else if(Esize=="9")
{ Econ = 9*2; }
else if(Esize=="13")
{ Econ = 13*2; }
else if(Esize=="17")
{ Econ = 17*3; }
}
}
public void mouseDragged(MouseEvent e)
{
Point p1 ;
switch(toolFlag){
case 0://画笔
x = (int)e.getX();
y = (int)e.getY();
p1 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p1);
repaint();
break;
case 1://橡皮
x = (int)e.getX();
y = (int)e.getY();
p1 = new Point(x, y, null, toolFlag, Econ);
paintInfo.addElement(p1);
repaint();
break;
default :
}
}
public void mouseMoved(MouseEvent e) {}
public void update(Graphics g)
{
paint(g);
}
public void mousePressed(MouseEvent e)
{
Point p2;
switch(toolFlag){
case 3://直线
x = (int)e.getX();
y = (int)e.getY();
p2 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p2);
break;
case 4: //圆
x = (int)e.getX();
y = (int)e.getY();
p2 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p2);
break;
case 5: //矩形
x = (int)e.getX();
y = (int)e.getY();
p2 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p2);
break;
default :
}
}
public void mouseReleased(MouseEvent e)
{
Point p3;
switch(toolFlag){
case 0://画笔
paintInfo.addElement(cutflag);
break;
case 1: //eraser
paintInfo.addElement(cutflag);
break;
case 3://直线
x = (int)e.getX();
y = (int)e.getY();
p3 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p3);
paintInfo.addElement(cutflag);
repaint();
break;
case 4: //圆
x = (int)e.getX();
y = (int)e.getY();
p3 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p3);
paintInfo.addElement(cutflag);
repaint();
break;
case 5: //矩形
x = (int)e.getX();
y = (int)e.getY();
p3 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p3);
paintInfo.addElement(cutflag);
repaint();
break;
default:
}
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==pen)//画笔
{toolFlag = 0;}
if(e.getSource()==eraser)//橡皮
{toolFlag = 1;}
if(e.getSource()==clear)//清除
{
toolFlag = 2;
paintInfo.removeAllElements();
repaint();
}
if(e.getSource()==drLine)//画线
{toolFlag = 3;}
if(e.getSource()==drCircle)//画圆
{toolFlag = 4;}
if(e.getSource()==drRect)//画矩形
{toolFlag = 5;}
if(e.getSource()==colchooser)//调色板
{
Color newColor = JColorChooser.showDialog(this,"调色板",c);
c = newColor;
}
if(e.getSource()==openPic)//打开图画
{
openPicture.setVisible(true);
if(openPicture.getFile()!=null)
{
int tempflag;
tempflag = toolFlag;
toolFlag = 2 ;
repaint();
try{
paintInfo.removeAllElements();
File filein = new File(openPicture.getDirectory(),openPicture.getFile());
picIn = new FileInputStream(filein);
VIn = new ObjectInputStream(picIn);
paintInfo = (Vector)VIn.readObject();
VIn.close();
repaint();
toolFlag = tempflag;
}
catch(ClassNotFoundException IOe2)
{
repaint();
toolFlag = tempflag;
System.out.println("can not read object");
}
catch(IOException IOe)
{
repaint();
toolFlag = tempflag;
System.out.println("can not read file");
}
}
}
if(e.getSource()==savePic)//保存图画
{
savePicture.setVisible(true);
try{
File fileout = new File(savePicture.getDirectory(),savePicture.getFile());
picOut = new FileOutputStream(fileout);
VOut = new ObjectOutputStream(picOut);
VOut.writeObject(paintInfo);
VOut.close();
}
catch(IOException IOe)
{
System.out.println("can not write object");
}
}
}
}//end paintboard
public class pb
{
public static void main(String args[])
{ new paintboard("画图程序"); }
}
java绘图,求代码
上代码:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.LayoutManager;
import java.awt.Paint;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.event.*;
public class YuanYiDong extends JFrame{
private static int BANJIN=0;
private static int X=0;
private static int Y=0;
JTextField rTxt=new JTextField(5);
JTextField xField=new JTextField(5);
JTextField yField=new JTextField(5);
JButton paintBt=new JButton("画");
JLabel huaban=new huaban();
JPanel jPanel=new JPanel();
JLabel banjingLabel,xLabel,yLabel;
public YuanYiDong(){
banjingLabel=new JLabel("半径");
xLabel=new JLabel("X坐标");
yLabel=new JLabel("Y坐标");
this.setTitle("圆的移动");
this.setLocation(300,100);
this.setSize(500, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.add(rTxt);
jPanel.setLayout(new FlowLayout());
add(huaban,BorderLayout.CENTER);
jPanel.add(banjingLabel);
jPanel.add(rTxt);
jPanel.add(xLabel);
jPanel.add(xField);
jPanel.add(yLabel);
jPanel.add(yField);
jPanel.add(paintBt);
add(jPanel,BorderLayout.NORTH);
paintBt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
BANJIN=Integer.parseInt(rTxt.getText());
X=Integer.parseInt(xField.getText());
Y=Integer.parseInt(yField.getText());
huaban.repaint();
}
});
}
private void drawCirlce(Graphics g) {
g.setColor(Color.blue);
g.fillOval(X, Y, BANJIN,BANJIN);
}
public static void main(String[] args) {
YuanYiDong frame = new YuanYiDong();
}
public class huaban extends JLabel{
public huaban(){}
public void paint(Graphics g) {
Image image = createImage(getWidth(), getHeight());
drawCirlce(image.getGraphics());
g.drawImage(image, 0, 0, null);
}
}
}
给分吧!
用java 在窗体中画一个简单图形。
帮你改了一下。
你要画在panel上,然后frame.add(panel)就能显示。
是不是和applet搞混了,applet复写一些方法就能显示,但现在你编的是java gui
import java.awt.*;
import java.awt.Event.*;
import javax.swing.*; //import javax.swing.Timer;
import java.awt.BasicStroke;
//import java.util.Date;
//import java.text.*;
//import java.util.*;
public class TestGui {
public void paint(Graphics g) {
Graphics2D a2d = (Graphics2D) g;
int x = 120, y = 90, width = 150, height = 150;
a2d.setColor(Color.red);
a2d.setStroke(new BasicStroke(3.0f)); // 设置线条宽度,3.0即线的宽度
a2d.drawOval(x, y, width, height);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
// frame.add(new paint(),BorderLayout.CENTER);
frame.setSize(500, 500);
frame.setLocation(200, 200);
frame.setVisible(true);
Panel p = new Panel();
frame.add(p);
// frame.paint(null);
// TODO code application logic here
}
}
class Panel extends JPanel {
// 重新覆盖paint方法
public void paint(Graphics g) {
super.paint(g);
Graphics2D a2d = (Graphics2D) g;
int x = 120, y = 90, width = 150, height = 150;
a2d.setColor(Color.red);
a2d.setStroke(new BasicStroke(3.0f)); // 设置线条宽度,3.0即线的宽度
a2d.drawOval(x, y, width, height);
}
}
求解java代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SimpleEvent extends JFrame {
private static final long serialVersionUID = 1L;
private static int a;
private static int b;
private static char c;
private static int d;
private JButton jb = new JButton("1");
private JButton jb1 = new JButton("2");
private JButton jb9 = new JButton("+");
private JButton jb10 = new JButton("=");
final JTextField jt = new JTextField("", 20);
private boolean firstDigit = true;
public SimpleEvent() {
setLayout(null);
setSize(350, 250);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
Container cp = getContentPane();
cp.add(jb);
cp.add(jb1);
cp.add(jb9);
cp.add(jb10);
cp.add(jt);
jb.setBounds(10, 100, 80, 30);
jb1.setBounds(120, 100, 80, 30);
jb9.setBounds(10, 220, 80, 30);
jb10.setBounds(120, 220, 80, 30);
getContentPane().setLayout(new FlowLayout(80, 50, 10));
// if (firstDigit = true) {
// firstDigit = false;
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (firstDigit) {
jt.setText("1");
a = 1;
}
else {
jt.setText("1");
b = 1;
}
}
});
jb1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (firstDigit) {
jt.setText("2");
a = 2;
}
else {
jt.setText("2");
b = 2;
}
}
});
// }
// if (firstDigit = false) {
// jb.addActionListener(new ActionListener() {
// public void actionPerformed(ActionEvent arg0) {
// }
// });
// jb1.addActionListener(new ActionListener() {
// public void actionPerformed(ActionEvent arg0) {
// }
// });
// }
jb9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
firstDigit = false;
jt.setText("+");
// char c = '+';
c = '+';
}
});
jb10.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
firstDigit = true;
jt.setText("=");
switch (c) {
case '+' :
d = a + b;
break;
}
jt.setText(d + "");
System.out.println(a);
System.out.println(b);
System.out.println(d);
}
});
setVisible(true);
}
public static void main(String[] args) {
new SimpleEvent();
}
}
网页标题:java画萝卜代码,java代码画樱花
文章链接:http://azwzsj.com/article/hcshpg.html