jlabel graphics
时间: 2023-08-26 08:05:08 浏览: 69
JLabel is a Swing component in Java that can display text, images, or both. Graphics in JLabel can be added by using the ImageIcon class.
Here's an example of how to display an image in a JLabel:
```
import javax.swing.*;
import java.awt.*;
public class LabelDemo {
public static void main(String[] args) {
ImageIcon icon = new ImageIcon("image.jpg");
JLabel label = new JLabel(icon);
JFrame frame = new JFrame("Label Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(400, 400));
frame.getContentPane().add(label);
frame.setVisible(true);
}
}
```
In this example, we create an ImageIcon with the image file "image.jpg" and set it as the icon for the JLabel. Then, we create a JFrame and add the JLabel to its content pane. Finally, we set the JFrame to be visible.
阅读全文