Java 在文本按钮中放置标签?
我想对自定义按钮使用Java 在文本按钮中放置标签?,java,libgdx,scene2d,Java,Libgdx,Scene2d,我想对自定义按钮使用TextButton,但这需要我(重新)将text或标签放置在该TextButton内。我尝试了一些选择,但我被卡住了 //this seems straightforward but nothing changes. final TextButton b = new TextButton("LABEL", skin); b.getLabel().setPosition(100, -20); //Read somewhere about adding new childre
TextButton
,但这需要我(重新)将text
或标签
放置在该TextButton
内。我尝试了一些选择,但我被卡住了
//this seems straightforward but nothing changes.
final TextButton b = new TextButton("LABEL", skin);
b.getLabel().setPosition(100, -20);
//Read somewhere about adding new children to buttons but no success either.
final TextButton b = new TextButton("OLD LABEL", skin);
b.clearChildren();
Label l = new Label("NEW LABEL", skin);
l.setPosition(400, 600);
b.add(l);
b.layout(); // Does not help either.
我正在将按钮
直接添加到阶段
,而没有一个表来说明它的重要性。有人对此有什么想法吗
texbutton是一个按钮,他们在构造函数中为其添加了标签
label = new Label(text, new LabelStyle(style.font, style.fontColor));
label.setAlignment(Align.top);
add(label).expand().fill();
首先,存在或看不到任何更改标签对齐方式的方法
label.setAlignment(Align.top);
另一方面,标签被添加到一个新的单元格中,演员
add(label).expand().fill();
知道我唯一想到的事情就是按照我所展示的方式建造一些房屋,这不是很好,但我想你会的
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.utils.Align;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
import com.esotericsoftware.tablelayout.Cell;
public class LabelButton extends Button {
private final Label label;
private TextButtonStyle style;
private float widthButton, heightButton;
public LabelButton (String text, Skin skin) {
this(text, skin.get(TextButtonStyle.class));
setSkin(skin);
}
//NEW CODE1 FOR ADD, NEW CLASS EXTENDS BUTTON, OTHER CODE EQUALS TEXTBUTTON.
/** @param boolean alignament: false desactiva la alineación al centro, x e y son las coordenadas donde se mostrar el texto del label.
* <p>
* xLabel, e yLabel tienen su pivote en el centro del texto, asi que las posiciones son basadas respecto a esto.
* <p>
* las variables float widthButton, y float heightButton, referidos a el boton.
* <p>
* boolean alignament: false deactivate center alignment, x and y are the coordinates where show the text label.
* <p>
* xLabel, ylabel and have its pivot in the center of the text, so the positions are based on this.
* <p>
* las variables widthButton float and float heightButton, referring to the button
*/
public LabelButton (String text, Skin skin, boolean alignament, float xLabel, float yLabel, float widthButton, float heightButton) {
this(text, skin.get(TextButtonStyle.class), alignament, xLabel, yLabel, widthButton, heightButton);
setSkin(skin);
}
//END NEW CODE1
public LabelButton (String text, Skin skin, String styleName) {
this(text, skin.get(styleName, TextButtonStyle.class));
setSkin(skin);
}
public LabelButton (String text, TextButtonStyle style) {
super();
setStyle(style);
this.style = style;
label = new Label(text, new LabelStyle(style.font, style.fontColor));
label.setAlignment(Align.top);
add(label).expand().fill();
setSize(getPrefWidth(), getPrefHeight());
}
//NEW CODE2 FOR ADD, NEW CLASS EXTENDS BUTTON, OTHER CODE EQUALS TEXTBUTTON.
/** @param boolean alignament: false desactiva la alineación al centro, x e y son las coordenadas donde se mostrar el texto del label.
* <p>
* xLabel, e yLabel tienen su pivote en el centro del texto, asi que las posiciones son basadas respecto a esto.
* <p>
* las variables float widthButton, y float heightButton, referidos a el boton.
* <p>
* boolean alignament: false deactivate center alignment, x and y are the coordinates where show the text label.
* <p>
* xLabel, ylabel and have its pivot in the center of the text, so the positions are based on this.
* <p>
* las variables widthButton float and float heightButton, referring to the button
*/
public LabelButton (String text, TextButtonStyle style, boolean alignament, float xLabel, float yLabel, float widthButton, float heightButton) {
super();
setStyle(style);
this.style = style;
label = new Label(text, new LabelStyle(style.font, style.fontColor));
if (alignament == true){
label.setAlignment(Align.top);
add(label).expand().fill();
}else{
this.heightButton = heightButton;
this.widthButton = widthButton;
add(label).padRight(widthButton - (xLabel * 2)).padBottom(-heightButton + (yLabel * 2));
}
setSize(getPrefWidth(), getPrefHeight());
}
public void setCoordenadasLabel(float xLabel, float yLabel){
getCell(label).padRight(widthButton - (xLabel * 2)).padBottom(-heightButton + (yLabel * 2));
}
//END NEW CODE2
public void setStyle (ButtonStyle style) {
if (style == null) {
throw new NullPointerException("style cannot be null");
}
if (!(style instanceof TextButtonStyle)) throw new IllegalArgumentException("style must be a TextButtonStyle.");
super.setStyle(style);
this.style = (TextButtonStyle)style;
if (label != null) {
TextButtonStyle textButtonStyle = (TextButtonStyle)style;
LabelStyle labelStyle = label.getStyle();
labelStyle.font = textButtonStyle.font;
labelStyle.fontColor = textButtonStyle.fontColor;
label.setStyle(labelStyle);
}
}
public TextButtonStyle getStyle () {
return style;
}
public void draw (Batch batch, float parentAlpha) {
Color fontColor;
if (isDisabled() && style.disabledFontColor != null)
fontColor = style.disabledFontColor;
else if (isPressed() && style.downFontColor != null)
fontColor = style.downFontColor;
else if (isChecked() && style.checkedFontColor != null)
fontColor = (isOver() && style.checkedOverFontColor != null) ? style.checkedOverFontColor : style.checkedFontColor;
else if (isOver() && style.overFontColor != null)
fontColor = style.overFontColor;
else
fontColor = style.fontColor;
if (fontColor != null) label.getStyle().fontColor = fontColor;
super.draw(batch, parentAlpha);
}
public Label getLabel () {
return label;
}
public Cell getLabelCell () {
return getCell(label);
}
public void setText (String text) {
label.setText(text);
}
public CharSequence getText () {
return label.getText();
}
/** The style for a text button, see {@link TextButton}.
* @author Nathan Sweet */
static public class TextButtonStyle extends ButtonStyle {
public BitmapFont font;
/** Optional. */
public Color fontColor, downFontColor, overFontColor, checkedFontColor, checkedOverFontColor, disabledFontColor;
public TextButtonStyle () {
}
public TextButtonStyle (Drawable up, Drawable down, Drawable checked, BitmapFont font) {
super(up, down, checked);
this.font = font;
}
public TextButtonStyle (TextButtonStyle style) {
super(style);
this.font = style.font;
if (style.fontColor != null) this.fontColor = new Color(style.fontColor);
if (style.downFontColor != null) this.downFontColor = new Color(style.downFontColor);
if (style.overFontColor != null) this.overFontColor = new Color(style.overFontColor);
if (style.checkedFontColor != null) this.checkedFontColor = new Color(style.checkedFontColor);
if (style.checkedOverFontColor != null) this.checkedFontColor = new Color(style.checkedOverFontColor);
if (style.disabledFontColor != null) this.disabledFontColor = new Color(style.disabledFontColor);
}
}
}
简单例子
//
在构造函数中,它仍然与以前相同,但使用布尔值false,它不与中心对齐,前两个浮点值是希望文本显示的位置,但轴是这一角的中心,前两个浮点值是希望文本显示的位置
你必须说出高度和宽度,你将有按钮来进行计算,然后不要使用设置将标签放置在单元格中,我不这样做,所以我有管理允许我的单元格,然后用相同的高度和宽度进行设置
此其他方法允许您移动标签,而不更改所创建按钮的高度和宽度
setCoordenadasLabel(float xLabel, float yLabel);
同时告诉你谁不是100%精确的,但我认为你值得这样做,然后你必须打开uiskin.json文件并添加这个
com.mygdx.game.LabelButton$TextButtonStyle: {
default: { down: default-round-down, up: default-round, font: default-font, fontColor: white },
toggle: { down: default-round-down, up: default-round, checked: default-round-down, font: default-font, fontColor: white, downFontColor: red }
},
com.mygdx.game被替换。通过该类所在的新包的名称
我希望你工作并成为你想要的,我证明它是有效的,但如果出现一些错误,我的英语很抱歉,如果我没有表达清楚,再见
另一种方式
另一种方法是只使用,不扩展类,但是对于各种按钮,如果您必须在应用过程中进行修改,或者更精确地进行修改,我认为最好使用扩展类
TEXTBUTTON.getCell(label).padRight("YOUR POSITION").padBottom("YOUR POSITION);
解决方案很简单:
final TextButton b = new TextButton("LABEL", skin);
b.getLabelCell().padBottom(xxx);
我只需要能够在按钮内移动文本。假设我的大按钮是300x200,我的文本/标签是120x40,需要仔细定位在左上角的像素精度。我不太在乎表格,有没有都可以。我非常熟悉创建表格、菜单和用户界面。我只需要在textbutton中移动文本。我添加了一个图像以帮助您理解。要将文本放置在按钮内,不想使用生成器的文本,如果可以,请使用可绘制的并将其放置在按钮内。我想使用任何可能的工具。你是说我应该做一个seperate标签,然后把它放在按钮里面?这正是我在问题的第二段代码中所做的,那么如何?Button是一个表子类。与其尝试直接重新定位内部标签,不如调用
textButton.top().right().padTop(20).padRight(20)代码>。如果这不起作用,也许9块补丁会有帮助。如果你需要矩形区域根据字符串扩展到不同的大小,可以考虑使用9块补丁作为背景。实际上,一个9-patch就可以解决这个问题。在Android用户界面中,一个9-patch可以定义图像的哪个部分超出了文本将进入的区域。不确定它在Libgdx中的行为是否相同。请看这里:可能您可以找到一些有用的东西。此方法在该页面的src代码中对您很有用:public void addButton(字符串标签、字符串二次标签、ChangeListener侦听器、布尔活动)…我设法在[x和y]中找到了正确的标签位置,如果需要,我可以发布一些代码。。。
TEXTBUTTON.getCell(label).padRight("YOUR POSITION").padBottom("YOUR POSITION);
final TextButton b = new TextButton("LABEL", skin);
b.getLabelCell().padBottom(xxx);