如何使用主类从项目中的其他类实现 Swing 组件?

huangapple go评论64阅读模式
英文:

How do I use a Main class to implement Swing Components from other classes in my project?

问题

我正在开发一个项目,其中我有 "Class1"、"Class2" 和 "Class3"。Class2 和 Class3 都创建了包含各种 JButton、JLabel 和其他 Swing 组件的 JFrame。如何在 Class1 中引用来自 Class2 的 JButton,并使用一个动作监听器将 Class2 的可见性设置为 false,Class3 的可见性设置为 true?

我尝试过以下方法:在主方法中将 Class2 设置为可见没有问题,但一旦我开始实现 Class3,事情就不起作用了。

总结:在另一个类中初始化 JButton 并使用引用该 JButton 的动作监听器存在问题。

英文:

Im developing a project in which I have "Class1", "Class2", and "Class3".
Class2 and Class3 both create JFrame's, each containing various JButton's, JLabel's, and other swing components. How do i make so in Class1 I can reffrence the JButton from Class2 and use an action listener to set the Class2's visibilty to false and Class3's visibilty to true.

I Tried This: Setting Class2 to visbile in my main method was no issue, but once i started to implement Class3 things didint work.

Summary: Having Issues initiating a jbutton from an other class and using an action listener that refrences that jbutton.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

@SuppressWarnings("serial")
public class Class1 extends JFrame implements ActionListener
{

    public static void main(String[] args) {
        Class2 frameFromclass2 = new Class2();

        frameFromclass2.setVisible(true);
    }       
    Class2 buttonMovetoclass3 = new Class2();

    public void actionPerformed(ActionEvent e) {

        if (buttonMovetoclass3 == e.getSource()) {   
            Class2 frameFromclass2 = new Class2();

            frameFromclass2.setVisible(false);

            Class3 frameFromclass3 = new Class3();

            frameFromclass3.setVisible(true);

                    	
        }
    }	
	
}

答案1

得分: 0

以下是您要翻译的部分:

你问题的一部分是由于对变量范围的理解不足引起的。问题的另一部分在于方法错误的处理方式。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

@SuppressWarnings("serial")
public class Class1 extends JFrame implements ActionListener
{
//这是一个方法。在此方法内声明的每个变量只能在此方法内访问。
public static void main(String[] args) {
//这也包括在此方法内。
Class2 frameFromclass2 = new Class2();
//你在这里使用了它,但它不仅被遗弃,而且无法在其他地方使用。它被包含在花括号的范围内。
frameFromclass2.setVisible(true);
}
Class2 buttonMovetoclass3 = new Class2();

public void actionPerformed(ActionEvent e) {

    if (buttonMovetoclass3 == e.getSource()) {   
        //在这里,你创建了一个新的Class2对象,并给它相同的名称,然后隐藏它。
        Class2 frameFromclass2 = new Class2();

        frameFromclass2.setVisible(false);
        //然后你在这里做同样的事情,但是用Class3;然后你显示它。
        Class3 frameFromclass3 = new Class3();

        frameFromclass3.setVisible(true);
    }
}   

}

我们无法看到您的其他类在构造函数中有什么内容,所以我无法确定是否也存在问题。然而,我可以提供更好的替代方案。

通常情况下,即使有弹出窗口或多个“页面”可以使用,也应保留1个JFrame。考虑使用多个JPanels,每个用于不同的布局。或者您可以使用JTabbedPane,每个选项卡上有不同的信息。对于登录目的,一个常见的选项是JOptionPaneJDialog。如果您坚持使用多个JFrames并且要在它们之间移动组件,应该使用类中的变量来存储信息,然后使用公共方法从其他类中访问这些变量。但是,一旦一个窗口不再使用,应该销毁它而不是隐藏它。这是一个很好的资源,介绍了各种方法来实现这一点。

英文:

Part of your problem is caused by a lack of understanding in terms of variable scope. The other part of your problem is just in approaching this the wrong way.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

@SuppressWarnings("serial")
public class Class1 extends JFrame implements ActionListener
{
    //This is a method. Every variable declared within this method, 
    //is only accessible from within this method. 
    public static void main(String[] args) {
        //That includes this one. 
        Class2 frameFromclass2 = new Class2();
        //You've used it here, but not only is it then abandoned, but it
        //cannot be used anywhere else. It is contained in a cage of curly braces. 
        frameFromclass2.setVisible(true);
    }       
    Class2 buttonMovetoclass3 = new Class2();

    public void actionPerformed(ActionEvent e) {

        if (buttonMovetoclass3 == e.getSource()) {   
            //Here you create a NEW Class2 object and give it the same
            //name you gave the other, then you hide it.
            Class2 frameFromclass2 = new Class2();

            frameFromclass2.setVisible(false);
            //And then you do the same thing here, but with Class3; then you show it. 
            Class3 frameFromclass3 = new Class3();

            frameFromclass3.setVisible(true);
        }
    }   
}

We aren't able to see what your other classes have in their constructors, so I can't say whether or not there's also a problem happening there. I can, however, offer better alternatives.

Generally you want to keep 1 JFrame, even if you have popup windows or multiple "pages" that can be used. Consider using multiple JPanels, one per layout. Alternatively you can use a JTabbedPane, and have different information on each tab. For login purposes, a common option is the JOptionPane or a JDialog. If you insist on using multiple JFrames and you want to move components between them, variables within your class (not a method) should be used to store information, then use public methods to access those variables from the other classes. Once a window is done being used, however, you should destroy it instead of hide it. Here's a great resource on various ways to do that.

huangapple
  • 本文由 发表于 2023年3月21日 01:51:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75793667-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定