第四次总结(swing编写QQ登陆界面)

//QQ登陆界面

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

public class Qlogin {

 /**
  * 程序入口主函数
  */
 public static void main(String[] args) {
  // 实例化一个Qlogin对象
  Qlogin Qlog=new Qlogin();
  // 调用初始化界面方法
  Qlog.InitUI();
 }
 
 /**
  * 初始化界面方法
  */
 public void InitUI(){
  // 新建一个窗体对象
  JFrame jf=new JFrame();
  
  //设置窗体各项属性
  jf.setTitle("QQ2013");
  jf.setSize(380,290);
  jf.setLocation(490, 230);
  jf.setDefaultCloseOperation(3);
  jf.setResizable(false);
  
  setbackground(jf);
  // 添加一个边框布局管理器对象
  jf.setLayout(new BorderLayout());
 
  
  jf.add(newNorthPanel(),BorderLayout.NORTH);
  jf.add(newWestPanel(),BorderLayout.WEST);
  jf.add(newSouthPanel(),BorderLayout.SOUTH);
  jf.add(newCenterPanel(),BorderLayout.CENTER);
  
  // 设置窗体可见
  jf.setVisible(true);
 }
 
 /**
  * 设置窗体背景
  */
 public void setbackground(JFrame jf)
 {
  ImageIcon image=new ImageIcon("images/2.jpg");
  JLabel jlb=new JLabel(image);
  //设置标签大小位置
  jlb.setBounds(0, 0, image.getIconWidth(), image.getIconHeight());
  //得到第二层面板并将标签放在上面
  jf.getLayeredPane().add(jlb,new Integer(Integer.MIN_VALUE));
  JPanel contentPanel=(JPanel)jf.getContentPane();
  //设置透明
  contentPanel.setOpaque(false);
 }
 
 /**
  * 生成中部面板的方法
  */
 public JPanel newCenterPanel(){
  JPanel centerPanel=new JPanel();
  //centerPanel.setBackground(Color.BLACK);
  //添加一个左对齐的流式布局管理器对象
  centerPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
  JComboBox jcb=new JComboBox();
  jcb.addItem("358251441");
  jcb.addItem("406511267");
  jcb.setEditable(true);//设置为可编辑
  jcb.setPreferredSize(new Dimension(180,25));
  centerPanel.add(jcb);
  JLabel laReg=new JLabel("注册账号");
  laReg.setForeground(new Color(27,59,92));
  centerPanel.add(laReg);
  JPasswordField  jpwd=new JPasswordField();
  jpwd.setPreferredSize(new Dimension(180,25));
  centerPanel.add(jpwd);
  JLabel laPwd=new JLabel("找回密码");
  laPwd.setForeground(new Color(27,59,92));
  centerPanel.add(laPwd);
  JCheckBox jch1=new JCheckBox("记住密码");
  jch1.setOpaque(false);
  centerPanel.add(jch1);
  JCheckBox jch2=new JCheckBox("自动登录");
  jch2.setOpaque(false);
  centerPanel.add(jch2);
  //设置面板透明色
  centerPanel.setOpaque(false);
 
  return centerPanel;
 }
 
 /**
  * 生成北部面板的方法
  */
 public JPanel newNorthPanel(){
  JPanel northPanel=new JPanel();
  //设置北部面板的大小
  northPanel.setPreferredSize(new Dimension(0,100));
  //northPanel.setBackground(Color.BLUE);
  northPanel.setOpaque(false);
  return northPanel;
 }
 
 /**
  * 生成西部面板的方法
  */
 public JPanel newWestPanel(){
  JPanel westPanel=new JPanel();
  //设置西部面板的大小
  westPanel.setPreferredSize(new Dimension(105,0));
  //westPanel.setBackground(Color.YELLOW);
  westPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
  //实例化一个ImageIcon类的对象
  ImageIcon image=new ImageIcon("images/1.jpg");
  JLabel jlabelImage=new JLabel(image);
  //设置面板透明色
  westPanel.setOpaque(false);
  //把image加到标签中
  westPanel.add(jlabelImage);
  return westPanel;
 }
 
 /**
  * 生成南部面板的方法
  */
 public JPanel newSouthPanel(){
  JPanel southPanel=new JPanel();
  //设置南部面板的大小
  southPanel.setPreferredSize(new Dimension(0,50));
  //southPanel.setBackground(Color.GREEN);
  //登录按钮对象
  JButton login_bu= new JButton("登             录");
  login_bu.setBackground(new Color(206,219,252));
  login_bu.setPreferredSize(new Dimension(150,30));
  //设置面板透明色
  southPanel.setOpaque(false);
  southPanel.add(login_bu);
  
  
  return southPanel;
 }
}

运行结果如图

第四次总结(swing编写QQ登陆界面)

 

