图像界面变成简单小例子之二

1.实现效果


实现代码

//金山词霸
package com.hanshunpin.java32;
import javax.swing.*;

import java.awt.*;
public class Demo04 extends JFrame{
	//定义组件
	JSplitPane jsp;
	JList jlist;
	JLabel jl1;
	
	public static void main(String[] args) {
		Demo04 demo04=new Demo04();
	}
	
	public Demo04(){
		//创建组件
		String[] words={"boy","girl","bird","tree"};
		jlist=new JList(words);
		
		jl1=new JLabel(new ImageIcon("image\\ciba.jpg"));
		//创建可拆分窗格
		jsp=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,jlist,jl1);
		//可以变化
		jsp.setOneTouchExpandable(true);

		//设置布局管理器
		
		//添加组件
		this.add(jsp);
		
		//设置大小
		this.setTitle("金山词霸");
		this.setSize(400, 300);
		this.setLocation(400, 400);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		this.setVisible(true);
	}
}

注意事项:

写路径名时:/一个而\\两个

JLable中也可以放图片:

jl1=new JLabel(new ImageIcon("image\\ciba.jpg"));


用到JSplitPane组件


2.实现效果


实现代码

//qq聊天
package com.hanshunpin.java32;

import javax.swing.*;

import java.awt.*;
public class Demo05 extends JFrame {
	JTextArea jta;
	JScrollPane jsp;
	JPanel jp1;
	JComboBox jcb;
	JTextField jtf;
	JButton jb;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Demo05 demo05=new Demo05();
	}
	public Demo05(){
		jta =new JTextArea();
		jsp=new JScrollPane(jta);
		//jta.set
		jp1=new JPanel();
		String[] chatter={"bob","Linda","cindy","Mary"};
		jcb=new JComboBox(chatter);
		jcb.setSelectedIndex(0);
		jb=new JButton("发送");
		jtf=new JTextField(15);
		
		jp1.add(jcb);
		jp1.add(jtf);
		jp1.add(jb);
		
		this.add(jp1,BorderLayout.SOUTH);
		this.add(jsp);
		
		//设置大小
		this.setIconImage((new ImageIcon("image/qq.jpg")).getImage());
		this.setTitle("qq聊天");
		this.setSize(400, 300);
		this.setLocation(400, 400);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		this.setVisible(true);
	}

}

更改界面左上角咖啡式图标:

this.setIconImage((new ImageIcon("image/qq.jpg")).getImage());

BorderLayout布局:上部分居中,下半部分是JPanel,有三个组件

3.实现效果


代码实现

package com.hanshunpin.java32;

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

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class Demo06 extends JFrame {
	//最上边的图片    QQ号码    QQ密码    忘记密码    申请密码保护
	JLabel jl0,jl1,jl2,jl3,jl4;
	//选项卡片
	JTabbedPane jtp;
	//文本框
	JTextField jtf;
	//密码框
	JPasswordField jpf;
	//复选框
	JCheckBox jcb1,jcb2;
	//最底部JPanel
	JPanel jp0;
	//选项片卡3个JPanel
	JPanel jp1,jp2,jp3;
	//请出号码按钮
	JButton jb0;
	//(登录    取消    注册向导) 按钮
	JButton jb1,jb2,jb3;
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Demo06 demo06=new Demo06();
	}
	public Demo06(){
		jtp=new JTabbedPane();
		jl0=new JLabel(new ImageIcon("image/QQtop.jpg"));
		jl1=new JLabel("QQ号码",JLabel.CENTER);
		jl2=new JLabel("QQ密码",JLabel.CENTER);
		jl3=new JLabel("忘记密码",JLabel.CENTER);
		jl3.setFont(new Font("宋体", Font.PLAIN, 16));
		jl3.setForeground(Color.BLUE);
		//普通版
		//jl4=new JLabel("申请密码保护");
		
		//高级版
		jl4=new JLabel("<html><a href='http://www.baidu.com'>申请密码保护</a>");
		jl4.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		
		jtf=new JTextField();
		jpf=new JPasswordField();
		
		jcb1=new JCheckBox("隐身登录");
		jcb2=new JCheckBox("忘记密码");
		
		jb0=new JButton(new ImageIcon("image/delete.jpg"));
		jb1=new JButton(new ImageIcon("image/login.jpg"));
		jb2=new JButton(new ImageIcon("image/concel.jpg"));
		jb3=new JButton(new ImageIcon("image/register.jpg"));
		
		//设置带图片JButton的大小
		jb0.setMargin(new java.awt.Insets(0, 0, 0, 0));
		ImageIcon icon0=new ImageIcon("image/delete.jpg");
		jb0.setPreferredSize(new Dimension(icon0.getIconWidth() - 1, icon0.getIconHeight() - 1));
		
		jb1.setMargin(new java.awt.Insets(0, 0, 0, 0));
		ImageIcon icon1=new ImageIcon("image/login.jpg");
		jb1.setPreferredSize(new Dimension(icon1.getIconWidth() - 1, icon1.getIconHeight() - 1));
		
		jb2.setMargin(new java.awt.Insets(0, 0, 0, 0));
		ImageIcon icon2=new ImageIcon("image/concel.jpg");
		jb2.setPreferredSize(new Dimension(icon2.getIconWidth() - 1, icon2.getIconHeight() - 1));
		
		jb3.setMargin(new java.awt.Insets(0, 0, 0, 0));
		ImageIcon icon3=new ImageIcon("image/register.jpg");
		jb3.setPreferredSize(new Dimension(icon3.getIconWidth() - 1, icon3.getIconHeight() - 1));
		
		
		jp0=new JPanel();
		jp1=new JPanel();
		jp2=new JPanel();
		jp3=new JPanel();
		
		jp0.setPreferredSize(new Dimension(0, 35));
		
		jp1.setLayout(new GridLayout(3,3));
		jp2.setBackground(Color.red);
		jp3.setBackground(Color.blue);
		
		jtp.add(jp1,"QQ号码");
		jtp.add(jp2,"手机号码");
		jtp.add(jp3,"电子邮箱");
		
		jp1.add(jl1);
		jp1.add(jtf);
		jp1.add(jb0);
		jp1.add(jl2);
		jp1.add(jpf);
		jp1.add(jl3);
		jp1.add(jcb1);
		jp1.add(jcb2);
		jp1.add(jl4);
		
		jp0.add(jb1);
		jp0.add(jb2);
		jp0.add(jb3);
		
		this.add(jl0,BorderLayout.NORTH);
		this.add(jtp);
		this.add(jp0,BorderLayout.SOUTH);
		
		//设置大小
		this.setIconImage((new ImageIcon("image/qq.jpg").getImage()));
		this.setTitle("qq聊天");
		this.setSize(290, 220);
		this.setLocation(400, 400);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		this.setVisible(true);	
		
	}

}

知识点:

1.设置带图片按钮的大小:

JButton jb3;

jb3=new JButton(new ImageIcon("image/register.jpg"));

jb3.setMargin(new java.awt.Insets(0, 0, 0, 0));

ImageIcon icon3=new ImageIcon("image/register.jpg");

jb3.setPreferredSize(new Dimension(icon3.getIconWidth() - 1, icon3.getIconHeight() - 1));

2.设置字体

JLabel jl3=new JLabel("忘记密码",JLabel.CENTER);

jl3.setFont(new Font("宋体", Font.PLAIN, 16));

jl3.setForeground(Color.BLUE);

3.设置JLable为链接+由鼠标放上去变手爪

JLabel jl4;

jl4=new JLabel("<html><a href='http://www.baidu.com'>申请密码保护</a>");

jl4.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值