【问题标题】:Why use public static textfields in swing为什么在 swing 中使用公共静态文本字段
【发布时间】:2023-03-22 04:00:01
【问题描述】:

我有一个问题,我目前正在学习java swing/awt

我正在创建一个小程序,有 2 个 jtextfields 和 1 个 jbutton

我使用了 2 个类,1 个用于 GUI,其他用于 LOGIC/IMPLEMENTATIONS

我在 class1 中声明了一个按钮并对其应用了 actionlistener,在 class2(extends class1) 中我创建了一个进行此比较的方法

MaisamCustom 是我的 class1,LogicClass 是 class2

此屏幕截图来自 MaisamCustom(Class1),即调用此方法的按钮

现在真正的问题是,当我输入 2 个不同/相同的值并按下按钮时,它会在 IF 语句中显示消息,否则无法正常工作

两场比赛!!!

所以我用谷歌搜索了它,经过几个小时的搜索,我在 stackoverflow 上找到了答案

这个叫@Azuu的人轻松回答了一个类似的问题

--> Get value from JPanel textfield in another class

所以我对我的 JTEXTFIELDS 对象声明做了同样的事情并且它工作了! :')

我很高兴,真的要感谢这个家伙 (@Azuu)。

现在的问题

我知道什么是静态的,什么是公开的

但是我的程序是如何开始完美运行 jtextfields public static

谁能给我解释一下:)

是的,这是代码 (它真的搞砸了,因为我正在试验 GUI 的不同方面)

1 类

package gui.examples;


import java.awt.event.*;
import javax.swing.*;

public class MaisamCustom {

    JFrame frame = new JFrame("My Desktop App");
    JPanel panel = new JPanel();
    public static JTextField txt1 = new JTextField(8),
            txt2 = new JTextField(8);
    JButton enter_btn = new JButton("Enter");
    public void launchFrame() {

        JLabel label1 = new JLabel("   "),
                label3 = new JLabel("   "),
                label4 = new JLabel("   "),
                label5 = new JLabel("   "),
                label2 = new JLabel("                  My Comparision Program");
        panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
        panel.add(label3);
        panel.add(label2);
        panel.add(label4);
        panel.add(txt1);
        panel.add(label1);
        panel.add(txt2);
        panel.add(label5);
        enter_btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                LogicClass obj = new LogicClass();
                obj.enterButton();

            }
        });
        panel.add(enter_btn);
        frame.setResizable(false);
        frame.getContentPane().add(panel);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }


    public static void main(String[] args) {
        MaisamCustom obj = new MaisamCustom();
        try {
            obj.launchFrame();
        } catch (Exception ex) {
            System.out.println("Some issue while launching the application...");
        }
    }


}

类2

package gui.examples;

import javax.swing.JOptionPane;

public class LogicClass extends MaisamCustom {

    public void info(String message, String title) {
        JOptionPane.showMessageDialog(null, message, "PromptBox: " + title, JOptionPane.INFORMATION_MESSAGE);
    }

    public void enterButton() {
        LogicClass obj = new LogicClass();
        if (txt1.getText().equals(txt2.getText())) {
            obj.info("Both Fields Match !!!", "Note !!");
        } else {
            obj.info("Both Fields Do Not Match !!!", "Note !!");
        }
    }

}

我真的很抱歉,我的问题很简单,但我必须解释一下 详细介绍,这就是让它这么长的原因,我真的很喜欢 一步一步详细解释我的问题

几个月前我以同样的方式发布了一个问题(有时用户会很粗鲁,他们会非常严厉地责骂你,所以所有的解释都是为了防止这种情况发生)

【问题讨论】:

  • 首先,任何回答的人都知道如何最好地解释事情:您是否已经知道static 的含义,您是否知道实例是什么(或类和对象之间的区别)?跨度>
  • 我的知识实例:例如。实例变量,范围遍及整个类....静态:它与内存有关,静态变量仅在内存中占1个位置,静态代码块在程序执行时首先运行,我知道类和对象之间的区别
  • @immibis 类的实例是我们创建的这些对象 myClass obj = new myClass();

标签: java swing user-interface static awt


【解决方案1】:

关于公众

protected 也可以。它只需要在您的 LogicClass 中可见。

关于静态:

每次使用回车按钮时,都会创建一个新的 LogicClass 对象。如果txt1txt2 不是静态的,您将创建没有文本的新JTextFields。两个字段总是匹配的。它们与对话框中的字段不同。

现在您已将字段设为静态,您可以继续使用最初在 MaisamCustom 对象中创建的原始字段;对话框中的实际字段。

使用public static,您可以稍微简化程序:LogicClass 不需要从 MaisamCustom 扩展:

package gui.examples;

import javax.swing.JOptionPane;

public class LogicClass {

    public void info(String message, String title) {
        JOptionPane.showMessageDialog(null, message, "PromptBox: " + title, JOptionPane.INFORMATION_MESSAGE);
    }

    public void enterButton() {
        LogicClass obj = new LogicClass();
        if (MaisamCustom.txt1.getText().equals(MaisamCustom.txt2.getText())) {
            obj.info("Both Fields Match !!!", "Note !!");
        } else {
            obj.info("Both Fields Do Not Match !!!", "Note !!");
        }
    }

}

另一种方法是显式使用扩展类:

package gui.examples;


import java.awt.event.*;
import javax.swing.*;

public class MaisamCustom {

    JFrame frame = new JFrame("My Desktop App");
    JPanel panel = new JPanel();
    protected JTextField txt1 = new JTextField(8),
            txt2 = new JTextField(8);
    JButton enter_btn = new JButton("Enter");
    public void launchFrame() {

        JLabel label1 = new JLabel("   "),
                label3 = new JLabel("   "),
                label4 = new JLabel("   "),
                label5 = new JLabel("   "),
                label2 = new JLabel("                  My Comparision Program");
        panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
        panel.add(label3);
        panel.add(label2);
        panel.add(label4);
        panel.add(txt1);
        panel.add(label1);
        panel.add(txt2);
        panel.add(label5);
        enter_btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (MaisamCustom.this instanceof LogicClass){
                    LogicClass logicClassObj = (LogicClass)MaisamCustom.this;
                    logicClassObj.enterButton();
                }
            }
        });
        panel.add(enter_btn);
        frame.setResizable(false);
        frame.getContentPane().add(panel);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }


    public static void main(String[] args) {
        MaisamCustom obj = new LogicClass();
        try {
            obj.launchFrame();
        } catch (Exception ex) {
            System.out.println("Some issue while launching the application...");
        }
    }


}

【讨论】:

  • thaaaaaaaaaaaaanks :) :) :) :) :)
  • 我同意,这可能不是您要找的内容。我添加了不同的编辑,希望它对您的理解有所帮助。
猜你喜欢
  • 1970-01-01
  • 2015-02-01
  • 1970-01-01
  • 2012-01-06
  • 2012-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多