2017-05-30 39 views
0

请我需要你的帮助,即时尝试做到这一点,我想现在3-4天,我不能。 我有2个类MainForm和Class2。 我在Class1中的方法内有一个JLablel,我想通过按下Class2中的按钮来修改它。修改一个无效方法内的JLabel

public class MainForm { 


    private JFrame frame; 

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

    /** 
    * Create the application. 
    */ 
    public MainForm() { 
     initialize(); 
    } 

    /** 
    * Initialize the contents of the frame. 
    */ 
    private void initialize() { 
     frame = new JFrame(); 
     frame.setBounds(400, 200, 488, 322); 
     frame.setTitle("ShutDown"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().setLayout(null); 
     frame.setResizable(false); 


/** 
     * Time BOX 
     */ 
     JComboBox<String> timeBox = new JComboBox<String>(); 
     timeBox.setBounds(73, 142, 90, 20); 
     timeBox.addItem("Days"); 
     timeBox.addItem("Hours"); 
     timeBox.addItem("Minutes"); 
     timeBox.addItem("Seconds"); 
     timeBox.addItem("Right now"); 
     timeBox.setSelectedItem("Minutes"); 
     frame.getContentPane().add(timeBox); 


     String getTimeBox = timeBox.getSelectedItem().toString(); 



     /** 
     * The label info. 
     */ 
     JLabel labelInfo = new JLabel(""); 
     labelInfo.setBounds(73, 209, 310, 14); 
     frame.getContentPane().add(labelInfo); 
     labelInfo.setText(getTimeBox); 
} 

和2类

Class2 

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

        } 

我想多和永诺不工作,什么我需要在该按钮写代码即可获得从时间盒(组合框)的selectedItem属性 ,并把它到那个标签?

回答

0

首先你需要在课堂上都 JComboBox & JLable对象的引用2.
简单的方法来做到这一点是在MainForm代替initialize方法的本地成员各自的私有成员声明&它们传递到构造或者使用setter方法。

考虑参考名称分别是jComboBox & jLable

现在你可以用以下语法使用此语法Class2.this.jComboBox & Class2.this.jLable(JComboBox中& jLable是对象的引用)从匿名类的actionPerformed方法中引用它们。