一个简单的java窗体下载小程序(完整代码)

合集下载

java窗口设计代码

java窗口设计代码

java窗口设计代码Java窗口设计代码Java窗口设计代码是指使用Java编程语言来创建和设计窗口界面的代码。

通过使用Java的窗口库,开发人员可以创建图形用户界面(GUI)并添加各种组件,如按钮、文本区域、下拉列表等,来与用户进行交互。

在下面的文章中,我将逐步介绍Java窗口设计代码的相关概念和步骤,帮助读者了解如何使用Java来创建窗口界面和与用户进行交互。

一、引入Java窗口库在使用Java窗口设计代码之前,我们首先需要引入Java窗口库。

Java提供了许多库,如AWT和Swing,用于创建窗口界面。

其中,Swing是AWT的升级版本,提供了更多的组件和功能。

要引入Swing库,我们需要在Java代码中添加以下语句:javaimport javax.swing.*;这样就可以使用Swing库提供的各种类和方法来设计窗口界面。

二、创建主窗口在Java窗口设计代码中,我们首先需要创建一个主窗口,作为整个窗口界面的容器。

可以通过继承JFrame类来创建主窗口,并使用以下代码:javapublic class MainFrame extends JFrame {public MainFrame() {设置主窗口的标题setTitle("Java窗口设计");设置主窗口的大小setSize(800, 600);设置主窗口的默认关闭操作setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);设置主窗口的布局setLayout(new BorderLayout());添加其他组件到主窗口...显示主窗口setVisible(true);}}在上述代码中,我们创建了一个`MainFrame`类,并继承了`JFrame`类。

然后,在`MainFrame`类的构造方法中,我们设置了主窗口的标题、大小、默认关闭操作和布局。

最后,我们添加其他组件到主窗口,并将主窗口设置为可见状态。

java解析小程序码

java解析小程序码

java解析小程序码小程序码是微信小程序的一种推广方式,用户可以通过扫描小程序码快速打开对应的小程序。

而在Java开发中,我们可以使用一些库来解析小程序码,从而获取相关的信息。

首先,我们需要引入相关的库。

在Java中,有很多开源的库可以用来解析小程序码,比如zxing、javase、jbarcode等。

这些库都提供了丰富的API,可以方便地解析小程序码。

接下来,我们需要获取小程序码的图片。

可以通过网络请求或者本地文件的方式获取小程序码的图片。

如果是网络请求,可以使用Java的HttpURLConnection或者HttpClient等库来发送请求并获取图片数据。

如果是本地文件,可以使用Java的FileInputStream来读取文件数据。

获取到小程序码的图片后,我们就可以使用相应的库来解析了。

以zxing为例,我们可以使用它提供的MultiFormatReader类来解析小程序码。

首先,我们需要将图片数据转换成Bitmap对象,然后将Bitmap对象转换成BinaryBitmap对象,最后使用MultiFormatReader的decode方法来解析。

解析后,我们可以获取到小程序码的内容。

小程序码的内容一般是一个URL,可以通过解析后的结果来获取。

我们可以使用正则表达式或者字符串处理的方式来提取URL。

除了获取小程序码的内容,我们还可以获取其他的信息,比如小程序码的宽度、高度、颜色等。

这些信息可以通过解析后的结果来获取。

在解析小程序码的过程中,我们还需要注意一些异常情况的处理。

比如图片解析失败、解析结果为空等情况,我们需要进行相应的处理,以保证程序的稳定性。

总结一下,Java解析小程序码可以通过引入相关的库来实现。

我们可以获取小程序码的图片,然后使用相应的库来解析。

解析后,我们可以获取小程序码的内容以及其他的信息。

在解析过程中,我们需要注意异常情况的处理,以保证程序的稳定性。

通过Java解析小程序码,我们可以方便地获取小程序码的相关信息,为小程序的推广和使用提供便利。

Java生成微信小程序二维码(可以指定小程序页面与动态参数)

