Java界面的运用以及JLabel的一些实用方法

JLabel 对象可以显示文本、图像或同时显示二者。可以通过设置垂直和水平对齐方式,指定标签显示区中标签内容在何处对齐。默认情况下,标签在其显示区内垂直居中对齐。默认情况下,只显示文本的标签是开始边对齐;而只显示图像的标签则水平居中对齐。

还可以指定文本相对于图像的位置。默认情况下,文本位于图像的结尾边上,文本和图像都垂直对齐。

根据标签的 ComponentOrientation 属性值确定其开始边和结尾边。目前,默认的 ComponentOrientation 设置将开始边映射到左边,将结尾边映射到右边。

构造方法摘要
JLabel()
          创建无图像并且其标题为空字符串的 JLabel。
JLabel(Icon image)
          创建具有指定图像的 JLabel 实例。
JLabel(Icon image, int horizontalAlignment)
          创建具有指定图像和水平对齐方式的 JLabel 实例。
JLabel(String text)
          创建具有指定文本的 JLabel 实例。
JLabel(String text, Icon icon, int horizontalAlignment)
          创建具有指定文本、图像和水平对齐方式的 JLabel 实例。
JLabel(String text, int horizontalAlignment)
          创建具有指定文本和水平对齐方式的 JLabel 实例。
方法摘要


 String getText()
          返回该标签所显示的文本字符串。
 void setText(String text)
          定义此组件将要显示的单行文本。
 Icon getIcon()
          返回该标签显示的图形图像(字形、图标)。

 void setIcon(Icon icon)

下面使我本周用界面做的一个小程序

package com.lanqiao.jiemian;

import java.awt.EventQueue;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Random;

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.border.EmptyBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import java.awt.Image;
import java.awt.Color;
import javax.swing.JScrollPane;

public class TestFrame extends JFrame {

	private JPanel contentPane;
	static ImageIcon icon = new ImageIcon("QQ图片20180625173601.jpg");
	static Image img = icon.getImage();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TestFrame frame = new TestFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public TestFrame() {
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 691, 511);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		

		
		JPanel panel = new JPanel();
		panel.setBounds(-2, 65, 672, 313);
		contentPane.add(panel);
		panel.setLayout(null);
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(0, 0, 257, 313);
		panel.add(scrollPane);
		JLabel lblNewLabel = new JLabel();
		scrollPane.setColumnHeaderView(lblNewLabel);
		img = img.getScaledInstance(300, 436,Image.SCALE_DEFAULT);
		icon.setImage(img);
		img = icon.getImage();
		lblNewLabel.setIcon(icon);
		
		JLabel lblNewLabel_2 = new JLabel(""+"心若不动"+"
"+"风又奈何"+"
"+"你若不伤"+"
"+"岁月无恙"+""); lblNewLabel_2.setFont(new Font("隶书", Font.PLAIN, 24)); lblNewLabel_2.setForeground(Color.PINK); lblNewLabel_2.setBounds(271, 0, 111, 313); panel.add(lblNewLabel_2); JLabel lblNewLabel_1 = new JLabel("小姐姐,想给你说件事,其实我喜欢你好久好久了,做我女朋友好不好。"); lblNewLabel_1.setBounds(0, 0, 670, 49); contentPane.add(lblNewLabel_1); lblNewLabel_1.setForeground(Color.PINK); lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 18)); JButton button = new JButton("不好"); button.setBounds(449, 391, 113, 27); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, "你会答应我的对不对,小姐姐"); } }); contentPane.setLayout(null); contentPane.add(button); JButton button_1 = new JButton("好"); button_1.setBounds(52, 391, 113, 27); button_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, "我就知道你一定会答应我的"); System.exit(0); } }); contentPane.add(button_1); button.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) {//鼠标松开回应 } @Override public void mousePressed(MouseEvent e) {//鼠标点击回应 } @Override public void mouseExited(MouseEvent e) {//鼠标移开回应 } @Override public void mouseEntered(MouseEvent e) {//鼠标接触回应 Random r = new Random(); int x = r.nextInt(674-499); int y = r.nextInt(490-391); button.setBounds(x+300, y+313+44,113 ,27 ); int x1 = e.getX(); int y1 = e.getY(); if((x1<(x+113) && x1>x)&&(y1<(y+27)&&y1>y)) { button_1.setBounds(x+300,y+313+44 , 113, 27); button.setBounds(52, 391, 113, 27); } } @Override public void mouseClicked(MouseEvent e) {//在鼠标点击并松开后回应 } }); addWindowListener(new WindowListener() { @Override public void windowOpened(WindowEvent e) {//打开 } @Override public void windowIconified(WindowEvent e) {//最小化窗口 } @Override public void windowDeiconified(WindowEvent e) { } @Override public void windowDeactivated(WindowEvent e) { } @Override public void windowClosing(WindowEvent e) { JOptionPane.showMessageDialog(null, "小姐姐你还没回答"); } @Override public void windowClosed(WindowEvent e) { } @Override public void windowActivated(WindowEvent e) { } }); } }

你可能感兴趣的:(java)