简体   繁体   English

如何创建用户定义的JLabel,JTextField?

[英]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: 评论:

  • There is no one-size fits all answer for this as it is a very broad and overly general question. 没有一个适合所有人的答案,因为这是一个非常广泛和过于普遍的问题。 That is why we are asking for clarification of your problem since the more specific your question, usually the more clear the answer. 这就是为什么我们要求澄清您的问题,因为您的问题越具体,通常答案就越明确。
  • General ways to change the state of an object's field including via a constructor parameter. 更改对象字段状态的一般方法,包括通过构造函数参数。 So you can give your GUI an int field say 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. 所以你可以给你的GUI一个int字段说numberOfFoo ,然后给它一个构造函数参数来匹配,然后当一个类创建另一个的实例时,它可以将正确的数字传递给第一个类的构造函数。
  • Or you could use a setter method such as 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM