如何使用主类在我的项目中实现来自其他类的Swing组件?

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

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.

  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3. import javax.swing.JButton;
  4. import javax.swing.JFrame;
  5. @SuppressWarnings("serial")
  6. public class Class1 extends JFrame implements ActionListener
  7. {
  8. public static void main(String[] args) {
  9. Class2 frameFromclass2 = new Class2();
  10. frameFromclass2.setVisible(true);
  11. }
  12. Class2 buttonMovetoclass3 = new Class2();
  13. public void actionPerformed(ActionEvent e) {
  14. if (buttonMovetoclass3 == e.getSource()) {
  15. Class2 frameFromclass2 = new Class2();
  16. frameFromclass2.setVisible(false);
  17. Class3 frameFromclass3 = new Class3();
  18. frameFromclass3.setVisible(true);
  19. }
  20. }
  21. }

答案1

得分: 0

部分你的问题是由于对变量作用域的理解不足引起的。你问题的另一部分是因为方法不太合适。

  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3. import javax.swing.JButton;
  4. import javax.swing.JFrame;
  5. @SuppressWarnings("serial")
  6. public class Class1 extends JFrame implements ActionListener
  7. {
  8. //这是一个方法。在这个方法内声明的每个变量只能在该方法内访问。
  9. public static void main(String[] args) {
  10. //包括这个。
  11. Class2 frameFromclass2 = new Class2();
  12. //你在这里使用了它,但不仅它被丢弃了,而且它不能在其他地方使用。
  13. 它被包含在花括号的限制范围内
  14. frameFromclass2.setVisible(true);
  15. }
  16. Class2 buttonMovetoclass3 = new Class2();
  17. public void actionPerformed(ActionEvent e) {
  18. if (buttonMovetoclass3 == e.getSource()) {
  19. //在这里你创建一个新的Class2对象,并赋予它与其他相同的名称,然后隐藏它。
  20. Class2 frameFromclass2 = new Class2();
  21. frameFromclass2.setVisible(false);
  22. //然后在这里你做了相同的事情,但是使用Class3;然后你显示它。
  23. Class3 frameFromclass3 = new Class3();
  24. frameFromclass3.setVisible(true);
  25. }
  26. }
  27. }

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

通常情况下,即使有弹出窗口或多个“页面”可供使用,你也应该保留一个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.

  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3. import javax.swing.JButton;
  4. import javax.swing.JFrame;
  5. @SuppressWarnings("serial")
  6. public class Class1 extends JFrame implements ActionListener
  7. {
  8. //This is a method. Every variable declared within this method,
  9. //is only accessible from within this method.
  10. public static void main(String[] args) {
  11. //That includes this one.
  12. Class2 frameFromclass2 = new Class2();
  13. //You've used it here, but not only is it then abandoned, but it
  14. //cannot be used anywhere else. It is contained in a cage of curly braces.
  15. frameFromclass2.setVisible(true);
  16. }
  17. Class2 buttonMovetoclass3 = new Class2();
  18. public void actionPerformed(ActionEvent e) {
  19. if (buttonMovetoclass3 == e.getSource()) {
  20. //Here you create a NEW Class2 object and give it the same
  21. //name you gave the other, then you hide it.
  22. Class2 frameFromclass2 = new Class2();
  23. frameFromclass2.setVisible(false);
  24. //And then you do the same thing here, but with Class3; then you show it.
  25. Class3 frameFromclass3 = new Class3();
  26. frameFromclass3.setVisible(true);
  27. }
  28. }
  29. }

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-4.html
匿名

发表评论

匿名网友

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

确定