javax.swing中;(Javax.Swing; How can I create an object of a different class and assign variables rece

编程入门 行业动态 更新时间:2024-10-27 22:30:52
javax.Swing中;(Javax.Swing; How can I create an object of a different class and assign variables received from the jframe ? Conceptual issue)

我第一次使用javax.swing和jframes,所以如果你发现这个问题原语,请原谅。

问题:在我的main函数中,我创建了一个类的对象,让我们说ClassTest。 所以代码如下:

import TestPackage.ClassTest.*; public class Qinterface extends JFrame and implements ActionListener { public string Login; public static void main(String[] args){ ClassTest test = new ClassTest(); try{ eventqueue invoker ...}catch{} } Qinterface(){ setResizable(false); setTitle("Carrefour : Qualys Application"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(300, 100, 850, 500); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); txtEnterText = new JTextField(); txtEnterText.setText("Enter Qualys Login"); txtEnterText.setBounds(10, 193, 166, 23); contentPane.add(txtEnterText); txtEnterText.setColumns(10); btnSubmit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent eSubmit) { //button is pressed System.out.println("You clicked the button Submit"); Login = txtEnterText.getText();}}); } }

因此,如在代码的最后一行中所见,我能够从txtEnterText字段获取值并分配给本地变量“Login”。 但是,如果我想将此值分配给在main函数中创建的类的实例,我该怎么办呢?

test.x=txtEnterText.getText();

我知道这种方法不可能,因为我们在构造函数中使用Qinterface类,并且在main中实例化的ClassTest的变量是不可见的。

所以问题是一般的和概念性的; 在用javax.swing编码时,你如何处理这类问题来解决它们?

I am working for the first time with javax.swing and jframes, so please excuse me if you find this question primitive.

Problem: In my main function I have created an object of a class lets say ClassTest. So the code goes like:

import TestPackage.ClassTest.*; public class Qinterface extends JFrame and implements ActionListener { public string Login; public static void main(String[] args){ ClassTest test = new ClassTest(); try{ eventqueue invoker ...}catch{} } Qinterface(){ setResizable(false); setTitle("Carrefour : Qualys Application"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(300, 100, 850, 500); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); txtEnterText = new JTextField(); txtEnterText.setText("Enter Qualys Login"); txtEnterText.setBounds(10, 193, 166, 23); contentPane.add(txtEnterText); txtEnterText.setColumns(10); btnSubmit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent eSubmit) { //button is pressed System.out.println("You clicked the button Submit"); Login = txtEnterText.getText();}}); } }

So as seen in the last line of the code I am able to get the value from the txtEnterText field and assign to a local variable "Login". But how do i go about if I want to assign this value to an instance of a class created in the main function, for example;

test.x=txtEnterText.getText();

I know its not possible in this approach as we are in the constructor the Qinterface class and the variable of the ClassTest instantiated in the main are not visible.

So the question is general and conceptual; how do you go about such kinds of problems to resolve them, when coding with javax.swing ?

最满意答案

以登录过程为例:

您的接口类可以包含由操作侦听器填充的“LoginData”对象。 通过为数据对象提供getter,可以从界面外部访问登录数据。

这只是解决此问题的众多方法之一。

这将是MVC架构的一个很好的候选者 - 你可以在这里阅读一下。

Using a login process as an example:

Your interface class could hold a "LoginData" object that is populated by the action listener. By providing a getter for the data object, the login data can then be accessed from outside of the interface.

This is just one of the many ways you could tackle this problem.

This would be a good candidate for MVC architecture - you can read about it a little here.

更多推荐

本文发布于:2023-08-07 14:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464755.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Swing   create   object   javax   swing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!