[英]How do I create user-defined JLabel,JTextField?
Based on the title, how do I create a dynamic size of JLabel and JTextfield determined by user ? 根据标题,如何创建由用户确定的JLabel和JTextfield的动态大小?
I have a Main.java
which takes user input and I'm creating a JFrame on another file ( Design3.java
) but the problem I encounter is how do I pass the value from the Main.java
to Design3.java
我有一个
Main.java
接受用户输入,我在另一个文件( Design3.java
)上创建一个JFrame,但我遇到的问题是如何将值从Main.java
传递给Design3.java
Here is my code for the Design3.java
file. 这是我的
Design3.java
文件的代码。 Basically I'm stucked because the value I passed from Main.java
the scope of the variable last for the Design3 method. 基本上我被困了,因为我从
Main.java
传递的值为Design3方法的变量范围。 How do I make it global because I need to create JLabel
globally. 如何使其全局化,因为我需要全局创建
JLabel
。
int numberOfSellers;
Label[] l = new Label[numberOfSellers];
public class Design3 {
int numberOfSellers;
Design3 nOS = new Design3();
public Design3( int number_of_sellers )
{
nOS.setNumberOfSellers(number_of_sellers);
JFrame newWindow = new JFrame("Test");
newWindow.setVisible(true);
newWindow.setSize(500,500);
newWindow.setLayout(null);
}
public void setNumberOfSellers(int num)
{
this.numberOfSellers = num;
}
} }
Based on the title, how do I create a dynamic size of JLabel and JTextfield determined by user ?
根据标题,如何创建由用户确定的JLabel和JTextfield的动态大小?
What are you trying to do ? 你想做什么 ? You can look into creating a custom class that extends
JLabel
and another custom class which extends JTextField
. 您可以考虑创建一个扩展
JLabel
的自定义类和另一个扩展JTextField
自定义类。
Second, why does Design3
have an instance of Design3
? 其次,为什么
Design3
有一个Design3
实例? I am sensing a lot of bad design here. 我在这里感觉到很多不好的设计。
Third, look into the Swing MVC
architecture. 第三,研究
Swing MVC
架构。
Your question in a nutshell is: How can I have an object of one class change the state of an object in another class. 简而言之,您的问题是:如何让一个类的对象更改另一个类中对象的状态。 Usually that translates into having one object change the state of the field(s) of another object.
通常转换为让一个对象改变另一个对象的字段的状态。
Comments: 评论:
numberOfFoo
and then give it a constructor parameter to match, and then when one class creates an instance of the other, it can pass the proper number into the first class's constructor. numberOfFoo
,然后给它一个构造函数参数来匹配,然后当一个类创建另一个的实例时,它可以将正确的数字传递给第一个类的构造函数。 setFoo(int foo)
where one class can pass the number into the other class. setFoo(int foo)
类的setter方法,其中一个类可以将数字传递给另一个类。 If you have a more specific question, then you'll have to delineate it further. 如果您有更具体的问题,那么您必须进一步描述它。 For instance, if you want one object to monitor another object's field and respond to changes, then again as per my comment, you'll want to use some type of observer design pattern mechanism.
例如,如果您希望一个对象监视另一个对象的字段并响应更改,那么根据我的注释,您将需要使用某种类型的观察者设计模式机制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.