发现网上有个很好的接口,用来生成二维码,于是做了一个简单的DEMO,希望对新手有帮助。
下面是项目目录,很简单,就2个类:
//Main.java
package main;
import java.awt.Frame;
public class Main extends Frame {
private static final long serialVersionUID = 1L;
public static void main(String[] args) throws Exception {
new MyFrame().display();
}
}
//MyFrame.java
package main;
import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MyFrame extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel imagePanel;
private JPanel editPanel;
private JButton okBtn;
private JButton clearBtn;
private JTextField msgText;
JLabel imageLabel;
ImageIcon imageIcon;
public MyFrame() throws Exception {
this.setTitle("二维码生成器");
this.setSize(500, 600);
Toolkit kit = Toolkit.getDefaultToolkit();
this.setLocation((int) (this.getWidth() / 2 - kit.getScreenSize().getWidth() / 2),
(int) (this.getHeight() / 2 - kit.getScreenSize().getHeight() / 2));
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
imagePanel = new JPanel();
imageLabel = new JLabel();
imagePanel.add(imageLabel);
this.getContentPane().add(new JLabel("二维码生成器"), BorderLayout.NORTH);
this.getContentPane().add(imagePanel, BorderLayout.CENTER);
editPanel = new JPanel();
msgText = new JTextField(30);
msgText.setText("在这里输入你想显示的内容");
okBtn = new JButton("确定");
clearBtn = new JButton("清空");
editPanel.add(msgText, BorderLayout.WEST);
editPanel.add(okBtn, BorderLayout.CENTER);
editPanel.add(clearBtn, BorderLayout.EAST);
this.getContentPane().add(editPanel, BorderLayout.SOUTH);
clearBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
msgText.setText("");
}
});
okBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
imageLabel
.setIcon(new ImageIcon(new URL("http://qr.topscan.com/api.php?text=" + msgText.getText())));
System.out.println("http://qr.topscan.com/api.php?text=" + msgText.getText());
} catch (MalformedURLException e1) {
JOptionPane.showInternalMessageDialog(MyFrame.this, "出错了", "二维码生成出错,请稍后再试!",
JOptionPane.INFORMATION_MESSAGE);
}
}
});
}
public void display() {
this.setVisible(true);
}
}