java图形小程序源代码 java简单图形代码
如何用Java编写一个绘制图形的小程序?
import java.awt.*;
成都创新互联长期为千余家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为清远企业提供专业的做网站、成都网站设计,清远网站改版等技术服务。拥有10年丰富建站经验和众多成功案例,为您定制开发。
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
//不规则图形的绘制
public class IrregularShapeDemo extends JFrame {
GeneralPath gPath= new GeneralPath(); //GeneralPath对象实例
Point aPoint;
//构造函数
public IrregularShapeDemo() {
super("不规则图形的绘制"); //调用父类构造函数
enableEvents(AWTEvent.MOUSE_EVENT_MASK|AWTEvent.MOUSE_MOTION_EVENT_MASK); //允许事件
setSize(300, 200); //设置窗口尺寸
setVisible(true); //设置窗口可视
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序
}
public void paint(Graphics g) { //重载窗口组件的paint()方法
Graphics2D g2D = (Graphics2D)g; //获取图形环境
g2D.draw(gPath); //绘制路径
}
public static void main(String[] args) {
new IrregularShapeDemo();
}
protected void processMouseEvent(MouseEvent e) { //鼠标事件处理
if(e.getID() == MouseEvent.MOUSE_PRESSED) {
aPoint = e.getPoint(); //得到当前鼠标点
gPath = new GeneralPath(); //重新实例化GeneralPath对象
gPath.moveTo(aPoint.x,aPoint.y); //设置路径点
}
}
protected void processMouseMotionEvent(MouseEvent e) { //鼠标运动事件处理
if(e.getID() == MouseEvent.MOUSE_DRAGGED) {
aPoint = e.getPoint(); //得到当前鼠标点
gPath.lineTo(aPoint.x, aPoint.y); //设置路径
gPath.moveTo(aPoint.x, aPoint.y);
repaint(); //重绘组件
}
}
}
用java编写一个图形化界面小程序,swing窗体定义4个标签对象,分别是:用户名、输入料号、扫描条码等。
用java编写一个图形化界面小程序,swing窗体定义4个标签对象,分别是:用户名、输入料号数量、扫描条码等。另外定义两个按钮对象(确定、取消)
上面这些你肯定会的
至于
用户点击确定按钮功能之后把以上数据(用户名、料号数量、条码、还有用户操作日期)统计在excel表中
如果本地测试的话,就新建一个excel,可以搜索相关java操作excel的代码,有jxl和poi,
首先查找设定的excel名字,如果没有创建一个;
还可以保存到数据库,提供实时导出excel
求一个简单的Java 小程序源文件!高分悬赏!
给你个计算器的,
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator {
//计算器面板
private JFrame f = new JFrame("Calculator");
//输入面扳
private JPanel inputPanel = new JPanel();
//加减乘除面板
private JPanel operationPanel = new JPanel();
//数字面板
private JPanel buttonsPanel = new JPanel();
//输入数据文本框
private JTextField input = new JTextField(20);
//退格键
private JButton backspace = new JButton("BackSpace");
//清空
private JButton CE = new JButton("CE ");
//删除
private JButton C = new JButton("C ");
//四则运算符号键
private JButton add = new JButton("+");
private JButton sub = new JButton("-");
private JButton mul = new JButton("*");
private JButton div = new JButton("/");
//小数点
private JButton decimal = new JButton(".");
//等号
private JButton equal = new JButton("=");
//数字键
private JButton zero = new JButton("0");
private JButton one = new JButton("1");
private JButton two = new JButton("2");
private JButton three = new JButton("3");
private JButton four = new JButton("4");
private JButton five = new JButton("5");
private JButton six = new JButton("6");
private JButton seven = new JButton("7");
private JButton eight = new JButton("8");
private JButton nine = new JButton("9");
private String num1 = "";//保存第一个运算数字
private String operator = "";//保存运算符号
public static void main(String[] args) {
new Calculator();//new计算器实例
}
public Calculator(){
//添加组件,布局
inputPanel.add(input);
f.add(inputPanel, BorderLayout.NORTH);
operationPanel.add(backspace);
operationPanel.add(CE);
operationPanel.add(C);
f.add(operationPanel, BorderLayout.CENTER);
buttonsPanel.add(add);
buttonsPanel.add(sub);
buttonsPanel.add(mul);
buttonsPanel.add(div);
buttonsPanel.add(one);
buttonsPanel.add(two);
buttonsPanel.add(three);
buttonsPanel.add(zero);
buttonsPanel.add(four);
buttonsPanel.add(five);
buttonsPanel.add(six);
buttonsPanel.add(decimal);
buttonsPanel.add(seven);
buttonsPanel.add(eight);
buttonsPanel.add(nine);
buttonsPanel.add(equal);
buttonsPanel.setLayout(new GridLayout(4, 4));
f.add(buttonsPanel, BorderLayout.SOUTH);
//注册各个组件监听事件
backspace.addMouseListener(new OperationMouseListener());
CE.addMouseListener(new OperationMouseListener());
C.addMouseListener(new OperationMouseListener());
decimal.addMouseListener(new OperationMouseListener());
equal.addMouseListener(new OperationMouseListener());
//注册四则运算监听
add.addMouseListener(new CalcMouseListener());
sub.addMouseListener(new CalcMouseListener());
mul.addMouseListener(new CalcMouseListener());
div.addMouseListener(new CalcMouseListener());
//注册数字监听事件
zero.addMouseListener(new NumberMouseListener());
one.addMouseListener(new NumberMouseListener());
two.addMouseListener(new NumberMouseListener());
three.addMouseListener(new NumberMouseListener());
four.addMouseListener(new NumberMouseListener());
five.addMouseListener(new NumberMouseListener());
six.addMouseListener(new NumberMouseListener());
seven.addMouseListener(new NumberMouseListener());
eight.addMouseListener(new NumberMouseListener());
nine.addMouseListener(new NumberMouseListener());
f.setVisible(true);
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class NumberMouseListener implements MouseListener{
public void mouseClicked(MouseEvent e) {
if(input.getText().trim().equals("0")){//如果文本框已经是0,结果还是0
input.setText(((JButton)e.getSource()).getText().trim());
}else{//否则的话,把0添加到后面,譬如文本框是1,结果就为10
input.setText(input.getText().concat(((JButton)e.getSource()).getText().trim()));
}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
private class CalcMouseListener implements MouseListener{
//如果输入的是运算符号,保存第一个结果和运算符号
public void mouseClicked(MouseEvent e) {
num1 = input.getText().trim();input.setText("");
operator = ((JButton)e.getSource()).getText().trim();
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
private class OperationMouseListener implements MouseListener{
public void mouseClicked(MouseEvent e) {
if(e.getSource() == backspace){//退格键,删除一个直到没有字符删除
String inputText = input.getText();
if(inputText.length() 0){
input.setText(inputText.substring(0, inputText.length() - 1));
}
}else if(e.getSource() == C){
input.setText("0");//C,清空所有运算数字和符号
num1 = "";
}else if(e.getSource() == CE){
input.setText("0");//CE--将文本框置为0
}else if(e.getSource() == decimal){
String text = input.getText().trim();
//如果按了小数点,如果文本框已经有小数点,不做任何操作,否则在结果后面加上小数点
if(text.indexOf(".") == -1){
input.setText(text.concat("."));
}
}else if(e.getSource() == equal){
//如果是等号
if(!operator.trim().equals("")){
if(!input.getText().trim().equals("")){
double result = 0D;
if(operator.equals("+")){//执行加法运算
result = Double.parseDouble(num1) + Double.parseDouble(input.getText().trim());
}else if(operator.equals("-")){//减法运算
result = Double.parseDouble(num1) - Double.parseDouble(input.getText().trim());
}else if(operator.equals("*")){//乘法运算
result = Double.parseDouble(num1) * Double.parseDouble(input.getText().trim());
}else if(operator.equals("/")){//除法运算
result = Double.parseDouble(num1) / Double.parseDouble(input.getText().trim());
}
//格式化最终结果,保留两位小数点
input.setText(new DecimalFormat("0.00").format(result));
}
}
}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
}
用java的图形界面设计写一个小程序,题目要求如下,在线等。。急急急、、、
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class test extends JFrame { private static final long serialVersionUID = 1L;
public static void main(String args[]) {
test frame = new test();
frame.setVisible(true);
}
public test() {
super();
getContentPane().setLayout(null);
setBounds(100, 100, 236, 193);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JLabel label = new JLabel();
label.setText("0");
label.setBounds(62, 33, 79, 18);
getContentPane().add(label);
final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
label.setText(String.valueOf((Integer.valueOf(label.getText())+1)));
}
});
button.setText("点击");
button.setBounds(50, 93, 106, 28);
getContentPane().add(button);
}
}
求JAVA大神给我发一段完整可运行的java图形小程序的代码(不用太多类),谢谢了
/*计算器*/
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
public class Calculator implements ActionListener{
JFrame frame;
JPanel panel;
JTextField tfShow;/*定义显示文本框*/
JButton b1[]=new JButton[10]; /*数字按钮*/
JButton b2[]=new JButton[6]; /*操作按钮*/
boolean isNumber;/*判断是否输入多位数字的变量*/
double number;/*存储输入数值、显示结果的变量*/
double result;/*存储中间运算结果的变量*/
char operator;/*存储当前操作符的成员变量*/
public Calculator(){
frame=new JFrame("计算器");
frame.setSize(300,300);/*指定框架窗口的大小*/
frame.setResizable(false);/*使框架窗口不可改变大小*/
JPanel contentPane=(JPanel)frame.getContentPane();
contentPane.setBorder(new EmptyBorder(20,20,20,20));/*绘制框架的指定大小的空透明边框*/
tfShow=new JTextField("0",25);/*指定属性的文本域*/
tfShow.setHorizontalAlignment(JTextField.RIGHT);/*设置文本域中文本的对齐方式*/
isNumber=true;/*初始值设置*/
number=0;/*初始值设置*/
result=0;/*初始值设置*/
operator=' ';/*初始值设置*/
for(int i=0;ib1.length;i++){
b1[i]=new JButton(Integer.toString(i));/*创建数字按钮*/
b1[i].setActionCommand(Integer.toString(i));
b1[i].addActionListener(this);
b1[i].setForeground(Color.blue);
}
String bs[]={"/","*","-","C","+","="};
for(int i=0;ib2.length;i++){
b2[i]=new JButton(bs[i]);/*创建操作按钮*/
b2[i].setActionCommand(bs[i]);
b2[i].addActionListener(this);
b2[i].setForeground(Color.red);
}
panel=new JPanel();
panel.setLayout(new GridLayout(4,5));
panel.add(b1[1]);
panel.add(b1[2]);
panel.add(b1[3]);
panel.add(b2[0]);
panel.add(b1[4]);
panel.add(b1[5]);
panel.add(b1[6]);
panel.add(b2[1]);
panel.add(b1[7]);
panel.add(b1[8]);
panel.add(b1[9]);
panel.add(b2[2]);
panel.add(b1[0]);
panel.add(b2[3]);
panel.add(b2[4]);
panel.add(b2[5]);
frame.add(tfShow,BorderLayout.NORTH);/*将文本框放置在框架上方*/
frame.add(panel,BorderLayout.CENTER);/*将装有按钮组的panel放在框架的中心*/
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/*设置框架窗口的默认窗口关闭操作*/
frame.setVisible(true);/*设置框架可见*/
}
public double getDisplay(){/*返回要显示的结果*/
return number;
}
public void reDisplay(){/*刷新文本域的内容*/
tfShow.setText(""+getDisplay());
}
/*对输入数字的处理*/
public void numberProcess(int num){
if(isNumbernum!=0){
String s1=Integer.toString(num);
String s2=Integer.toString((int)(this.number));
this.number=Double.parseDouble(s2+s1);/*对多位数字的处理*/
}else{
this.number=num;
}
isNumber=true;/*输入连续数字(即多位数字)时为真*/
}
public void operationProcess(char operator){/*根据输入的操作符改变当前操作符*/
switch(operator){
case '-':
this.operator='-';
break;
case '+':
this.operator='+';
break;
case '*':
this.operator='*';
break;
case '/':
this.operator='/';
break;
}
result=number;
isNumber=false;/*输入操作符时表示输入连续数字的标记变量为假*/
}
public void clear(){
number=0;
result=0;
}
public void equal(){/*计算运算结果*/
switch(operator){
case '-':
result=result-number;
break;
case '+':
result=result+number;
break;
case '*':
result=result*number;
break;
case '/':
result=result/number;
break;
case ' ':
result=number;
break;
}
number=result; /*把运算结果赋值给显示变量*/
isNumber=false;
operator=' ';
}
public static void main(String args[]){
Calculator cal=new Calculator();/*创建计算器*/
}
public void actionPerformed(ActionEvent e){
String command=e.getActionCommand();/*获取按钮激发的操作事件的命令名称*/
char c=command.charAt(0);/*将按钮命令名称的第一个字符赋值给一个字符c*/
switch(c){
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '0':
int number=Integer.parseInt(command);
numberProcess(number);/*输入数字的处理*/
break;
case '+':
case '-':
case '*':
case '/':
operationProcess(c);/*算数运算符的处理*/
break;
case '=':equal();break;/*计算运算结果*/
case 'C':clear();break;/*清零*/
}
reDisplay(); /*在文本域中显示信息*/
}
}
运行截图:
求一个java applet小程序的源代码
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.TextField;
import java.awt.event.ActionEvent;
public class Nicki extends Applet{
private static final long serialVersionUID = 1L;
private Button ok;
private int num=32;
private int resu=0;
private boolean isRig=false;
private TextField iPut;
public Nicki(){
this.setLayout(null);
ok=new Button("OK");
ok.setActionCommand(getName());
ok.setBounds(150, 150, 40, 20);
iPut=new TextField();
this.add(iPut);
iPut.setBounds(100, 150, 40, 20);
this.add(ok);
ok.addActionListener(new ButtonAction(this));
}
public void paint(Graphics g){
g.setColor(Color.white);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.BLACK);
g.drawString("Please guess a number ", 10, 20);
g.drawString("between 1 and 100", 10, 40);
if(isRig==falseresu!=0){
if(resunum){
g.drawString(""+resu+" is too big !", 10, 100);
}else if(resunum){
g.drawString(resu+"is too small !", 10, 100);
}
}else if(isRig==true){
g.setColor(Color.GREEN);
g.drawString("Yes,"+resu+" is the right number", 10, 80);
g.drawString("Your are great! ", 10, 100);
g.setColor(Color.red);
g.drawString(resu+"!", 70, 120);
}
iPut.setText("");
g.drawString("Input the number:", 0, 150);
}
public void ButtonActionPerformed(ActionEvent e){
if(e.getActionCommand().equals("panel0")){
resu=Integer.parseInt(iPut.getText());
if(num==resu){
isRig=true;
}else{
isRig=false;
}
repaint();
}
}
}
class ButtonAction implements java.awt.event.ActionListener{
Nicki su;
public ButtonAction(Nicki bun){
this.su=bun;
}
public void actionPerformed(ActionEvent e) {
su.ButtonActionPerformed(e);
}
}
当前标题:java图形小程序源代码 java简单图形代码
标题来源:http://cdiso.cn/article/hghpge.html