我第一次使用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.
更多推荐
发布评论