gpt4 book ai didi

java - 从另一个类调用 IntelliJ Swing Forms

转载 作者:太空宇宙 更新时间:2023-11-04 10:48:48 25 4
gpt4 key购买 nike

我刚刚开始接触 Java,我遇到了这个我不理解的概念。

我想知道如何打开另一个类中使用 IntelliJ 的 Swing 表单编辑器开发的表单。请参阅下面的代码,该代码在一个类中运行良好。

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GUIApp extends JPanel{
private JButton button1;
private JLabel label1;
private JPanel panel1;
private int iCount;


public GUIApp() {

button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
iCount++;
label1.setText(Integer.toString(iCount));
}
});
}

public static void main(String[] args){
JFrame frame = new JFrame("GUIApp");
frame.setContentPane(new GUIApp().panel1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);

}

}

当我尝试修改它以使用 main 方法从另一个类创建表单时,我遇到了麻烦。

GUIApp.java

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GUIApp extends JPanel{
private JButton button1;
private JLabel label1;
public JPanel panel1;
private int iCount;


public GUIApp() {

button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
iCount++;
label1.setText(Integer.toString(iCount));
}
});
}
}

应用程序.java

import javax.swing.*;

public class App {

public static void main(String[] args){


SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("GUIApp");
frame.setContentPane(new GUIApp().panel1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
});
}
}

我尝试将许多不同的内容传递给 setContentPane 方法,但没有成功。

我目前收到的错误消息如下:

Exception in thread "main" java.lang.NoSuchMethodException: GUIApp.main([Ljava.lang.String;)
at java.lang.Class.getMethod(Class.java:1786)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:125)

最佳答案

啊……这很简单。令我惊讶的是没有人跳出来解码错误消息,但我想我需要在早上用新的眼光来查看它,才能找到它。

我认为主要问题是编译器试图运行 GUIApp 类而不是 App 类,并且因为我删除了主类而感到害怕。即,我在进行更改后没有执行正确的类。

最后我使用了这段代码:

应用程序.java

import javax.swing.*;

public class App {

public static void main(String[] args){


SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {

JFrame frame = new JFrame("GUIApp");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new GUIApp().getter());
frame.pack();
frame.setVisible(true);


}
});
}
}

GUIApp.java

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GUIApp extends JPanel{
private JButton button1;
private JLabel label1;
private JPanel panel1;
private int iCount;

public GUIApp() {

button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
iCount++;
label1.setText(Integer.toString(iCount));
}
});
}

public JPanel getter(){
return this.panel1;
}

}

关于java - 从另一个类调用 IntelliJ Swing Forms,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48079805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com