Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JButton settext特定位置_Java_Swing_Layout_Jbutton_Settext - Fatal编程技术网

Java JButton settext特定位置

Java JButton settext特定位置,java,swing,layout,jbutton,settext,Java,Swing,Layout,Jbutton,Settext,我有一个带有图像图标的按钮,我需要定位文本。 有没有一种方法可以将JButton的文本定位在特定位置,而不是使用居中、前导、顶部等?您可以使用设置边界方法来设置按钮上文本的位置 JLabel positiongLabel = new JLabel(); positiongLabel.setBounds(100,50,300,30); 有关更多详细信息,请单击我以前从未在JButton上尝试过它,但也许您可以在按钮上添加一个JLabel作为组件。然后,可以使用布局管理器或边框来更恰当地定位标签

我有一个带有图像图标的按钮,我需要定位文本。
有没有一种方法可以将JButton的文本定位在特定位置,而不是使用居中、前导、顶部等?

您可以使用设置边界方法来设置按钮上文本的位置

JLabel positiongLabel = new JLabel();
positiongLabel.setBounds(100,50,300,30);

有关更多详细信息,请单击

我以前从未在JButton上尝试过它,但也许您可以在按钮上添加一个JLabel作为组件。然后,可以使用布局管理器或边框来更恰当地定位标签

以下是使用图标向JLabel添加组件的一些示例:

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(); } }); } }
我怀疑JTExtPane示例是否能与按钮一起工作,因为文本窗格将截获鼠标事件,因此在单击文本时无法单击按钮。因此,我将继续使用为按钮添加标签的示例。

西班牙语: Puedes sobreescribir el método setText(字符串str)

GoogleTraductorEnglish:您可以覆盖方法setText(stringstr)

private静态最终字符串HTML_1=”“;
私有静态最终字符串HTML_2=“”;
@凌驾
公共void setText(字符串txt){
if(txt.isEmpty()){
txt=“Dispositivo”;
}
字符串txt2=HTML_1+txt+HTML_2;
super.setText(txt2);
}

短答案,不。长答案,视情况而定,但您可以使用
获得更多控制,但您需要直接在html中混合图像和文本…@camickr现在不取决于SWT您如何使用AWT组件而不是Swing组件。而且,它也不能回答这个问题。问题在于如何将文本定位在按钮内,而不是如何将按钮定位在框架内。此外,不应使用setBounds()sot位置组件。你应该使用布局管理器。谢谢你的帮助。另外,如何使用相同的标签文本?我在做i18n,每次切换区域设置时,它都会在先前创建的标签上添加一个新标签,或者不会重置文本。我可能没有重置文本。这是您需要手动执行的操作。我不确定你会用什么样的听众。
private static final String HTML_1 = "<html><body style='margin:0px 0px 0px 0px'>";
private static final String HTML_2 = "</html>";

@Override
public void setText(String txt) {
    if (txt.isEmpty()) {
        txt = "Dispositivo";
    }
    String txt2 = HTML_1 + txt + HTML_2;
    super.setText(txt2);
}