Java 带图像背景和文本对齐的JButton

Java 带图像背景和文本对齐的JButton,java,swing,jbutton,Java,Swing,Jbutton,我(再次)遇到了一个问题:我有一个背景上有图像的jbutton,但当我想在它上面放一些文本时,它会出现在背景的右侧,不是在按钮中,而是在旁边。。。 这是工作代码,您只能链接一些图像:) 您从不设置背景,您可以设置按钮的图标。您从不设置背景,您可以设置按钮的图标。以下是4种在图像上显示文本的方法: import java.awt.*; import javax.swing.*; import javax.swing.text.*; public class LabelImageText exte

我(再次)遇到了一个问题:我有一个背景上有图像的jbutton,但当我想在它上面放一些文本时,它会出现在背景的右侧,不是在按钮中,而是在旁边。。。 这是工作代码,您只能链接一些图像:)


您从不设置背景,您可以设置按钮的图标。您从不设置背景,您可以设置按钮的图标。以下是4种在图像上显示文本的方法:

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();
            }
        });
    }
}
import java.awt.*;
导入javax.swing.*;
导入javax.swing.text.*;
公共类LabelImageText扩展了JPanel
{
公共LabelImageText()
{
JLabel label1=新的JLabel(新的颜色图标(Color.ORANGE,100100));
标签1.setText(“简易方式”);
标签1.设置水平文本位置(JLabel.中心);
标签1.设置垂直文本位置(JLabel.中心);
添加(标签1);
//
JLabel label2=新的JLabel(新的颜色图标(Color.YELLOW,200150));
label2.setLayout(新的BoxLayout(label2,BoxLayout.Y_轴));
添加(标签2);
JLabel text=新的JLabel(“更多控制”);
text.setAlignmentX(JLabel.CENTER\u对齐);
label2.add(Box.createVerticalGlue());
标签2.添加(文本);
标签2.添加(框。创建垂直支柱(10));
//
JLabel label3=新的JLabel(新的颜色图标(Color.GREEN,200150));
label3.setLayout(新的GridBagLayout());
添加(标签3);
JLabel text3=新的JLabel();
text3.setText(“文本

图像上方”); text3.设置位置(20,20); text3.setSize(text3.getPreferredSize()); 标签3.添加(文本3); // JLabel label4=新的JLabel(新的ColorIcon(Color.CYAN,200150)); 添加(标签4); JTextPane textPane=新的JTextPane(); setText(“添加一些以您喜欢的宽度换行的文本”); textPane.setEditable(false); textPane.set不透明(false); SimpleAttributeSet center=新的SimpleAttributeSet(); StyleConstants.setAlignment(中心,StyleConstants.ALIGN\u中心); StyledDocument doc=textPane.getStyledDocument(); doc.setParagraphAttributes(0,doc.getLength(),居中,false); textPane.setBounds(20,20,75,100); 标签4.添加(文本窗格); } 公共静态类ColorIcon实现Icon { 私人色彩; 私有整数宽度; 私人内部高度; 公共颜色图标(颜色、整数宽度、整数高度) { 这个颜色=颜色; 这个。宽度=宽度; 高度=高度; } public int getIconWidth() { 返回宽度; } public int getIconHeight() { 返回高度; } 公共虚空绘制图标(组件c、图形g、整数x、整数y) { g、 设置颜色(颜色); g、 fillRect(x,y,宽度,高度); } } 私有静态void createAndShowUI() { JFrame=新的JFrame(“LabelImageText”); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(新LabelImageText()); frame.pack(); frame.setLocationRelativeTo(空); frame.setVisible(true); } 公共静态void main(字符串[]args) { invokeLater(新的Runnable() { 公开募捐 { createAndShowUI(); } }); } }
以下是4种在图像上显示文本的方法:

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();
            }
        });
    }
}
import java.awt.*;
导入javax.swing.*;
导入javax.swing.text.*;
公共类LabelImageText扩展了JPanel
{
公共LabelImageText()
{
JLabel label1=新的JLabel(新的颜色图标(Color.ORANGE,100100));
标签1.setText(“简易方式”);
标签1.设置水平文本位置(JLabel.中心);
标签1.设置垂直文本位置(JLabel.中心);
添加(标签1);
//
JLabel label2=新的JLabel(新的颜色图标(Color.YELLOW,200150));
label2.setLayout(新的BoxLayout(label2,BoxLayout.Y_轴));
添加(标签2);
JLabel text=新的JLabel(“更多控制”);
text.setAlignmentX(JLabel.CENTER\u对齐);
label2.add(Box.createVerticalGlue());
标签2.添加(文本);
标签2.添加(框。创建垂直支柱(10));
//
JLabel label3=新的JLabel(新的颜色图标(Color.GREEN,200150));
label3.setLayout(新的GridBagLayout());
添加(标签3);
JLabel text3=新的JLabel();
text3.setText(“文本

图像上方”); text3.设置位置(20,20); text3.setSize(text3.getPreferredSize()); 标签3.添加(文本3); // JLabel label4=新的JLabel(新的ColorIcon(Color.CYAN,200150)); 添加(标签4); JTextPane textPane=新的JTextPane(); setText(“添加一些以您喜欢的宽度换行的文本”); textPane.setEditable(false); textPane.set不透明(false); SimpleAttributeSet center=新的SimpleAttributeSet(); StyleConstants.setAlignment(中心,StyleConstants.ALIGN\u中心); StyledDocument doc=textPane.getStyledDocument(); doc.setParagraphAttributes(0,doc.getLength(),居中,false); textPane.setBounds(20,20,75,100); 标签4.添加(文本窗格); } 公共静态类ColorIcon实现Icon { 私人色彩; 私有整数宽度; 私人内部高度; 公共颜色图标(颜色、整数宽度、整数高度) { 这个颜色=颜色; 这个。宽度=宽度; 高度=高度; } public int getIconWidth() { 返回宽度; } public int getIconHeight() { 返回高度; } 公共虚空绘制图标(组件c、图形g、整数x、整数y) { g、 设置颜色(颜色); g、 f
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class StringTest extends JFrame
        implements ActionListener
{
  private JTextField input, result;
  private int i;

  public StringTest()
  {
    super("String test");
    i=0;

    Box box1 = Box.createVerticalBox();
    box1.add(new JLabel(" Input:"));
    box1.add(Box.createVerticalStrut(10));
    box1.add(new JLabel("Result:"));

    input = new JTextField(40);
    input.setBackground(Color.WHITE);
    input.addActionListener(this);
    input.selectAll();

    result = new JTextField(40);
    result.setBackground(Color.YELLOW);
    result.setEditable(false);

    Box box2 = Box.createVerticalBox();
    box2.add(input);
    box2.add(Box.createVerticalStrut(10));
    box2.add(result);

    Box box3 = Box.createHorizontalBox(); 
    box3.add(box1);
    box3.add(box2);

    Box box4 = Box.createVerticalBox();
    JButton new_game = new JButton("NEW GAME");
    JLabel game_title =new JLabel("***Welcome to Hangman Game***");
    box4.add(game_title);

    box4.add(box3);
    box4.add(Box.createVerticalStrut(10));
    box4.add(box3);
    box4.add(Box.createVerticalStrut(10));
    box4.add(new_game);
    new_game.setAlignmentX(Component.CENTER_ALIGNMENT);

    Container c = getContentPane();
    c.setLayout(new FlowLayout());
    c.add(box4);
    //c.add(box2);
   // c.add(ok_button);c.add(ok1_button);
    input.requestFocus();
  }



  public void actionPerformed(ActionEvent e)
  {

    String str = input.getText();

    result.setText(str);
    if (i%2==0)
       result.setBackground(Color.WHITE);
    else
       result.setBackground(Color.YELLOW);
    input.selectAll();
  }

  public static void main(String[] args)
  {
    StringTest window = new StringTest();
    window.setBounds(100, 100, 600, 100);
    window.setDefaultCloseOperation(EXIT_ON_CLOSE);
    window.setVisible(true);
  }
}