在单独的类java中操作组件

时间:2015-06-17 15:19:48

标签: java swing miglayout

我有三个java类

StitchSorts

import Sorts.*;

public class StitchSorts
{   
    public static void main(String[] args)
    {
        StitchSorts sS = new StitchSorts();
        SortsGui.main(args);
    }
}

SortsGui

package Sorts;

import javax.swing.*;
import java.awt.*;
import net.miginfocom.swing.MigLayout;
import Sorts.*;
import javax.swing.event.*;
import java.awt.event.*;

public class SortsGui
{
    JFrame myMainWindow = new JFrame("Sorts");

    JPanel sortPanel = new JPanel();

    MyMenuBar mbr = new MyMenuBar();

    JTextField txtField = new JTextField();
    JTextField txtField2 = new JTextField();

    public void runGUI()
    {
        myMainWindow.setBounds(10, 10, 800, 800);
        myMainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        myMainWindow.setLayout(new GridLayout(1,1));

        createSortTestPanel();

        myMainWindow.getContentPane().add(sortPanel);

        myMainWindow.setJMenuBar(mbr);

        myMainWindow.setVisible(true);
    }

    public void createSortTestPanel()
    {
        MigLayout layout = new MigLayout("" , "[grow]");
        sortPanel.setLayout(layout);

        sortPanel.add(txtField,"growx");
        sortPanel.add(txtField2,"growx");
    }

    public void clearTextBoxes()
    {
        for (Component C : sortPanel.getComponents())
        {    
            if (C instanceof JTextField)
            {
                ((JTextField) C).setText("");
                System.out.println("Multiple");
            }
        }
    }

    public static void main(String[] args)
    {
        SortsGui sG = new SortsGui();
        sG.runGUI();
    }
}

MyMenuBar

package Sorts;

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

import java.io.*;
import java.lang.*;

public class MyMenuBar extends JMenuBar
{
    JButton btnClear = new JButton("Clear");
    SortsGui sG;

    public MyMenuBar()
    {
        setBorderPainted(true);
        makePopUpMenu();
    }

    void makePopUpMenu()
    {
        add(Box.createHorizontalGlue());
        clearButton(btnClear);
        add(btnClear);
    }

    public void clearButton(JButton J)
    {
        J.setOpaque(false); //These remove the button filling and border
        J.setContentAreaFilled(false);
        J.setBorder(null);
        J.setMinimumSize(new Dimension(50,25));
        J.setPreferredSize(new Dimension(50,25));
        J.setMaximumSize(new Dimension(50,25));
        J.addActionListener(new buttonPress());
        J.setFocusable(false);
    }

    class buttonPress implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {               
            if(e.getSource() == btnClear)
            {
                System.out.println("Clearing");
                sG = new SortsGui();
                sG.clearTextBoxes();
            }
        }
    }
}

我在这里想要实现的是,当点击JButton btnClear时,应该清除SortsGui中的JTextFields。我在clearTextBoxes中调用它时设置的公共空SortsGui是有效的。但是,当单击btnClear调用该方法时,它不会检测JPanel sortPanel上的任何JTextField。有没有办法纠正这个?如果是这样,最好的方法是什么?

1 个答案:

答案 0 :(得分:3)

您的问题在于此处的代码:

    public void actionPerformed(ActionEvent e)
    {               
        if(e.getSource() == btnClear)
        {
            System.out.println("Clearing");
            sG = new SortsGui(); // You are creating a new instance of SortsGui
            sG.clearTextBoxes(); //You are calling the method on this new instance
        }
    }

可以通过在SortsGui的构造函数中传递MyMenuBar的实例并将其存储在实例变量sG中来完成问题的解决方案,如您所做的那样你的代码如下:

public MyMenuBar(SortsGui sG)
{
    this.sG = sG;
    setBorderPainted(true);
    makePopUpMenu();
}

现在在执行动作的方法中删除行:

  sG = new SortsGui();

现在,在mbr类中变量SortsGui的初始化中,使用以下代码:

MyMenuBar mbr = new MyMenuBar(this);

而不是

MyMenuBar mbr = new MyMenuBar();

我认为这可以解决您的问题。