如何使int变量对GUI JButton中的其他类可见

时间:2015-01-03 22:42:35

标签: java class variables declaration

我正在尝试从另一个类访问此GUI表单类中的int myInt。该方法确实将输入变量正确传递给myInt,但我无法使其对其他java文件可见。我一直在阅读关于范围和类声明的内容,并且到目前为止已经能够混淆,但我在这里做错了。我没有尝试过任何工作。

package com.jdividend.platform.allocation;

import com.ib.client.*;
import com.jdividend.platform.model.*;
import com.jdividend.platform.trader.*;
import com.jdividend.platform.util.*;

import javax.swing.*;

import java.awt.*;
import java.util.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * Dialog to show the account size allowed info.
 */
public class AllocationDialog extends JDialog {

    /* inner class to define the "about" model */
    public static class AllocationTableModel extends TableDataModel {

        private AllocationTableModel() {
            String[] aboutSchema = {"Property", "Value"};
            setSchema(aboutSchema);
        }

    }

    public AllocationDialog(JFrame parent) {
        super(parent);
        init();
        pack();
        setLocationRelativeTo(parent);
        setVisible(true);
    }

    public void init() {
        setModal(true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setTitle("Allocation - JDividend");

        JPanel contentPanel = new JPanel();
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        contentPanel.setLayout(new BorderLayout(0, 0));
        {
            JPanel panel = new JPanel();
            contentPanel.add(panel, BorderLayout.CENTER);
            panel.setLayout(null);

            JPanel panel_1 = new JPanel();
            panel_1.setBounds(84, 146, 274, 19);
            panel.add(panel_1);

            JLabel lblNewLabel = new JLabel("Total account cash and margin that");
            panel_1.add(lblNewLabel);

            JPanel panel_2 = new JPanel();
            panel_2.setBounds(84, 163, 274, 19);
            panel.add(panel_2);

            JLabel lblJdividendIsAllowed = new JLabel("JDividend is allowed to trade with.");
            panel_2.add(lblJdividendIsAllowed);

            JFormattedTextField formattedTextField = new JFormattedTextField();
            formattedTextField.setBounds(172, 189, 100, 20);
            panel.add(formattedTextField);

            JButton btnOk = new JButton("OK");
            btnOk.setBounds(121, 221, 89, 23);
            panel.add(btnOk);
            btnOk.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    try {
                        int myInt = Integer.parseInt(formattedTextField.getText());
                        System.out.println(myInt);
                        //do some stuff
                    }
                    catch (NumberFormatException ex) {
                        System.out.println("Not a number");
                        //do you want
                    }

                    JButton btnCancel = new JButton("Cancel");
                    btnCancel.setBounds(238, 220, 89, 23);
                    panel.add(btnCancel);

                    formattedTextField.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent arg0) {
                            JButton button = new JButton("OK");
                        }
                    });

                }
            });
        }

        String serverVersion = "Disconnected from server";
        Trader trader = Dispatcher.getInstance().getTrader();
        if (trader != null) {
            int version = trader.getAssistant().getServerVersion();
            if (version != 0) {
                serverVersion = String.valueOf(version);
            }
        }

        TableDataModel aboutModel = new AllocationTableModel();

        getContentPane().setPreferredSize(new Dimension(450, 400));

        Properties properties = System.getProperties();
        Enumeration<?> propNames = properties.propertyNames();

        while (propNames.hasMoreElements()) {
            String key = (String) propNames.nextElement();
            String value = properties.getProperty(key);
            String[] row = {key, value};
            aboutModel.addRow(row);
        }
    }   
}

1 个答案:

答案 0 :(得分:0)

myInt应该在类中声明,但在任何方法实现之外:

public class AllocationDialog extends JDialog {

    public int myInt;

    // ...
}