gpt4 book ai didi

java - NetBeans Matisse - 从另一个类访问 jframe 组件

转载 作者:行者123 更新时间:2023-12-02 05:56:50 25 4
gpt4 key购买 nike

来自 Netbeans 的 Matisse 代码已被阻止。我遇到的问题是,我必须从不同包中的另一个类将 setBackground 设置为 JLabel,但我无法执行此操作,因为我无法访问 JLabel 由于其私有(private)且被阻止的代码。

有什么办法可以解决这个问题吗?

最佳答案

"The Matisse code from Netbeans is blocked"

您可以编辑它,如图 here

"because i have no access to the JLabel due to its private and blocked code"

只需为另一个类中的标签编写一个getter方法

public class OtherClass .. {
private JLabel jLabel1;

public JLabel getLabel() {
return jLabel1;
}
}

import otherpackage.OtherClass;

public class MainFrame extends JFrame {
private OtherClass otherClass;
...
private void jButtonActionPerformed(ActionEvent e) {
JLabel label = otherClass.getLabel();
label.setBackground(...)
}
}
<小时/>

"Access jframe component from another class"

听起来您正在使用多个框架。请参阅The Use of Multiple JFrames, Good/Bad Practice?

<小时/>

更新

" I have a MAIN frame made in matisse but due to some reasons i have to set the background of an textField inside matisse from another class when X validation happens in the other class"

然后您可以做的是将 Main 框架的引用传递给另一个类,并在 Main 框架中拥有一个 setter 。比如(我会提供一个访问的接口(interface))

public interface Gettable {
public void setLabelBackground(Color color);
}

public class Main extends JFrame implements Gettable {
private JLabel jLabel1;
private OtherPanel otherPanel;

public void initComponents() {
otherPanel = new OtherPanel(Main.this); // see link above to edit this area
}

@Override
public void setLabelBackground(Color color) {
jLabel1.setBackground(color);
}
}

public class OtherPanel extends JPanel {
private Gettable gettable;

public OtherPanel(Gettable gettable) {
this.gettable = gettable;
}

private void jButtonActionPerformed(ActionEvent e) {
gettable.setLabelBackground(Color.RED);
}
}

关于java - NetBeans Matisse - 从另一个类访问 jframe 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23006570/

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