我有三个Java类

针脚排序

import Sorts.*;

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




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();
    }
}


我的菜单栏

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中的JTextField。我为实现此目的而设置的public void clearTextBoxesSortsGui中被调用时起作用。但是,当通过单击btnClear调用该方法时,它不会在JPanel sortPanel上检测到任何JTextField。有没有办法纠正这个问题?如果是这样,最好的方法是什么?

最佳答案

您的问题出在这里的代码中:

    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();


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

08-03 23:02