//计算器界面:

 

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Calculator {

 /**入口主函数
  * @param args
  */
 public static void main(String[] args) {
  //实例化一个计算器类对象
  Calculator ca=new Calculator();
  //调用类内部定义的initUI()方法初始化界面
  ca.initUI();
 }

 /** 界面初始化函数
  * @param args
  */
 public void initUI(){
  javax.swing.JFrame jf=new javax.swing.JFrame();
  
  //设置窗体属性值
  jf.setTitle("计算器");
  jf.setSize(300,360);
  jf.setLocation(200, 200);
  jf.setDefaultCloseOperation(3);
  jf.setResizable(false);
  //设置背景色
  //jf.setBackground(new Color(65,143,211));
  //设置背景图片
  ImageIcon image = new ImageIcon("images/calculator.jpg");
  JLabel background = new JLabel(image);
  background.setBounds(0, 0, image.getIconWidth(), image.getIconHeight());
  
  jf.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
  
  JPanel contentPanel=(JPanel)jf.getContentPane();
  contentPanel.setOpaque(false);
  
  //给窗体添加一个BorderLayout管理器对象
  java.awt.BorderLayout bl=new java.awt.BorderLayout();
  jf.setLayout(bl);
  bl.setHgap(5);//设置组件之间的水平间距
  bl.setVgap(10);//设置组件之间的垂直间距
  
  //在窗体的北部嵌套一个面板容器
  JPanel northPanel=new JPanel();
  northPanel.setPreferredSize(new Dimension(0,65));
  northPanel.setOpaque(false);
  JLabel jlb = new JLabel("计算器");
  jlb.setFont(new Font("黑体",1,15));
  northPanel.add(jlb );
  JTextField jtx =new JTextField(22);
  northPanel.add(jtx);
  jf.add(northPanel,bl.NORTH);
  
  //给东、西、南部分别设置面板留白
  JPanel westPanel =new JPanel();
  westPanel.setPreferredSize(new Dimension(20,0));
  westPanel.setOpaque(false);
  JPanel eastPanel =new JPanel();
  eastPanel.setPreferredSize(new Dimension(20,0));
  eastPanel.setOpaque(false);
  JPanel southPanel =new JPanel();
  southPanel.setPreferredSize(new Dimension(0,20));
  southPanel.setOpaque(false);
  
  jf.add(westPanel,BorderLayout.WEST);
  jf.add(eastPanel,BorderLayout.EAST);
  jf.add(southPanel,BorderLayout.SOUTH);
  


  JPanel centerPanel=setCenterPanel();
  //将中部面板添加到窗体中
  jf.add(centerPanel,bl.CENTER);
  
  
  //最后设置显示窗体
  jf.setVisible(true);
 }
 

 /** 中部面板设置函数
  * @return JPanel
  */
 public JPanel setCenterPanel() {
    JPanel jpa2 =new JPanel();
    //设置透明色
    jpa2.setOpaque(false);
    //给jpa2添加一个4行5列、列距和行距分别为10和5的边框布局管理器对象
    jpa2.setLayout(new GridLayout(5,4,10,5));
    //依次创建并添加各个按钮对象
    JButton jbmc =new JButton("mc");
    jpa2.add(jbmc);
    JButton jbmr =new JButton("mr");
    jpa2.add(jbmr);
    JButton jbms =new JButton("ms");
    jpa2.add(jbms);
    JButton jb11 =new JButton("←");
    jpa2.add(jb11);
    JButton jb7 =new JButton("7");
    jb7.setFont(new Font("",1,16));
    jb7.setBackground(new Color(162,171,255));
    jb7.setForeground(Color.WHITE);
    jpa2.add(jb7);
    JButton jb8 =new JButton("8");
    jb8.setFont(new Font("",1,16));
    jb8.setBackground(new Color(162,171,255));
    jb8.setForeground(Color.WHITE);
    jpa2.add(jb8);
    JButton jb9 =new JButton("9");
    jb9.setFont(new Font("",1,16));
    jb9.setBackground(new Color(162,171,255));
    jb9.setForeground(Color.WHITE);
    jpa2.add(jb9);
    JButton jbpl =new JButton("+");
    jpa2.add(jbpl);
    JButton jb4 =new JButton("4");
    jb4.setFont(new Font("",1,16));
    jb4.setBackground(new Color(162,171,255));
    jb4.setForeground(Color.WHITE);
    jpa2.add(jb4);
    JButton jb5 =new JButton("5");
    jb5.setFont(new Font("",1,16));
    jb5.setBackground(new Color(162,171,255));
    jb5.setForeground(Color.WHITE);
    jpa2.add(jb5);
    JButton jb6 =new JButton("6");
    jb6.setFont(new Font("",1,16));
    jb6.setBackground(new Color(162,171,255));
    jb6.setForeground(Color.WHITE);
    jpa2.add(jb6);
    javax.swing.JButton jbmi =new JButton("—");
    jpa2.add(jbmi);
    JButton jb1 =new JButton("1");
    jb1.setFont(new Font("",1,16));
    jb1.setBackground(new Color(162,171,255));
    jb1.setForeground(Color.WHITE);
    jpa2.add(jb1);
    JButton jb2 =new JButton("2");
    jb2.setFont(new Font("",1,16));
    jb2.setBackground(new Color(162,171,255));    
    jb2.setForeground(Color.WHITE);
    jpa2.add(jb2);
    JButton jb3 =new JButton("3");
    jb3.setFont(new Font("",1,16));
    jb3.setBackground(new Color(162,171,255));
    jb3.setForeground(Color.WHITE);
    jpa2.add(jb3);
    JButton jbmu =new JButton("×");
    jpa2.add(jbmu);
    JButton jb0 =new JButton("0");
    jpa2.add(jb0);
    JButton jbpo =new JButton(". ");
    jpa2.add(jbpo);
    JButton jbis =new JButton("=");
    jpa2.add(jbis);
    JButton jbdi =new JButton("÷");
    jpa2.add(jbdi);
    
    
    return jpa2;
  
 }
}

运行结果如图:
第四次总结(swing编写QQ登陆界面)

 

 

你可能感兴趣的:(swing)