将值从 JFrame 传递到 JPanel
我在使用 JFrame 和 JPanel 时遇到一个恼人的问题。我有一个扩展 JFrame 的类,在构造函数中我有一个字符串。我想在构造函数中也将该值传递到 JPanel 中。我想不出该怎么做。这就是我所做的:
public class NewFileMaker extends JFrame{
private String name;
public NewFileMaker(JPanel j, String newfilename){
setTitle("New File");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(j);
this.pack();
this.name = newfilename;
}
有没有办法传递值“name”?我可以扩展 JPanel 并创建一个新类和一个新方法,但这需要对许多其他类进行大量修改。
更多信息: 我采纳了建议并扩展了 JPanel,这实际上非常轻松。
NewFileMaker 类在另一个类中被调用,如下所示
new NewFileMaker(new GeneratePanel(getFileName()));
,其中 getfileName() 获取我想要的名称。其实解决办法很简单,我必须向大家道歉。抱歉浪费您的时间!
I have an annoying problem using JFrames and JPanels. I have a class extending a JFrame and in the contructor I have a string. I want to pass this value into the JPanel also in the contructor. I cant think how to do it. This is what I did:
public class NewFileMaker extends JFrame{
private String name;
public NewFileMaker(JPanel j, String newfilename){
setTitle("New File");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(j);
this.pack();
this.name = newfilename;
}
Is there a way of passing the value "name"? I could extend JPanel and create a new class and a new method, but it would require a lot of reworking a lot of other classes.
MORE INFO:
I took the advice and extended JPanel, which was actually really painless.
The NewFileMaker class is called in another class like this
new NewFileMaker(new GeneratePanel(getFileName()));
where getfileName() gets the name I wanted. Actually the solution is so simple I have to apologize to everybody. Sorry for wasting your time!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为最好的解决方案是扩展 jpanel。
无论如何,如果你不能这样做,也许你可以向 jframe 添加一个 PropertyChangeListener。
i think that the best solution would be extending jpanel.
Anyway if you can't do that maybe you can add to jframe a PropertyChangeListener.
JPanel 没有字符串构造函数,因此您不能以这种方式传递它。
您可以尝试调用
panel.setName(name)
。JPanel doesn't have a string constructor, so you can't pass it in that way.
You can try calling
panel.setName(name)
instead.