做东西时遇到的一些问题以及解决方法~

正在做一个消费管理系统(就是自己想功能,自己不停的写东西)。接下来写的都是在做的时候遇到的一些问题,有的很快解决了,有的花了点时间。

(1)、如何在JFrame中里面插入一张背景图片呢?  本质上有两种方法,我只会其实的一种方法,另一种下次补充。我们可以使用JPanel来解决,因为里面一个paintComponent()函数,它的作用就是在JPanel一旦被实例化以后并且被放到JFrame了以后它就要显示里面的东西了,本质上说起来就是它会自动显示里面的图片

	class backGroundPaint extends JPanel
	{
		Image im;
		public backGroundPaint(Image im)
		{
			this.im=im;
			this.setOpaque(true);//背景图片不为隐藏
		}
		public void paintComponent(Graphics g)
		{
			super.paintComponents(g);
			g.drawImage(im, 0, 0, this.getWidth(), this.getHeight(),  this);
		}
	}

那么如果是gif图怎么办,其实这种方法也可以解决。所以这个不仅适合于静态图也适合与动态图。

(2)如果你要在其中放入几张并且在过一段时间不停的滚动应该怎么做呢。还是要JPanel来帮我们解决这个问题,在其中我们需要ImageIcon数组来帮我们保存图片,因为在一定的时候需要让它们显示,还有就是线程,没有线程动不起来的。

	class mationLabel extends JLabel implements Runnable
	{
		int index;
		ImageIcon[]images;
		public mationLabel()
		{
			images = new ImageIcon[4];
			images[0] = new ImageIcon("D:/新建文件夹/project/image/qwe3.jpg");
			images[1] = new ImageIcon("D:/新建文件夹/project/image/qwe4.jpg");
			images[2] = new ImageIcon("D:/新建文件夹/project/image/qwe5.jpg");
			images[3] = new ImageIcon("D:/新建文件夹/project/image/qwe6.jpg");
			index = 0;
			this.setIcon(images[index]);//把当前的图片显示在JPanel中
		}
		public void change()
		{
			index++;
			if(index>=4)
			{
				index = 0;
			}
			this.setIcon(images[index]);
		}
		public void run() {
			while(true)
			{
				change();
				try {
					Thread.sleep(3000);//每三秒钟转换一次。
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			
		}
		
	}
(2)1、在JTextArea定义的对象如果要让它到了最大的宽度换行应该使用setLineWrap(true),用setEditable(false)可以让这个文本区无法被选定,其实就是不能呗修改~。2、在当前的框体被按XX的时候它只是表面呗关闭了,但是在虚拟机里还在跑,我们可以使用JFrame.EXIT_ON_CLOSE来让那个框全部关闭,并释放内存。但是有个问题,如果你当前有个框,并且通过其中的按钮来打开了其他的框,但是你只是想让刚刚打开的框完全退出,原来的不想退出,你用这个方法那就不行了,肯定是两个框全部退出,那么我们就可以使用WindowConstants.HIDE_ON_CLOSE,这个可以只让当前的隐藏,使用WindowConstants.DISPOSE_ON_CLOSE来完全退出,不影响其他的。3、如果你想让你的按钮花样装X应该怎么办,也就是在按钮中放个好看的图片,通过下面

		ImageIcon icon = new ImageIcon("D:/新建文件夹/project/image/qwe12.gif");//获得图片
		b1=new JButton();
                b1.setIcon(icon);//把图片显示在按钮汇总
	        b1.setBorderPainted(false);//不要让按钮的边框显示出来
4、如果想让当前的框自动消失,可以使用dispose()这样就能自动消失5、我以前十分喜欢用InputStreamReader和OutputStreamWriter来读取或者传输数据,但是这个基本只适合与读取文件,不适合直接传输,所以最好别用。


(3)接下来的是重头戏,之前我有说过服务器的问题,现在的软件基本都是由客户端,服务器,客户端组成的。客户端的数据传输到了服务器以后,服务器应该把这些保存下来,那么应该保存在哪里呢?在这里肯定不是很少的数据,一定是大量的,所以肯定要用到数据库(数据库弄了一两天的时间来搭建以及熟悉如何使用),我现在使用的是微软的数据库SQL SERVER2008,因为正好数据库的课程也需要,这可以用来存放一些我做的那个东西的数据,说白了数据不可能十分的大,微软的可以用用,如果真的是太多的数据,那就需要甲骨文的数据库了,我这里有个网站可以帮助你们如何配置数据库

点击打开链接

等到你配置好了,接下来就可以使用了,首先你应该在数据库中创建一个新的数据库,然后在创建一张表,这个实在是很简单,就不具体展示了。这样好了以后,数据库那边就已经解决好了,。

Java需要先和数据库连接上,然后进行操作,对于这个我还不是很熟悉。

这个是添加:

	public void input(String s,String s1,boolean ju)
	{
		String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
		String dbUrl = "jdbc:sqlserver://localhost:1433; DatabaseName=消费管理系统数据库";// DatabaseName这个是要写数据库的名字
		String userName = "sa";
		String passPwd = "1499945";
		Connection dbConn;
		try {
			Class.forName(driverName);//连接数据库
			dbConn = DriverManager.getConnection(dbUrl, userName, passPwd);
			Statement stmt=dbConn.createStatement();

				if(ju)
				{
					String ss1="insert into 账号管理 values('"+s+"','"+s1+"')";//这个是网表中添加数据的语句。
				    stmt.executeUpdate(ss1);
				}


		} catch (ClassNotFoundException e) {
			//e.printStackTrace();
		} catch (SQLException e) {
			//e.printStackTrace();
		}
	}
这个是进行修改:
	public boolean judge(String s)
	{
		String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
		String dbUrl = "jdbc:sqlserver://localhost:1433; DatabaseName=消费管理系统数据库";
		String userName = "sa";
		String passPwd = "1499945";
		Connection dbConn;
		try {
			Class.forName(driverName);
			dbConn = DriverManager.getConnection(dbUrl, userName, passPwd);
			Statement stmt=dbConn.createStatement();
			PreparedStatement st = dbConn.prepareStatement("select username from 账号管理");
		    ResultSet rs = st.executeQuery();
			while(rs.next())
			{
				String s2=rs.getString("username");
				s2=s2.trim();//对s2进行去空格的处理
				if(s.equals(s2)==true)
				{
					return false;
				}
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return true;
	}

这个是查询某一个需要的数据
public void ddlDel(int index)throws SQLException{

		 String ddlDelsql="delete from UserInfo where userId="+index;

		 Connection conn=getConn(driverName, dbUrl, us, pw);

		 Statement sta=conn.createStatement();

		 sta.executeUpdate(ddlDelsql);

		 sta.close();

		 conn.close();

		 

	}

这个是删除某一条满足的需记录
	public void input1(String s,String s1,boolean ju)
	{
		String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
		String dbUrl = "jdbc:sqlserver://localhost:1433; DatabaseName=消费管理系统数据库";
		String userName = "sa";
		String passPwd = "1499945";
		Connection dbConn;
		//s是账号
		//s1是密码
		try {
			Class.forName(driverName);
			dbConn = DriverManager.getConnection(dbUrl, userName, passPwd);
			String ss1="update 账号管理  set userpossd=? where username= ?";
			PreparedStatement st = dbConn.prepareStatement(ss1);
			if(ju)
			{
				st.setString(1, s1);
				st.setString(2,s);
				st.addBatch();
			    st.executeBatch();
			}

		} catch (ClassNotFoundException e) {
			//e.printStackTrace();
		} catch (SQLException e) {
			//e.printStackTrace();
		}
	}

不过这里说一句我有些用的已经是过时的比如说在上面用到的Statement现在已经大都数在使用PreparedStatement了。所以要不停的学习。真的~

最后一点我们应该怎么让服务器一直接受来自客户端的信息呢,只要在让线程一直启动就可以了。

	public void go()
	{
		try {
			 ser=new ServerSocket(9999);
			while(true)
			{
				soc=ser.accept();
				Thread t=new Thread(new thread(soc));
				t.start();
				try {
					PrintWriter print=new PrintWriter(soc.getOutputStream(),true);
					print.println();
				} catch (IOException e) {
					e.printStackTrace();
				}

				
			}
		} catch (IOException e) {
			//e.printStackTrace();
		}
		
	}

这就是一个星期来一直在干的事情~。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值