如何用Java制作瓷砖

编程入门 行业动态 更新时间:2024-10-26 04:28:46
本文介绍了如何用Java制作瓷砖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在 Java 中制作一个与此类似的图块:

I want to make a tile similar to this in Java:

因此,tile 由背景图像和应在 Java 程序中设置的文本组成.

So, the tile consists of the background image, and the text which should be set in the Java program.

我曾尝试使用 JLabel 但它只显示背景图像之外的文本.有什么建议吗?

I have tried using JLabel but that only displays the text outside the background image. Any suggestions?

推荐答案

使用 JLabel 作为背景,然后添加另一个 Swing 组件到标签以显示文本.

Use a JLabel as the background and then add another Swing component to the label to display the text.

几个例子:

import java.awt.*; import javax.swing.*; import javax.swing.text.*; public class LabelImageText extends JPanel { public LabelImageText() { JLabel label1 = new JLabel( new ColorIcon(Color.ORANGE, 100, 100) ); label1.setText( "Easy Way" ); label1.setHorizontalTextPosition(JLabel.CENTER); label1.setVerticalTextPosition(JLabel.CENTER); add( label1 ); // JLabel label2 = new JLabel( new ColorIcon(Color.YELLOW, 200, 150) ); label2.setLayout( new BoxLayout(label2, BoxLayout.Y_AXIS) ); add( label2 ); JLabel text = new JLabel( "More Control" ); text.setAlignmentX(JLabel.CENTER_ALIGNMENT); label2.add( Box.createVerticalGlue() ); label2.add( text ); label2.add( Box.createVerticalStrut(10) ); // JLabel label3 = new JLabel( new ColorIcon(Color.GREEN, 200, 150) ); label3.setLayout( new GridBagLayout() ); add( label3 ); JLabel text3 = new JLabel(); text3.setText("<html><center>Text<br>over<br>Image<center></html>"); text3.setLocation(20, 20); text3.setSize(text3.getPreferredSize()); label3.add( text3 ); // JLabel label4 = new JLabel( new ColorIcon(Color.CYAN, 200, 150) ); add( label4 ); JTextPane textPane = new JTextPane(); textPane.setText("Add some text that will wrap at your preferred width"); textPane.setEditable( false ); textPane.setOpaque(false); SimpleAttributeSet center = new SimpleAttributeSet(); StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER); StyledDocument doc = textPane.getStyledDocument(); doc.setParagraphAttributes(0, doc.getLength(), center, false); textPane.setBounds(20, 20, 75, 100); label4.add( textPane ); } public static class ColorIcon implements Icon { private Color color; private int width; private int height; public ColorIcon(Color color, int width, int height) { this.color = color; this.width = width; this.height = height; } public int getIconWidth() { return width; } public int getIconHeight() { return height; } public void paintIcon(Component c, Graphics g, int x, int y) { g.setColor(color); g.fillRect(x, y, width, height); } } private static void createAndShowUI() { JFrame frame = new JFrame("LabelImageText"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add( new LabelImageText() ); frame.pack(); frame.setLocationRelativeTo( null ); frame.setVisible( true ); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } }

更多推荐

如何用Java制作瓷砖

本文发布于:2023-07-10 08:25:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1089833.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:瓷砖   如何用   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!