代码说明:
通过使用Jfame中的组件(Jpanel,JTextFiled,JLabel,ImageIcon,Jbutton等类)还有Jframe中事件监听器的设置通过Eclipse编译运行得到一个简单的界面.
布局如下:
点击‘确定’按钮会弹出另一个界面(运用事件监听器):
完整源码如下:
package ui1;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.prefs.BackingStoreException;
import javax.swing.*;
//import javax.swing.text.PlainView;
public class Main extends JFrame {
JPanel pa1 = new JPanel();
JPanel pa2 = new JPanel();
public static void main(String[] args){
Main jf = new Main();
}
public Main(){
//this.setLayout(null);
this.setTitle("xxx影院(登录界面)");
pa1.setLayout(null);
pa1.setOpaque(false);
pa1.setSize(800,100);
ImageIcon backgrod = new ImageIcon("jj.png");
JLabel lab5 = new JLabel(backgrod);
lab5.setBounds(0,0, 800, 600);
this.getLayeredPane().setLayout(null);
this.getLayeredPane().add(lab5,new Integer(Integer.MIN_VALUE));
((JPanel) this.getContentPane()).setOpaque(false);
JLabel lab1 = new JLabel("欢迎光临×××影院");
Font font = new Font("宋体",Font.BOLD,28);
lab1.setFont(font);
lab1.setBounds(250,30, 300,50);
pa1.add(lab1);
//::::::::::::::::::::::::::::::::::::::::::::::::
pa2.setLayout(null);
pa2.setOpaque(false);
pa2.setSize(800,400);
Font font1 = new Font("宋体",Font.PLAIN,15);
JLabel lab2 = new JLabel("用户名:");
lab2.setBounds(200,170,60,30);
lab2.setFont(font1);
JLabel lab3 = new JLabel("密码:");
lab3.setBounds(200,220,50,30);
lab3.setFont(font1);
JLabel lab4 = new JLabel("验证码:");
lab4.setBounds(200, 270, 60, 30);
lab4.setFont(font1);
// imagepane.setLayout(null);
//
JTextField jtex1 = new JTextField();
jtex1.setBounds(250,170,250,30);
JTextField jtex2 = new JTextField();
jtex2.setBounds(250, 220, 250, 30);
JTextField jtex3 = new JTextField();
jtex3.setBounds(350, 270, 80, 30);
Vccode vcc1 = new Vccode();
vcc1.setBounds(250, 270,40,40);
pa2.add(lab2);
pa2.add(lab3);
pa2.add(lab4);
//:::::::::::::::::::::::::::: ::::::
pa2.add(jtex1);
pa2.add(jtex2);
pa2.add(jtex3);
pa2.add(vcc1);
// this.addWindowListener( new windows());
this.add(pa1);
this.add(pa2);
this.setSize(800,600);
this.setResizable(false);
this.setVisible(true);
Main1();
//Panel pa1 = new Panel();
// this.dispose();
}
public void Main1(){
JButton bu1 = new JButton("确定");
bu1.setBounds(250, 350, 80 ,30);
bu1.addActionListener( new bu1Y());
// this.dispose();
JButton bu2 = new JButton("注册");
bu2.setBounds(400, 350, 80 ,30);
bu2.addActionListener(new bu2Y());
pa2.add(bu1);
pa2.add(bu2);
}
}
//确定按钮事件监听器
class bu1Y implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Confirm con = new Confirm();
// con.Imagg();
}
}
//注册按钮事件监听器
class bu2Y implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
package ui1;
import java.awt.Font;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Confirm extends JFrame{
private final static int WIDTH = 150;
private final static int HEIGHT = 200;
public Confirm(){
this.setTitle("xxx电影展示厅");
JPanel Cpan1 = new JPanel();
Cpan1.setLayout(null);
Cpan1.setOpaque(false);
Cpan1.setSize(750,80);
Ui_display();
Ui_dianying_display();
//this.dispose();
this.setSize(750,850);
this.setResizable(false);
this.setVisible(true);
}
public void Ui_display(){
JPanel Cpan1 = new JPanel();
Cpan1.setLayout(null);
Cpan1.setOpaque(false);
Cpan1.setSize(750,80);
JLabel Ilab = new JLabel("xxx电影展示厅");
Font font = new Font("宋体",Font.BOLD,28);
Ilab.setFont(font);
Ilab.setBounds(250,30, 300,50);
Cpan1.add(Ilab);
Cpan1.setOpaque(false);
this.add(Cpan1);
//JPanel Cpan2 = new JPanel();
ImageIcon backgrod = new ImageIcon("jj.png");
backgrod.setImage(backgrod.getImage().getScaledInstance(750, 850, Image.SCALE_DEFAULT));
JLabel Ilab7 = new JLabel(backgrod);
Ilab7.setBounds(0,0, 750, 850);
this.getLayeredPane().setLayout(null);
this.getLayeredPane().add(Ilab7,new Integer(Integer.MIN_VALUE));
((JPanel) this.getContentPane()).setOpaque(false);
}
public void Ui_dianying_display(){
JPanel Cpan2 = new JPanel();
Cpan2.setLayout(null);
Cpan2.setOpaque(false);
Cpan2.setSize(750,770);
Cpan2.setOpaque(false);
ImageIcon lin1 = new ImageIcon("sishi.png");
lin1.setImage(lin1.getImage().getScaledInstance(this.WIDTH, this.HEIGHT,Image.SCALE_DEFAULT));//调整图片的比例能够正常的在界面中显示出来
ImageIcon lin2 = new ImageIcon("shenhua.png");
lin2.setImage(lin2.getImage().getScaledInstance(this.WIDTH, this.HEIGHT,Image.SCALE_DEFAULT));
ImageIcon lin3 = new ImageIcon("tiequan.png");
lin3.setImage(lin3.getImage().getScaledInstance(this.WIDTH, this.HEIGHT,Image.SCALE_DEFAULT));
ImageIcon lin4 = new ImageIcon("sansheng.png");
lin4.setImage(lin4.getImage().getScaledInstance(this.WIDTH, this.HEIGHT,Image.SCALE_DEFAULT));
ImageIcon lin5 = new ImageIcon("yueyu.png");
lin5.setImage(lin5.getImage().getScaledInstance(this.WIDTH, this.HEIGHT,Image.SCALE_DEFAULT));
ImageIcon lin6 = new ImageIcon("真皮秘籍.png");
lin6.setImage(lin6.getImage().getScaledInstance(this.WIDTH, this.HEIGHT,Image.SCALE_DEFAULT));
JLabel Ilab1 = new JLabel(lin1);
//Ilab1.setIcon(lin1);
Ilab1.setBounds(85, 100,150,200);
JLabel Ilab2 = new JLabel(lin2);
Ilab2.setBounds(310, 100, 150, 200);
JLabel Ilab3 = new JLabel(lin3);
Ilab3.setBounds(535, 100,150, 200);
JLabel Ilab4 = new JLabel(lin4);
Ilab4.setBounds(85, 400,150,200);
JLabel Ilab5 = new JLabel(lin5);
Ilab5.setBounds(310, 400,150,200);
JLabel Ilab6 = new JLabel(lin6);
Ilab6.setBounds(535, 400,150,200);
Cpan2.add(Ilab1);
Cpan2.add(Ilab2);
Cpan2.add(Ilab3);
Cpan2.add(Ilab4);
Cpan2.add(Ilab5);
Cpan2.add(Ilab6);
this.add(Cpan2);
}
}
package ui1;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
import javax.swing.JComboBox;
import javax.swing.JComponent;
public class Vccode extends JComponent implements MouseListener{
private String code;
private int width,hight = 40;
private int codelength = 4;
private Random random = new Random();
public Vccode(){
width = this.codelength*16+(this.codelength-1)*10;
setPreferredSize(new Dimension());
setSize(width,hight);
this.addMouseListener(this);
setToolTipText("点击更换验证码");
}
public int getCodelength(){
return codelength;
}
//验证验证码的长度
public void setCodelength(int codelength){
if(codelength<4){
this.codelength=4;
}
else{
this.codelength=codelength;
}
}
public String getCode(){
return code;
}
//设置颜色
public Color getRandColor(int min, int max) {
if (min > 255)
min = 255;
if (max > 255)
max = 255;
int red = random.nextInt(max - min) + min;
int green = random.nextInt(max - min) + min;
int blue = random.nextInt(max - min) + min;
return new Color(red, green, blue);
}
/*
设置验证码具体的字母是什么
*/
protected String generateCode() {
char[] codes = new char[this.codelength];
for (int i = 0, len = codes.length; i < len; i++) {
if (random.nextBoolean()) {
codes[i] = (char) (random.nextInt(26) + 65);
} else {
codes[i] = (char) (random.nextInt(26) + 97);
}
}
this.code = new String(codes);
return this.code;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if(this.code == null || this.code.length() != this.codelength) {
this.code = generateCode();
}
width = this.codelength * 16 + (this.codelength - 1) * 10;
super.setSize(width, hight);
super.setPreferredSize(new Dimension(width, hight));
Font mFont = new Font("Arial", Font.BOLD | Font.ITALIC, 25);
g.setFont(mFont);
//绘制出验证码的背景的矩形轮廓
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(getRandColor(200, 250));
g2d.fillRect(0, 0, width, hight);
g2d.setColor(getRandColor(180, 200));
g2d.drawRect(0, 0, width - 1, hight - 1);
//绘制出验证码背景的线
int i = 0, len = 150;
for (; i < len; i++) {
int x = random.nextInt(width - 1);
int y = random.nextInt(hight - 1);
int x1 = random.nextInt(width - 10) + 10;
int y1 = random.nextInt(hight - 4) + 4;
g2d.setColor(getRandColor(180, 200));
g2d.drawLine(x, y, x1, y1);
}
//绘制出验证码的具体字母
i = 0; len = this.codelength;
FontMetrics fm = g2d.getFontMetrics();
int base = (hight - fm.getHeight())/2 + fm.getAscent();
for(;i<len;i++) {
int b = random.nextBoolean() ? 1 : -1;
g2d.rotate(random.nextInt(10)*0.01*b);
g2d.setColor(getRandColor(20, 130));
g2d.drawString(code.charAt(i)+"", 16 * i + 10, base);
}
}
//下一个验证码
public void nextCode() {
generateCode();
repaint();
}
@Override
public void mouseClicked(MouseEvent e) {
nextCode();
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
还有后续功能需要补全;
心得与体会:
设置背景图片的时候,各个组件一定需要注意设置为透明,使用setOpaque()方法,参数为false。在界面里面插入图片的时候,SetBounds()函数并不能直接改变图片的大小,需要调整比例,使用getScaledInstance()函数来调整图片比例大小达到想要的效果;
各个面板之间尽量不要覆盖,这里的界面现在只有很简单的一部分,后续还有很多页面会补齐。