Java生成微信小程序二维码(可以指定小程序页面与动态参数)

Java⽣成微信⼩程序⼆维码(可以指定⼩程序页⾯与动态参数)⼀、准备⼯作1.2. ⼩程序的唯⼀标识(appid)3. ⼩程序的密钥(secret)⼆、获取access_token打开,在参数列表中输⼊⼩程序的appid和secret,点击检查问题,如果appid和secret正确,则可以返回正确的access_token结果(图中下⽅的红框)三、⽣成微信⼩程序⼆维码⽣成⼩程序⼆维码官⽅⽂档⼀共有三种⽣成⼆维码的⽅式,可以根据使⽤场景去选择,这⾥我使⽤的是第三种⽣成⽅式 wxacode.getUnlimitedwxacode.createQRCode获取⼩程序⼆维码,适⽤于需要的码数量较少的业务场景。

通过该接⼝⽣成的⼩程序码,永久有效,有数量限制,详见获取⼆维码。

POST https:///cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKENwxacode.get获取⼩程序码,适⽤于需要的码数量较少的业务场景。

通过该接⼝⽣成的⼩程序码,永久有效,有数量限制,详见获取⼆维码。

POST https:///wxa/getwxacode?access_token=ACCESS_TOKENwxacode.getUnlimited获取⼩程序码,适⽤于需要的码数量极多的业务场景。

通过该接⼝⽣成的⼩程序码,永久有效,数量暂⽆限制。

更多⽤法详见获取⼆维码。

POST https:///wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN使⽤wxacode.getUnlimited⽣成⼩程序⼆维码获取⼩程序码,适⽤于需要的码数量极多的业务场景。

通过该接⼝⽣成的⼩程序码,永久有效,数量暂⽆限制。

更多⽤法详见获取⼆维码。

POST https:///wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN说明通过该接⼝⽣成的⼩程序码,永久有效,数量暂⽆限制。

JAVA小程序—贪吃蛇源代码

JAVA小程序—贪吃蛇源代码

JAVA贪吃蛇源代码SnakeGame。

javapackage SnakeGame;import javax.swing。

*;public class SnakeGame{public static void main( String[]args ){JDialog。

setDefaultLookAndFeelDecorated( true ); GameFrame temp = new GameFrame();}}Snake.javapackage SnakeGame;import java。

awt.*;import java。

util。

