📄 jlabeldemo.java
字号:
import java.awt.*;
import javax.swing.*;
/**
* Demonstrates the component {@link JLabel}. This class extends
* class {@link @JPanel}.
*
* @author author name
* @version 1.0.0
*/
public class JLabelDemo extends JPanel {
private JLabel labelLeft;
private JLabel labelCenter;
private JLabel labelRight;
/**
* Creates a window.
*
* @param args not used.
*/
public static void main(String[] args) {
JFrame frame = new JFrame("JLabelDemo");
frame.setContentPane(new JLabelDemo());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack(); // Adjust the size of the window
frame.setVisible(true);
}
/**
* Creates three {@link JLabel} components .
*/
public JLabelDemo() {
setLayout(new GridLayout(1, 3));
setBackground(Color.white);
ImageIcon icon = new ImageIcon("images/bananas.gif",
"an image with bananas");
// Create the components
labelLeft = new JLabel("Hello", icon, JLabel.CENTER);
labelCenter = new JLabel("Hello");
labelRight = new JLabel(icon);
// Set the position of the text, relative to the icon
labelLeft.setVerticalTextPosition(JLabel.BOTTOM);
labelLeft.setHorizontalTextPosition(JLabel.CENTER);
labelLeft.setFont(new Font("Serif", Font.BOLD, 16));
// Set the alignment and the font of the label
labelCenter.setHorizontalAlignment(JLabel.CENTER);
labelCenter.setFont(new Font("Serif", Font.BOLD, 30));
// Add labels to the container
add(labelLeft);
add(labelCenter);
add(labelRight);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -