📜  J标签 | Java摇摆

📅  最后修改于: 2022-05-13 01:55:29.578000             🧑  作者: Mango

J标签 | Java摇摆

JLabel 是Java Swing 的一个类。 JLabel 用于显示短字符串或图像图标。 JLabel 可以显示文本、图像或两者。 JLabel 只是文本或图像的显示,它不能获得焦点。 JLabel 无法输入鼠标焦点或键盘焦点等事件。默认情况下,标签垂直居中,但用户可以更改标签的对齐方式。
该类的构造函数是:

  1. JLabel() :创建一个没有文本或图像的空白标签。
  2. JLabel(String s) :使用指定的字符串创建一个新标签。
  3. JLabel(Icon i) :创建一个带有图像的新标签。
  4. JLabel(String s, Icon i, int align) :创建一个带有字符串、图像和指定水平对齐的新标签

该类常用的方法有:

  1. getIcon() :返回标签显示的图像
  2. setIcon(Icon i) :将标签显示的图标设置为图像 i
  3. getText() :返回标签将显示的文本
  4. setText(String s) :将标签显示的文本设置为字符串s

1. 程序创建一个空白标签并向其添加文本。

Java
// Java Program to create a
// blank label and add text to it.
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class text extends JFrame {
 
    // frame
    static JFrame f;
 
    // label to display text
    static JLabel l;
 
    // default constructor
    text()
    {
    }
 
    // main class
    public static void main(String[] args)
    {
        // create a new frame to store text field and button
        f = new JFrame("label");
 
        // create a label to display text
        l = new JLabel();
 
        // add text to label
        l.setText("label text");
 
        // create a panel
        JPanel p = new JPanel();
 
        // add label to panel
        p.add(l);
 
        // add panel to frame
        f.add(p);
 
        // set the size of frame
        f.setSize(300, 300);
 
        f.show();
    }
}


Java
// Java Program to create a new label
// using constructor - JLabel(String s)
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class text extends JFrame {
 
    // frame
    static JFrame f;
 
    // label to display text
    static JLabel l;
 
    // default constructor
    text()
    {
    }
 
    // main class
    public static void main(String[] args)
    {
        // create a new frame to store text field and button
        f = new JFrame("label");
 
        // create a label to display text
        l = new JLabel("new text ");
 
        // create a panel
        JPanel p = new JPanel();
 
        // add label to panel
        p.add(l);
 
        // add panel to frame
        f.add(p);
 
        // set the size of frame
        f.setSize(300, 300);
 
        f.show();
    }
}


Java
// Java Program to create  a label
// and add image to it .
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class text extends JFrame {
 
    // frame
    static JFrame f;
 
    // label to display text
    static JLabel l;
 
    // default constructor
    text()
    {
    }
 
    // main class
    public static void main(String[] args)
    {
        // create a new frame to store text field and button
        f = new JFrame("label");
 
        // create a new image icon
        ImageIcon i = new ImageIcon("f:/image.png");
 
        // create a label to display image
        l = new JLabel(i);
 
        // create a panel
        JPanel p = new JPanel();
 
        // add label to panel
        p.add(l);
 
        // add panel to frame
        f.add(p);
 
        // set the size of frame
        f.setSize(500, 500);
 
        f.show();
    }
}


Java
// Java Program to add a image and string
// to a label with horizontal alignment
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class text extends JFrame {
 
    // frame
    static JFrame f;
 
    // label to display text
    static JLabel l;
 
    // default constructor
    text()
    {
    }
 
    // main class
    public static void main(String[] args)
    {
        // create a new frame to store text field and button
        f = new JFrame("label");
 
        // create a new image icon
        ImageIcon i = new ImageIcon("f:/image.png");
 
        // create a label to display text and image
        l = new JLabel("new image text ", i, SwingConstants.HORIZONTAL);
 
        // create a panel
        JPanel p = new JPanel();
 
        // add label to panel
        p.add(l);
 
        // add panel to frame
        f.add(p);
 
        // set the size of frame
        f.setSize(600, 500);
 
        f.show();
    }
}


输出 :

2. 使用构造函数创建新标签的程序 - JLabel(String s)

Java

// Java Program to create a new label
// using constructor - JLabel(String s)
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class text extends JFrame {
 
    // frame
    static JFrame f;
 
    // label to display text
    static JLabel l;
 
    // default constructor
    text()
    {
    }
 
    // main class
    public static void main(String[] args)
    {
        // create a new frame to store text field and button
        f = new JFrame("label");
 
        // create a label to display text
        l = new JLabel("new text ");
 
        // create a panel
        JPanel p = new JPanel();
 
        // add label to panel
        p.add(l);
 
        // add panel to frame
        f.add(p);
 
        // set the size of frame
        f.setSize(300, 300);
 
        f.show();
    }
}

输出 :

3. 程序创建标签并添加图像。

Java

// Java Program to create  a label
// and add image to it .
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class text extends JFrame {
 
    // frame
    static JFrame f;
 
    // label to display text
    static JLabel l;
 
    // default constructor
    text()
    {
    }
 
    // main class
    public static void main(String[] args)
    {
        // create a new frame to store text field and button
        f = new JFrame("label");
 
        // create a new image icon
        ImageIcon i = new ImageIcon("f:/image.png");
 
        // create a label to display image
        l = new JLabel(i);
 
        // create a panel
        JPanel p = new JPanel();
 
        // add label to panel
        p.add(l);
 
        // add panel to frame
        f.add(p);
 
        // set the size of frame
        f.setSize(500, 500);
 
        f.show();
    }
}

输出 :

4. 将图像和字符串添加到标签的程序

Java

// Java Program to add a image and string
// to a label with horizontal alignment
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class text extends JFrame {
 
    // frame
    static JFrame f;
 
    // label to display text
    static JLabel l;
 
    // default constructor
    text()
    {
    }
 
    // main class
    public static void main(String[] args)
    {
        // create a new frame to store text field and button
        f = new JFrame("label");
 
        // create a new image icon
        ImageIcon i = new ImageIcon("f:/image.png");
 
        // create a label to display text and image
        l = new JLabel("new image text ", i, SwingConstants.HORIZONTAL);
 
        // create a panel
        JPanel p = new JPanel();
 
        // add label to panel
        p.add(l);
 
        // add panel to frame
        f.add(p);
 
        // set the size of frame
        f.setSize(600, 500);
 
        f.show();
    }
}

输出 :

注意:此程序可能无法在在线编译器中运行,请使用离线 IDE。