英文:
How do I use a Main class to implement Swing Components from other classes in my project?
问题
我正在开发一个项目,其中我有 "Class1","Class2" 和 "Class3"。
Class2 和 Class3 都创建了 JFrame,每个包含各种 JButton、JLabel 和其他 Swing 组件。我如何在 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);
}
}
}
我们无法看到你的其他类在它们的构造函数中有什么内容,所以我无法确定是否在那里也存在问题。不过,我可以提供更好的替代方案。
通常情况下,即使有弹出窗口或多个“页面”可供使用,你也应该保留一个JFrame
。考虑使用多个JPanels
,每个对应一种布局。或者你可以使用JTabbedPane
,每个选项卡上都可以放置不同的信息。对于登录目的,常见选项是JOptionPane
或JDialog
。如果你坚持使用多个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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论