找到了,很久以前写的一个简单画图,呵呵~当时要求用AWT写,很难受。
成都创新互联-云计算及IDC服务提供商,涵盖公有云、IDC机房租用、内蒙古服务器托管、等保安全、私有云建设等企业级互联网基础服务,服务电话:18982081108
package net.miqiang.gui;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
/**
* 简单画图板程序
* 好久没用 AWT 了,写起来真别扭,如果用 swing 会很舒服,有空再改写吧。
*
* @author 米强
*
*/
public class TestMain extends Frame {
// 画板
private Palette palette = null;
// 显示当前颜色的面板
private Panel nonceColor = null;
// 画笔粗细
private Label drawWidth = null;
// 画笔端点的装饰
private Label drawCap = null;
// 选取颜色按钮的监听事件类
private ButtonColorAction buttonColorAction = null;
// 鼠标进入按钮后光标样式的监听事件类
private ButtonCursor buttonCursor = null;
// 画笔样式的监听事件
private ButtonStrokeAction buttonStrokeAction = null;
/**
* 构造方法
*
*/
public TestMain() {
// 设置标题栏文字
super("简易画图板");
// 构造一个画图板
palette = new Palette();
Panel pane = new Panel(new GridLayout(2, 1));
// 画笔颜色选择器
Panel paneColor = new Panel(new GridLayout(1, 13));
// 12 个颜色选择按钮
Button [] buttonColor = new Button[12];
Color [] color = {Color.black, Color.blue, Color.cyan, Color.darkGray, Color.gray, Color.green, Color.magenta, Color.orange, Color.pink, Color.red, Color.white, Color.yellow};
// 显示当前颜色的面板
nonceColor = new Panel();
nonceColor.setBackground(Color.black);
paneColor.add(nonceColor);
buttonColorAction = new ButtonColorAction();
buttonCursor = new ButtonCursor();
for(int i = 0; i buttonColor.length; i++){
buttonColor[i] = new Button();
buttonColor[i].setBackground(color[i]);
buttonColor[i].addActionListener(buttonColorAction);
buttonColor[i].addMouseListener(buttonCursor);
paneColor.add(buttonColor[i]);
}
pane.add(paneColor);
// 画笔颜色选择器
Panel paneStroke = new Panel(new GridLayout(1, 13));
// 控制画笔样式
buttonStrokeAction = new ButtonStrokeAction();
Button [] buttonStroke = new Button[11];
buttonStroke[0] = new Button("1");
buttonStroke[1] = new Button("3");
buttonStroke[2] = new Button("5");
buttonStroke[3] = new Button("7");
buttonStroke[4] = new Button("9");
buttonStroke[5] = new Button("11");
buttonStroke[6] = new Button("13");
buttonStroke[7] = new Button("15");
buttonStroke[8] = new Button("17");
buttonStroke[9] = new Button("■");
buttonStroke[10] = new Button("●");
drawWidth = new Label("3", Label.CENTER);
drawCap = new Label("●", Label.CENTER);
drawWidth.setBackground(Color.lightGray);
drawCap.setBackground(Color.lightGray);
paneStroke.add(drawWidth);
for(int i = 0; i buttonStroke.length; i++){
paneStroke.add(buttonStroke[i]);
buttonStroke[i].addMouseListener(buttonCursor);
buttonStroke[i].addActionListener(buttonStrokeAction);
if(i = 8){
buttonStroke[i].setName("width");
}else{
buttonStroke[i].setName("cap");
}
if(i == 8){
paneStroke.add(drawCap);
}
}
pane.add(paneStroke);
// 将画笔颜色选择器添加到窗体中
this.add(pane, BorderLayout.NORTH);
// 将画图板添加到窗体中
this.add(palette);
// 添加窗口监听,点击关闭按钮时退出程序
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// 设置窗体 ICON 图标
this.setIconImage(Toolkit.getDefaultToolkit().createImage("images/palette.png"));
// 设置窗口的大小
this.setSize(new Dimension(400, 430));
// 设置窗口位置,处于屏幕正中央
this.setLocationRelativeTo(null);
// 显示窗口
this.setVisible(true);
}
/**
* 程序入口
*
* @param args
* 字符串数组参数
*/
public static void main(String[] args) {
new TestMain();
}
/**
* 选取颜色按钮的监听事件类
* @author 米强
*
*/
class ButtonColorAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
Color color_temp = ((Button)e.getSource()).getBackground();
nonceColor.setBackground(color_temp);
palette.setColor(color_temp);
}
}
/**
* 鼠标进入按钮变换光标样式监听事件类
* @author 米强
*
*/
class ButtonCursor extends MouseAdapter {
public void mouseEntered(MouseEvent e) {
((Button)e.getSource()).setCursor(new Cursor(Cursor.HAND_CURSOR));
}
public void mouseExited(MouseEvent e) {
((Button)e.getSource()).setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
}
/**
* 设置画笔的监听事件类
* @author 米强
*
*/
class ButtonStrokeAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
Button button_temp = (Button) e.getSource();
String name = button_temp.getName();
if(name.equalsIgnoreCase("width")){
drawWidth.setText(button_temp.getLabel());
palette.setStroke(Float.parseFloat(button_temp.getLabel()));
}else if(name.equalsIgnoreCase("cap")){
drawCap.setText(button_temp.getLabel());
if(button_temp.getLabel().equals("■")){
palette.setStroke(BasicStroke.CAP_SQUARE);
}else if(button_temp.getLabel().equals("●")){
palette.setStroke(BasicStroke.CAP_ROUND);
}
}
}
}
}
/**
* 画板类
*
* @author 米强
*
*/
class Palette extends Panel implements MouseListener, MouseMotionListener {
// 鼠标 X 坐标的位置
private int mouseX = 0;
// 上一次 X 坐标位置
private int oldMouseX = 0;
// 鼠标 Y 坐标的位置
private int mouseY = 0;
// 上一次 Y 坐标位置
private int oldMouseY = 0;
// 画图颜色
private Color color = null;
// 画笔样式
private BasicStroke stroke = null;
// 缓存图形
private BufferedImage image = null;
/**
* 构造一个画板类
*
*/
public Palette() {
this.addMouseListener(this);
this.addMouseMotionListener(this);
// 默认黑色画笔
color = new Color(0, 0, 0);
// 设置默认画笔样式
stroke = new BasicStroke(3.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
// 建立 1280 * 1024 的 RGB 缓存图象
image = new BufferedImage(1280, 1024, BufferedImage.TYPE_INT_RGB);
// 设置颜色
image.getGraphics().setColor(Color.white);
// 画背景
image.getGraphics().fillRect(0, 0, 1280, 1024);
}
/**
* 重写 paint 绘图方法
*/
public void paint(Graphics g) {
super.paint(g);
// 转换为 Graphics2D
Graphics2D g2d = (Graphics2D) g;
// 获取缓存图形 Graphics2D
Graphics2D bg = image.createGraphics();
// 图形抗锯齿
bg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// 设置绘图颜色
bg.setColor(color);
// 设置画笔样式
bg.setStroke(stroke);
// 画线,从上一个点到新的点
bg.drawLine(oldMouseX, oldMouseY, mouseX, mouseY);
// 将缓存中的图形画到画板上
g2d.drawImage(image, 0, 0, this);
}
/**
* 重写 update 方法
*/
public void update(Graphics g) {
this.paint(g);
}
/**
* @return stroke
*/
public BasicStroke getStroke() {
return stroke;
}
/**
* @param stroke 要设置的 stroke
*/
public void setStroke(BasicStroke stroke) {
this.stroke = stroke;
}
/**
* 设置画笔粗细
* @param width
*/
public void setStroke(float width) {
this.stroke = new BasicStroke(width, stroke.getEndCap(), stroke.getLineJoin());
}
/**
* 设置画笔端点的装饰
* @param cap 参考 java.awt.BasicStroke 类静态字段
*/
public void setStroke(int cap) {
this.stroke = new BasicStroke(stroke.getLineWidth(), cap, stroke.getLineJoin());
}
/**
* @return color
*/
public Color getColor() {
return color;
}
/**
* @param color 要设置的 color
*/
public void setColor(Color color) {
this.color = color;
}
public void mouseClicked(MouseEvent mouseEvent) {
}
/**
* 鼠标按下
*/
public void mousePressed(MouseEvent mouseEvent) {
this.oldMouseX = this.mouseX = mouseEvent.getX();
this.oldMouseY = this.mouseY = mouseEvent.getY();
repaint();
}
public void mouseReleased(MouseEvent mouseEvent) {
}
/**
* 鼠标进入棋盘
*/
public void mouseEntered(MouseEvent mouseEvent) {
this.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
}
/**
* 鼠标退出棋盘
*/
public void mouseExited(MouseEvent mouseEvent) {
this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
/**
* 鼠标拖动
*/
public void mouseDragged(MouseEvent mouseEvent) {
this.oldMouseX = this.mouseX;
this.oldMouseY = this.mouseY;
this.mouseX = mouseEvent.getX();
this.mouseY = mouseEvent.getY();
repaint();
}
public void mouseMoved(MouseEvent mouseEvent) {
}
}
绝对开源,绝对明了的留言板,便于学习的源码
用JAVA写的留言板原代码
/*
* guestbookServlet.java
*
* */
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.util.Date;
/**
*
* */
public class guestbookServlet extends HttpServlet {
boolean debug=false;
String sDBDriver;
Connection conn=null;
ResultSet rs=null;
/** Initializes the servlet.
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);
if(debug)
sDBDriver=new String("sun.jdbc.odbc.JdbcOdbcDriver");
else
sDBDriver=new String("org.gjt.mm.mysql.Driver");
try{
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e){
System.err.println("Driver类初始化:"+e.getMessage());
}
}
/** Destroys the servlet.
*/
public void destroy() {
}
/** Processes requests for both HTTP codeGET/code and codePOST/code methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
response.setContentType("text/html;charset=gb2312");
java.io.PrintWriter out = response.getWriter();
boolean empty=true;
boolean noResult=true;
String sqlStr;
int currentPage=0;
int totalPage=0;
int reccount=0;
ResultSet myrs=null;
String username=request.getParameter("username");
String email=request.getParameter("email");
String ucontent=request.getParameter("ucontent");
String ipage=request.getParameter("ipage");
if(ipage==null||ipage.length()==0)
currentPage=1;
else
currentPage=Integer.parseInt(ipage);
if((username==null||username.length()==0)||(ucontent==null||ucontent.length()==0))empty=true;
else empty=false;
Date myDate=new Date();
//String intime=new String(String.valueOf(myDate.getYear()+1990)+String.valueOf(myDate.getMonth()));
String year=String.valueOf(myDate.getYear()+1900);
String month=String.valueOf(myDate.getMonth()+1);
if(month.length()==1)
month=new String("0"+month);
String days=String.valueOf(myDate.getDate());
if(days.length()==1)
days=new String("0"+days);
String hours=String.valueOf(myDate.getHours());
if(hours.length()==1)
hours=new String("0"+hours);
String minutes=String.valueOf(myDate.getMinutes());
if(minutes.length()==1)
minutes=new String("0"+minutes);
String intime=year+"-"+month+"-"+days+" "+hours+":"+minutes;
if(email==null||email.length()==0)
email=new String("");
if(!empty){
//username=convert(username);
//email=convert(email);
//ucontent=convert(ucontent);
sqlStr="insert into Mintegbook(Mname,Memail,Mcontent,Mtime,Mid) values("+username+","+email+","+ucontent+","+intime+",1)";
getDsnConn();
executeInsert(sqlStr);
}
sqlStr=new String("select Mname,Memail,Mcontent,Mtime from Mintegbook order by Mtime DESC");
getDsnConn();
reccount=getRecordCount("Mintegbook");
if(reccount==0)
noResult=true;
else
noResult=false;
if(!noResult){
int ipageSize=10;
totalPage=getTotalPage("Mintegbook",ipageSize);
if(currentPagetotalPage)
currentPage=totalPage;
int cursor=(currentPage-1)*ipageSize+1;
try{
myrs=executeScrollableQuery(sqlStr);
myrs.absolute(cursor);
}
catch(SQLException e){
noResult=true;
}
}
out.println("HTMLHEADTITLE我的Servlet留言板/TITLE");
out.println("META http-equiv="Content-Type" content="text/html; charset=gb2312"");
out.println("STYLE type="text/css"");
out.println("!--");
out.println(".mytext { font-family: "宋体"; font-size: 12px}");
out.println(" --");
out.println("/STYLE");
out.println("/HEAD");
out.println("BODY bgcolor="#FFFFFF" text="#000000"");
out.println("TABLE width="600" border="0" cellspacing="0" cellpadding="0" align="CENTER" class="mytext"");
out.println("TRTD height="22" | a href=""我的主页/a | 我的Servlet留言板(A href="mailto:yf188@21cn.com"川石/A制作)/TD/TR");
out.println("TRTD height="1" bgcolor="#999933"/TD/TR");
out.println("/TABLE");
out.println("BR");
out.println("TABLE width="600" border="0" cellspacing="0" cellpadding="0" align="CENTER" class="mytext"");
out.println("TRTD height="8"/TD/TR");
out.println("TRTD height="18" bgcolor="#f7f7f7"DIV align="right"");
out.println("共有 "+reccount + " 条留言 ");
out.println(" 当前第font color=#ff0000"+currentPage+"/font/共 "+totalPage+" 页 ");
if(currentPage1)
out.println(" a href=guestbookServlet?ipage=1首页/a a href=guestbookServlet?ipage="+(currentPage-1)+"上一页/a ");
else
out.println(" 首页 上一页 ");
if(currentPagetotalPage)
out.println(" a href=guestbookServlet?ipage="+(currentPage+1)+"下一页/a a href=guestbookServlet?ipage="+totalPage+"末页/a ");
else
out.println(" 下一页 末页 ");
out.println("/DIV/TD/TR");
out.println("/TABLE");
out.println("BR");
//这里是显示留言内容
if(!noResult){
String dname;
String demail;
String dcontent;
String dtime;
Date temptime;
try{
do{
dname=new String(myrs.getString("Mname"));
demail=new String(myrs.getString("Memail"));
dcontent=new String(myrs.getString("Mcontent"));
try{
dtime=new String(myrs.getObject("Mtime").toString());
}
catch(java.lang.NullPointerException e){
dtime=new String("2001-04-06 12:30");
}
if(dname==null)
dname=new String("川石");
if(demail==null)
demail=new String("yf188@21cn.com");
if(dcontent==null)
dcontent=new String("test");
if(dtime==null)
dtime=new String("2001-04-06 12:30");
dname=convert(dname);
dcontent=convert(dcontent);
//temptime=myrs.getDate("Mtime");
/*
String tempyear=String.valueOf(temptime.getYear()+1900);
String tempmonth=String.valueOf(temptime.getMonth()+1);
if(tempmonth.length()==1)
tempmonth=new String("0"+tempmonth);
String tempdays=String.valueOf(temptime.getDate());
if(tempdays.length()==1)
tempdays=new String("0"+tempdays);
String tempminute=String.valueOf(temptime.getMinutes());
if(tempminute.length()==1)
tempminute=new String("0"+tempminute);
String temphours=String.valueOf(temptime.getHours());
if(temphours.length()==1)
temphours=new String("0"+temphours);
String dtime=tempyear+" 年 "+ tempmonth +" 月 "+ tempdays +" 日 " + temphours+ " 时 "+ tempminute + " 分 ";
*/
out.println("TABLE width="600" border="0" cellspacing="0" cellpadding="4" align="CENTER" class="mytext"");
out.println("TRTD姓名 A href="mailto:"+demail+"""+dname+"/A 留言时间:"+dtime+"/TD/TR");
out.println("TRTD height="10"/TD/TR");
out.println("TRTD height="10""+dcontent+"/TD/TR");
out.println("/TABLE");
out.println("HR width="600" size="1"");
}while(myrs.next());}
catch(SQLException e){
out.println("error found");
}
}
else{
out.println("还没有留言!");
}
//结束
out.println("FORM name="form1" method="post" action="guestbookServlet"");
out.println("TABLE width="600" border="0" cellspacing="0" cellpadding="4" align="CENTER" class="mytext"");
out.println("TRTD width="80"姓名:/TDTDINPUT type="text" name="username"*/TD/TR");
out.println("TRTDEmail:/TDTDINPUT type="text" name="email"*/TD/TR");
out.println("TRTD留言:/TDTDTEXTAREA name="ucontent" cols="65" rows="4"/TEXTAREA/TD/TR");
out.println("/TABLE");
out.println("TABLE width="400" border="0" cellspacing="0" cellpadding="6" align="CENTER" class="mytext"");
out.println("TRTD height="15" width="200" /TDTD /TD/TR");
out.println("TRTDDIV align="RIGHT"INPUT type="button" name="Button" value=" 提 交 " style="cursor:hand" onclick="javascript:check()"/DIV/TD");
out.println("TDINPUT type="reset" name="Submit2" value=" 重 置 "/TD/TR");
out.println("/TABLE");
out.println("/FORM");
out.println("TABLE width="400" border="0" cellspacing="0" cellpadding="4" align="CENTER" class="mytext"");
out.println("TRTD width="15" /TD/TR");
out.println("TRTDDIV align="LEFT"/DIV/TD/TR");
out.println("TRTD /TD/TR");
out.println("/TABLE");
out.println("/BODY");
out.println("/HTML");
out.println("SCRIPT language="javascript"");
out.println("function check(){");
out.println("if(form1.username.value.length1||form1.ucontent.value.length1)");
out.println("{alert(姓名和留言是必须有的!);}else{form1.submit();}}");
out.println("/SCRIPT");
out.close();
}
/** Handles the HTTP codeGET/code method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
processRequest(request, response);
}
/** Handles the HTTP codePOST/code method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
processRequest(request, response);
}
protected void getDsnConn(){
String sqlUrl="jdbc:mysql://10.0.0.1/你申请用户名?user=你的名字password=你的帐号";
try{
if(debug)
conn=DriverManager.getConnection("jdbc:odbc:ODBC源","用户名","密码口令");
else
conn=DriverManager.getConnection(sqlUrl);
}
catch(SQLException es){
System.err.println("和库连接时出错:"+es.getMessage());
}
}
protected void executeInsert(String sqlStr){
try{
Statement stmt=conn.createStatement();
stmt.executeUpdate(sqlStr);
}
catch(SQLException es){
System.err.println("执行插入时:"+es.getMessage());
}
}
protected void executeUpdate(String sqlStr){
try{
Statement stmt=conn.createStatement();
stmt.executeUpdate(sqlStr);
}
catch(SQLException e){
System.err.println("error in query record");
}
}
//查寻
protected ResultSet executeQuery(String sqlStr){
rs=null;
try{
Statement stmt=conn.createStatement();
rs=stmt.executeQuery(sqlStr);
}
catch(SQLException ex){
System.err.println("执行查寻出错:"+ex.getMessage());
}
return rs;
}
protected ResultSet executeScrollableQuery(String sqlStr){
rs=null;
try{
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(sqlStr);
}
catch(SQLException e){
System.err.println("执行动态查寻出错");
}
return rs;
}
//得到表记录总数
protected int getRecordCount(String sTableName){
rs=null;
int CountResult=0;
String sqlStr="select count(*) from "+sTableName;
try{
Statement stmt=conn.createStatement();
rs=stmt.executeQuery(sqlStr);
if(rs.next())
CountResult=rs.getInt(1);
rs=null;
stmt.close();
}
catch(SQLException ex){
System.err.println(ex.getMessage());
}
return CountResult;
}
//得到记录总页数
protected int getTotalPage(String sTableName,int iPageSize){
int totalPage;
int totalRecNum=getRecordCount(sTableName);
if(totalRecNum%iPageSize==0)
totalPage=totalRecNum/iPageSize;
else
totalPage=totalRecNum/iPageSize+1;
return totalPage;
}
protected String convert(String InputStr){
String converted=new String();
byte[] bytes;
try{
bytes=InputStr.getBytes("ISO8859-1");
converted=new String(bytes,"GB2312");
}
catch(java.io.UnsupportedEncodingException e){
System.out.print("error");
}
return converted;
}
/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
}
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();
}
}
你自己慢慢的看吧!
您好。我重新地帮你写了代码,完全地符合了你的图片中的界面要求、功能要求。代码如下:(解决了问题,记得给我分哦~^_^呵呵……)
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Rectangle;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import javax.swing.JTextArea;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JButton;
public class Liuyb extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JLabel jLabel = null;
private JTextArea jTextArea = null;
private JScrollPane jScrollPane = null;
private JLabel jLabel1 = null;
private JComboBox jComboBox = null;
private JLabel jLabel2 = null;
private JTextField jTextField = null;
private JButton jButton = null;
private JButton jButton1 = null;
private JButton jButton2 = null;
private JButton jButton3 = null;
/**
* This is the default constructor
*/
public Liuyb() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setContentPane(getJContentPane());
this.setTitle("留言板程序");
this.setBounds(new Rectangle(0, 0, 640, 480));
this.setVisible(true);
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel2 = new JLabel();
jLabel2.setBounds(new Rectangle(214, 407, 45, 24));
jLabel2.setFont(new Font("Dialog", Font.BOLD, 14));
jLabel2.setText("地说:");
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(32, 407, 15, 24));
jLabel1.setFont(new Font("Dialog", Font.BOLD, 14));
jLabel1.setText("你");
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(283, 15, 45, 20));
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
jLabel.setFont(new Font("Dialog", Font.BOLD, 14));
jLabel.setText("留言板");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jLabel, null);
jContentPane.add(getJScrollPane(), null);
jContentPane.add(jLabel1, null);
jContentPane.add(getJComboBox(), null);
jContentPane.add(jLabel2, null);
jContentPane.add(getJTextField(), null);
jContentPane.add(getJButton(), null);
jContentPane.add(getJButton1(), null);
jContentPane.add(getJButton2(), null);
jContentPane.add(getJButton3(), null);
}
return jContentPane;
}
/**
* This method initializes jTextArea
*
* @return javax.swing.JTextArea
*/
private JTextArea getJTextArea() {
if (jTextArea == null) {
jTextArea = new JTextArea();
jTextArea.setText("留言内容:");
jTextArea.setEditable(false);
}
return jTextArea;
}
/**
* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setBounds(new Rectangle(22, 49, 534, 347));
jScrollPane.setViewportView(getJTextArea());
}
return jScrollPane;
}
/**
* This method initializes jComboBox
*
* @return javax.swing.JComboBox
*/
private JComboBox getJComboBox() {
if (jComboBox == null) {
jComboBox = new JComboBox();
jComboBox.setBounds(new Rectangle(54, 407, 149, 24));
String[] mycbox={"微笑","大笑","痛苦"};
jComboBox.addItem(mycbox[0]);
jComboBox.addItem(mycbox[1]);
jComboBox.addItem(mycbox[2]);
}
return jComboBox;
}
/**
* This method initializes jTextField
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setBounds(new Rectangle(265, 407, 231, 24));
}
return jTextField;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(505, 407, 70, 24));
jButton.setText("提交");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
jTextArea.setText(jTextArea.getText()+"\r\n"+"你"+jComboBox.getSelectedItem().toString()+"地说:"+jTextField.getText());
}
});
}
return jButton;
}
/**
* This method initializes jButton1
*
* @return javax.swing.JButton
*/
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setBounds(new Rectangle(565, 51, 60, 32));
jButton1.setText("清屏");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
jTextArea.setText("留言内容:");
}
});
}
return jButton1;
}
/**
* This method initializes jButton2
*
* @return javax.swing.JButton
*/
private JButton getJButton2() {
if (jButton2 == null) {
jButton2 = new JButton();
jButton2.setBounds(new Rectangle(565, 112, 60, 32));
jButton2.setText("置顶");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
jTextArea.setCaretPosition(0);
}
});
}
return jButton2;
}
/**
* This method initializes jButton3
*
* @return javax.swing.JButton
*/
private JButton getJButton3() {
if (jButton3 == null) {
jButton3 = new JButton();
jButton3.setBounds(new Rectangle(565, 173, 60, 32));
jButton3.setText("至尾");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
jTextArea.setCaretPosition((int)jTextArea.getText().length());
}
});
}
return jButton3;
}
public static void main(String args[]){
new Liuyb();
}
}
先说说回复吧:
假如:
留言表(id , text )两个字段
回复表( id , text parentId )用来存放留言表id 三个字段
目前有一条留言
1001,'这是一条留言';
那么给这条留言的回复就是
1003 , '这是一条回复', 1001(这就是该留言的ID) 这样就把留言和回复关联上了
回复后点保存时,将数据传入后台,数据库做insert操作就行了
删除一样的
如果删除回复,直接传入回复的ID 即可
如果删除留言,得先判断该留言有没有回复,如果有回复,则先删除回复,在删除留言(外键约束)
售后响应及时
7×24小时客服热线数据备份
更安全、更高效、更稳定价格公道精准
项目经理精准报价不弄虚作假合作无风险
重合同讲信誉,无效全额退款