调用从另一个JPanel中执行的action获取的实例变量

时间:2016-02-21 10:22:56

标签: java swing

我用cardLayout创建了一个JFrame,第一个可见的JPanel有一个Jbutton,我已经添加了一个动作侦听器来执行一个动作。该操作创建了一个String变量' hhhhh'我想在另一个JPanel中使用。这就是我遇到的问题。

Class 1

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

    public class NHome extends JFrame{


    JPanel Bucket= new JPanel(), Start= new JPanel(), Cashier = new csView(), Manager = new JPanel();
    JButton stbtn= new JButton("Start"), mnbtn= new JButton("Manager"), csbtn= new JButton("Cashier");
    CardLayout cl= new CardLayout();
    private final JTextField textField = new JTextField();
    private JPasswordField passwordField;



    public NHome() {
    textField.setBounds(322, 141, 158, 31);
    textField.setColumns(10);
    Bucket.setLayout(cl);
    Bucket.add(Start, "1");
    Bucket.add(Cashier, "2");
    Bucket.add(Manager, "3");

    Start.setLayout(null);
    stbtn.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
    cl.show(Bucket, "2");

    /*
     * I want to use this value of this String in the another class (csView)
    */
    String hhhhh=new String("Peter");
    System.out.println(hhhhh);
        }
    });





    stbtn.setBounds(353, 245, 76, 23);
    Start.add(stbtn);

    Start.add(textField);

    passwordField = new JPasswordField();
    passwordField.setBounds(322, 183, 158, 31);
    Start.add(passwordField);
    Cashier.setLayout(null);
    csbtn.setBounds(197, 139, 116, 23);

    Cashier.add(csbtn);
    Manager.setBackground(Color.BLUE);
    Manager.add(mnbtn);

    cl.show(Bucket, "1");

    setTitle("NOVA PHARM");

    getContentPane().add(Bucket);
    setBounds(300, 300, 566, 482);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().setLayout(new CardLayout(5, 5));
    setResizable(true);


    }
    /**
    * Launch the application.
    */
    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {
    try {
    NHome window = new NHome();
    window.setVisible(true);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    });
    }
    }

    /* 
    *this is the second class where I want to use the variable
    */ 

第2类

    import javax.swing.JPanel;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import java.awt.Font;

    public class csView extends JPanel {

    /**
    * Create the panel.
    */
    public csView() {
    setLayout(null);
    /**
    * I want to display the String hhhhh in the JLabel Uniqlbl below
    *   
    */
    JLabel Uniqlbl = new JLabel("Cashier Name:");
    Uniqlbl.setFont(new Font("Tahoma", Font.PLAIN, 16));
    Uniqlbl.setBounds(227, 62, 253, 46);
    add(Uniqlbl);

    }
    }

2 个答案:

答案 0 :(得分:0)

您可以这样做的一种方法是在NHome类中创建一个静态变量,作为全局变量。这意味着csView将能够读取和写入变量。

这是如何实施的一个例子:

angular.module('app')
.directive('instalmentsBox'['InstalmentService', function(InstalmentService) {
    return {
     templateUrl:'scripts/directives/contracts/instalments-box.html?v='+window.app_version,
        restrict: 'E',
        scope: {
            selectedcontractguid: '=selectedcontractguid'
        },
        controller: function($scope,$watch) {
          $scope.$watch('selectedcontractguid', function (watch) {
            console.log(watch);
          })
        }
    }
}]);

那么你可以通过说:

在csView中实现它
  public static Comparable[] findCommonElements(Comparable[][] collections) {
    if((collections[0]==null) || (collections[1]==null)){
    return new Comparable[0];
    }
    Comparable[] arr1 = collections[0];
    Comparable[] arr2 = collections[1];
    Comparable[] hashArray;
    Comparable[] searchArray;
    if(arr1.length < arr2.length) {
    [...]

答案 1 :(得分:0)

尝试修改csView

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Font;

public class csView extends JPanel {
    // Declare those variables here
    String hhhhh;
    JLabel Uniqlbl;

    public csView() {
        setLayout(null);

        Uniqlbl = new JLabel("Cashier Name:");
        Uniqlbl.setFont(new Font("Tahoma", Font.PLAIN, 16));
        Uniqlbl.setBounds(227, 62, 253, 46);
        add(Uniqlbl);

    }

    // Add a setter here
    public void setHhhhh(String hhhhh) {
        this.hhhhh = hhhhh;
        // Edit: Add this line to update the Uniqlbl text
        Uniqlbl.setText(hhhhh);
    }
}

在ActionListener中调用setHhhhh()方法

stbtn.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent arg0) {
        cl.show(Bucket, "2");

        String hhhhh=new String("Peter");

        // Call the setter here
        Cashier.setHhhhh(hhhhh);

        System.out.println(hhhhh);
    }
});

我建议你不要在csView构造函数中使用“hhhhh”变量,否则你可能会遇到NullPointerException。或者,如果您想这样做,请首先像这样初始化“hhhhh”变量

String hhhhh = "";