Netbeans的Matisse代码被阻止。我遇到的问题是,我必须在不同的包中从另一个类setBackground到一个JLabel,但是我不能这样做,因为我没有访问JLabel的权限,因为它的私有代码和阻塞代码。
这有什么解决办法吗?
发布于 2014-04-11 08:00:59
“Netbeans的Matisse代码被阻塞”
您可以像看到的here那样编辑它
“因为我没有访问JLabel的权限,因为它的私有代码和阻塞代码”
只需为另一个类中的标签编写一个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(...)
}
}“从另一个类访问jframe组件”
听起来你用的是多帧。请参阅The Use of Multiple JFrames, Good/Bad Practice?
更新
“我有一个用马蒂斯制作的主框架,但由于某些原因,当另一个类发生X验证时,我必须从另一个类中设置马蒂斯内部textField的背景。”
然后,您可以将Main框架的引用传递给另一个类,并在Main框架中有一个setter。类似于(我将提供一个访问接口)
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);
}
}发布于 2014-04-11 07:53:28
https://stackoverflow.com/questions/23006570
复制相似问题