*;class Snake extends LinkedList{public int snakeDirection = 2;public int snakeReDirection = 4;public Snake(){this。

add( new Point( 3, 3 ));this。

add(new Point(4, 3 ));this.add( new Point(5,3 ) );this。

add( new Point(6,3 ) );this。

add(new Point(7,3 ));this。

add( new Point( 8,3 ) );this。

add( new Point( 9, 3 ));this。

add( new Point( 10,3 ));}public void changeDirection( Point temp, int direction ) {this。

snakeDirection = direction;switch(direction ){case 1://upthis.snakeReDirection = 3;this。

add( new Point(temp.x,temp.y - 1 ));break;case 2://rightthis。

java下载excel文件

java下载excel文件

java下载excel文件原代码剪切图片:修改后剪切图片:源码:public void downloadTaskModel()throws Exception{HttpServletResponse response = ToftServletContext.getContext().getResponse(); HttpServletRequest request = ToftServletContext.getContext().getRequest();String path = request.getRealPath("/");path = new String(path.getBytes("iso-8859-1"),"GBK");String matches = "[A-Za-z]:\\\\[^:?\"><*]*";if(!path.matches(matches)){throw new Exception("文件路径存在非法字符!");}path = path.replaceAll("\\\\", "/");File file = new File(path+"/templateFile/taskTemplate.xls");if(!file.isFile()||!file.exists()||file.isDirectory()){throw new Exception("下载的文件路径或文件不存在!");}//由于限制了导入模板的类型,所以在导出模板时必须限制。

if(!file.getName().matches(".*\\.(?i)(xls)$")){throw new Exception("当前模板类型有误,只能导出“.xls”格式的excel模板!"); }response.setContentType("application/x-msdownload");response.setContentLength((int)file.length());String taskNme = ClientUtils.getTimeStr(new Date(), "yyyyMMddHHmmss");response.setHeader("Content-Disposition","attachment;filename="+URLEncoder.encode("模板"+taskNme+".xls","UTF-8"));FileInputStream fis = new FileInputStream(file);BufferedInputStream buff = new BufferedInputStream(fis);byte b[] = new byte[1024];long k = 0 ;OutputStream myout = response.getOutputStream();while(k<file.length()){int j = buff.read(b,0,1024);k += j ;myout.write(b,0,j);}myout.flush();myout.close();}。

一个简单又有趣的JAVA小游戏代码

一个简单又有趣的JAVA小游戏代码

一个简单又有趣的JAVA小游戏代码猜数字import java.util.*;import java.io.*;public class CaiShu{public static void main(String[] args) throws IOException{Random a=new Random();int num=a.nextInt(100);System.out.println("请输入一个100以内的整数:");for (int i=0;i<=9;i++){BufferedReader bf=new BufferedReader(new InputStreamReader(System.in)); String str=bf.readLine();int shu=Integer.parseInt(str);if (shu>num)System.out.println("输入的数大了,输小点的!");else if (shu<num)System.out.println("输入的数小了,输大点的!");else {System.out.println("恭喜你,猜对了!");if (i<=2)System.out.println("你真是个天才!");else if (i<=6)System.out.println("还将就,你过关了!"); else if (i<=8)System.out.println("但是你还……真笨!"); elseSystem.out.println("你和猪没有两样了!"); break;}import java.util.Scanner;import java.util.Random;public class Fangfa{static int sum,sum1=0;public static void main(String [] args){int a=1,b=1,c=1;int k=0,m=1;int money =5000;int zhu =0;boolean flag = true;Random rand = new Random();Scanner input = new Scanner(System.in);while(m==1){while(flag){System.out.println("掷色子开始!");System.out.println("请下注注:下注金额只能是50的倍数且不能超过1000"); zhu=input.nextInt();if(zhu%50==0&&zhu<=1000&&zhu<=money){System.out.println("下注成功");System.out.println("买大请输入数字1,买小输入数字2");k=input.nextInt();a= rand.nextInt(6)+1;b= rand.nextInt(6)+1;c= rand.nextInt(6)+1;sum=a+b+c;if(k==1){if(sum>9){money+=zhu;System.out.println("恭喜您猜对了,骰子点数为"+sum+"结果是大"+"余额为"+money); }else{money-=zhu;System.out.println("很遗憾,骰子点数为"+sum+"结果是小"+"余额为"+money);}}if(k==2){if(sum<=9){money+=zhu;System.out.println("恭喜您猜对了,骰子点数为"+sum+"结果是小"+"余额为"+money); }else{money-=zhu;System.out.println("很遗憾,骰子点数为"+sum+"结果是大"+"余额为"+money);}}flag= false;System.out.println("继续请按1,退出请按任意键");m=input.nextInt();if(m==1){flag=true;System.out.println("您选择的是继续");}else{flag=false;System.out.println("欢迎您下次再来玩");}}else{System.out.println("下注失败"+"余额为"+money); }}}}。

Java下载文件的四种方式详细代码

Java下载文件的四种方式详细代码

Java下载⽂件的四种⽅式详细代码1.以流的⽅式下载public HttpServletResponse download(String path, HttpServletResponse response) {try {// path是指欲下载的⽂件的路径。

File file = new File(path);// 取得⽂件名。

String filename = file.getName();// 取得⽂件的后缀名。

String ext = filename.substring(stIndexOf(".") + 1).toUpperCase();// 以流的形式下载⽂件。

InputStream fis = new BufferedInputStream(new FileInputStream(path));byte[] buffer = new byte[fis.available()];fis.read(buffer);fis.close();// 清空responseresponse.reset();// 设置response的Headerresponse.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));response.addHeader("Content-Length", "" + file.length());OutputStream toClient = new BufferedOutputStream(response.getOutputStream());response.setContentType("application/octet-stream");toClient.write(buffer);toClient.flush();toClient.close();} catch (IOException ex) {ex.printStackTrace();}return response;}2.下载本地⽂件public void downloadLocal(HttpServletResponse response) throws FileNotFoundException {// 下载本地⽂件String fileName = "Operator.doc".toString(); // ⽂件的默认保存名// 读到流中InputStream inStream = new FileInputStream("c:/Operator.doc");// ⽂件的存放路径// 设置输出的格式response.reset();response.setContentType("bin");response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");// 循环取出流中的数据byte[] b = new byte[100];int len;try {while ((len = inStream.read(b)) > 0)response.getOutputStream().write(b, 0, len);inStream.close();} catch (IOException e) {e.printStackTrace();}}3.下载⽹络⽂件public void downloadNet(HttpServletResponse response) throws MalformedURLException {// 下载⽹络⽂件int bytesum = 0;int byteread = 0;URL url = new URL("/logo.gif");try {URLConnection conn = url.openConnection();InputStream inStream = conn.getInputStream();FileOutputStream fs = new FileOutputStream("c:/abc.gif");byte[] buffer = new byte[1204];int length;while ((byteread = inStream.read(buffer)) != -1) {bytesum += byteread;System.out.println(bytesum);fs.write(buffer, 0, byteread);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}4.⽀持在线打开的⽅式public void downLoad(String filePath, HttpServletResponse response, boolean isOnLine) throws Exception {File f = new File(filePath);if (!f.exists()) {response.sendError(404, "File not found!");return;}BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));byte[] buf = new byte[1024];int len = 0;response.reset(); // ⾮常重要if (isOnLine) { // 在线打开⽅式URL u = new URL("file:///" + filePath);response.setContentType(u.openConnection().getContentType());response.setHeader("Content-Disposition", "inline; filename=" + f.getName());// ⽂件名应该编码成UTF-8} else { // 纯下载⽅式response.setContentType("application/x-msdownload");response.setHeader("Content-Disposition", "attachment; filename=" + f.getName());}OutputStream out = response.getOutputStream();while ((len = br.read(buf)) > 0)out.write(buf, 0, len);br.close();out.close();}到此这篇关于Java下载⽂件的四种⽅式详细代码的⽂章就介绍到这了。

Java 小程序连连看源代码

Java 小程序连连看源代码
}
public void reload() {
int save[] = new int[30];
int n=0,cols,rows;
int grid[][]= new int[8][7];
for(int i=0;i<=6;i++) {
for(int j=0;j<=5;j++) {
resetButton.addActionListener(this);
newlyButton=new JButton("再来一局");
newlyButton.addActionListener(this);
southPanel.add(exitButton);
southPanel.add(resetButton);
thisContainer.add(southPanel,"South");
thisContainer.add(northPanel,"North");
centerPanel.setLayout(new GridLayout(6,5));
for(int cols = 0;cols < 6;cols++){
Container thisContainer;
JPanel centerPanel,southPanel,northPanel; //子面板
JButton diamondsButton[][] = new JButton[6][5];//游戏按钮数组
JButton exitButton,resetButton,newlyButton; //退出,重列,重新开始按钮
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
监听事件的listener包:
其中,ui包:
MainWin.java
package下载.ui;
public class MainWin {
public static void main(String[] args) {
DownloadWin win = new DownloadWin("下载");
}
* <p>文件保存目录
*/
private void downFile(String httpAddress, String fileDir) {
SplitString httpSplit = new SplitString();
fileName = httpSplit.split(httpAddress,"/");//根据“/”分解
private InputStream itStream;
private DataInputStream in;
private FileOutputStream otStream;
private DataOutputStream out;
public DownloadListener(JTextField httpLine, JTextField savePath,
Box boxV2 = Box.createHorizontalBox();
Box boxV3 = Box.createHorizontalBox();
Box boxV4 = Box.createHorizontalBox();
Box boxV5 = Box.createHorizontalBox();
*
* @param accordingString
* @return返回“/”之后最后一个字符串
*/
public String split(String splitString,String accordingString) {
//httpAddress.endsWith("/");
//如果a/c/则返回的字符串是c
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import .URL;
import .URLConnection;
lineLabel = new JLabel("访问地址:");
httpLine = new JTextField();
saveLabel = new JLabel("保存路径:");
savePath = new JTextField();
btnDownload = new JButton("下载");
infoLabel = new JLabel();
/**
*监听器
*/
DownloadListener listener = new DownloadListener(httpLine,savePath,infoLabel);//将需要改变显示状态的控件传递过去处理
btnDownload.addActionListener(listener);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private void init() {
Box baseBox = Box.createVerticalBox();
Box boxV1 = Box.createHorizontalBox();
}
DownloadWin.java
package下载.ui;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
package下载.listener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
private JTextField httpLine;
private JTextField savePath;
private JButton btnDownload;
private JLabel lineLabel;
private JLabel saveLabel;
private static JLabel infoLabel;
while((n=itStream.read(b))!=-1){
otStream.flush();
otStream.write(b);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
//otStream.flush();//刷新此输出流并强制写出所有缓冲的输出字节
//String[] str = httpAddress.split("/");
//"unhappy".substring(2) returns "happy"
//"a.s.k"stIndexOf(".") returns 3
//splitstr = str[str.length-1];//获取“/”之后最后一个字符串
import javax.swing.JLabel;
import javax.swing.JTextField;
import下载.listener.DownloadListener;
public class DownloadWin extends JFrame{
/**
*
*/
private static final long serialVersionUID = -7226361346020458023L;
baseBox.add(boxV2);
Box.createVerticalStrut(15);
baseBox.add(boxV3);
Box.createVerticalStrut(15);
baseBox.add(boxV4);
Box.createVerticalStrut(15);
baseBox.add(boxV5);
public DownloadWin(String title) {
init();
setTitle(title);
setSize(400,300);
setLayout(new GridLayout(2, 1, 0, 0));//为美观考虑,第二行没有使用。
setLocationRelativeTo(null);
System.out.println(fileName);
if(""==fileName||null==fileName||""==fileDir||null==fileDir){
System.out.println("网络地址或保存路径不正确");
infoLabel.setText("网络地址或保存路径不正确");
//in.close();
itStream.close();
import javax.swing.JLabel;
import javax.swing.JTextField;
import下载.util.SplitString;
public class DownloadListener implements ActionListener {
private JTextField httpLine, savePath;
//获取链接指向的文件的相关数据
itStream = conn.getInputStream();
//in = new DataInputStream(itStream);//数据输入流
otStream = new FileOutputStream(file);//根据文件创建输出流
//out = new DataOutputStream(otStream);//数据输出流
首先,这只是一个很简单的程序,所以大神请绕道。
心血来潮,用java编写了一个能访问http链接,并能下载文件的窗体小程序。实测能够正确下载17M的压缩包,图片,文档,html等。但是因为程序效率低下,于是没有继续测试更大的压缩文件(问题可能在于保存输入流到缓冲区的数组)。
程序已打包jar可执行文件,并将代码一齐打包到jar中。为方便使用jar,使用word文档对象包装了它,将下面的对象(显示为一个图标)拖到桌面或者其他文件夹,即可得到该程序:
}
else{
System.out.println("下载中...");
infoLabel.setText("下载中...");
try {
url = new URL(httpAddress);//设置链接
conn = url.openConnection();//打开链接
file = new File(fileDir,fileName);
相关文档
最新文档