gpt4 book ai didi

Java - 使用存储在数组中的文本更改 JLabel

转载 作者:行者123 更新时间:2023-11-29 03:21:54 24 4
gpt4 key购买 nike

Hello World,我是 java 的新手,我正在尝试创建一种方法,在操作发生时将 JLabel 的文本更改为存储在数组中的某些文本,如下所示:

     JLabel one = new JLabel("Hello World");
JButton b1 = new JButton("Next");
JButton b2 = new JButton("Prev");

String[] main = ["LoveLace","Dynamics","Creed","Main"];
b1.addActionListsner(new ActionListener() {
actionPerformed(ActionEvent e) {

//In here it will load the first indexed string which is "Lovelace"
//This is where i need help
//Think of it like a jQuery slider
//When the next button is clicked, it loads a new word to the element
//When the prev button is clicked it load the last indexed string of the array

请这实际上是一个手动字 slider ,它会在单击下一个按钮时更改文本,并在单击上一个按钮时加载数组的最后一个索引字符串,拜托,我不知道如何执行此操作,请帮忙

最佳答案

我认为这应该可行...

private void yourMethodName(final String newLabelText) {
one.setText(newLabelText);
}

如果你想用这个方法通用...

private void yourMethodName(final JLabel label, final String newLabelText) {
label.setText(newLabelText);
}

现在你只需要用你想改变的参数调用这个方法。如果更改没有消失,您需要重新绘制您的东西所在的容器。如果您不使用容器,我建议您创建一个并将所有东西添加到这个容器中。 this thread的答案可能会有帮助。

关于Java - 使用存储在数组中的文本更改 JLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23106936/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com