所以我在我的主要课程中有这个代码:
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JLabel;
@SuppressWarnings("serial")
public class Main extends JPanel{
private final int height = 285;
private final int width = 416;
private static final double appversion = 0.1;
private BufferedImage sdk001;
private static final String appname = "App's Name";
private static final String appstage = "Alpha";
private JFrame frame;
public Main() {
try {
sdk001 = ImageIO.read(getClass().getResourceAsStream("img\\cards\\sdk001.png"));
frame = new JFrame(appname + " (" + appversion + " " + appstage + ")");
frame.add(new Main());
frame.setSize(1020, 680);
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
catch (IOException e) {
Logger LogErr = Logger.getLogger(Main.class.getName());
System.err.println("Caught IOException: " + e.getMessage());
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(sdk001, 0, 0, height, width, null);
}
public static void main(String[] args) {
Main main = new Main();
JFrame frame = main.getFrame();
}
public JFrame getFrame() {
return frame;
}
我试着在我的" A"上课,这里是代码:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
public class A {
public A(Main main){
JFrame frame = main.getFrame();
frame.add(new JLabel("Hi OtherClass did this") );
}
代码返回时没有错误,但没有显示JFrame或任何GUI。谁能帮我这个;我对此感到非常困惑。感谢。
答案 0 :(得分:0)
您需要使用JFrame
关键字初始化new
并使其显示首选大小,而不是您可以查看GUI。
Main main = new Main();
JFrame frame = new JFrame(); //main.getFrame();
frame.add(main); // Add JPanel with components to JFrame
frame.setSize(300,300);
frame.setVisible(true);