java - 在单独的类 java 中操作组件

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

我的菜单栏

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。我设置的 public void clearTextBoxesSortsGui 中调用时可以实现此目的。但是,当单击 btnClear 调用该方法时,它不会检测到 JPanel sortPanel 上的任何 JTextFields。有没有办法纠正这个问题?如果是这样,实现这一目标的最佳方法是什么?

最佳答案

你的问题在于这里的代码:

    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
        }
    }

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

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

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

  sG = new SortsGui();

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

MyMenuBar mbr = new MyMenuBar(this);

代替

MyMenuBar mbr = new MyMenuBar();

我认为这会解决您的问题。

关于java - 在单独的类 java 中操作组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30895768/

相关文章:

java - 如何使用 MigLayout 在面板上绘制分隔符

java - Gridbag 布局未正确对齐

java - 无法执行 jar 文件 : "no main manifest attribute"

java - 如何在 Composite JButton 类的 ChangeListener 中访问 toString() 方法?

java - 如何在 Java 8 中直接使用函数作为函数类型

Java Swing简单中心的JPanel在其他JPanel中

java - 为什么我绘制的图像在 JPanel 上闪烁?

gradle - 如何在gradle中进行miglayout摇摆

java - 重写 setPreferredSize() 和 getPreferredSize()

java - 具有无状态服务的分布式企业应用程序框架?