我有一个名为 Window 的类,它扩展了 JFrame 并设置了基本布局。我想向那个 JFrame 添加不同的 JPanel,一个名为 Tracking 的单独类决定在 JFrame 中显示哪个 JPanel。我希望 Tracking 类能够更改 JFrame 中显示的 JPanel。
每个 JPanel 是一个单独的类,JFrame 和 Tracking 类也是单独的类。
窗口.java
package Setup;
import javax.swing.JFrame;
public class Window extends JFrame
{
private static final int width = 1280;
private static final int height = 720;
public Window()
{
setResizable(false);
setSize(width, height);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// frame.setUndecorated(true);
// frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
setVisible(true);
}
public static void main(String[] args)
{
new Window();
}
}
动画.java
package Setup;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JPanel;
public class Animation extends JPanel
{
private final int width = super.getWidth();
private final int height = super.getHeight();
private Image dbImage;
private Graphics dbGraphics;
public Animation()
{
setSize(width, height);
setVisible(true);
}
@Override
public void paint(Graphics g)
{
dbImage = createImage(getWidth(), getHeight());
dbGraphics = dbImage.getGraphics();
paintComponent(dbGraphics);
g.drawImage(dbImage, 0, 0, this);
}
@Override
public void paintComponent(Graphics g)
{
//ADD Animation to get attention from users
g.drawString("TEST TEST TEST", 50, 50);
repaint();
}
}