所以我已经为此工作了 2 天,并且我已经掌握了 java.swing 的基础知识(我希望,至少,因为我在 2 天前开始学习它。)无论如何,我成功加载了背景图片,但我似乎无法让前台工作。我不确定您需要什么代码,所以我会发布所有代码。当您查看它时,我是否正确设置了我的 JPanel?(特别是 allContent 和fourRows 。)
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.util.*;
import junit.framework.Test;
public class MainFrame {
//Variables
private static final String IMAGE_PATH = "imageFolder/warlordsOrganizerBackground.png";
private static final String IMAGE_PATH2 = "imageFolder/warlordsLogo.png";
public static JFrame programFrame;
public static JLabel warlordsBackground;
public static JLabel warlordsLogo;
public static JPanel allContent;
public static JPanel fourRows;
public static JScrollPane scrollPane;
//Making the parts for the GUI
public static void createGUI(){
//programFrame Title and Layout
programFrame = new JFrame("Warlords Organizer");
programFrame.setLayout(new BorderLayout());
Icon backgroundIcon = new ImageIcon(IMAGE_PATH);
warlordsBackground = new JLabel(backgroundIcon);
File imageFile = new File(IMAGE_PATH);
File imageFile2 = new File(IMAGE_PATH2);
//Warlords Logo JLabel
Icon logoIcon = new ImageIcon(IMAGE_PATH2);
warlordsLogo = new JLabel(logoIcon);
//New JPanel for GridLayout
fourRows = new JPanel(new GridLayout(0,4));
fourRows.setLayout(new GridLayout());
//Makes the Initial BorderLayout (Using allContent JPanel)
allContent = new JPanel();
allContent.setLayout(new BorderLayout());
allContent.add(warlordsLogo, BorderLayout.NORTH);
allContent.setVisible(true);
allContent.add(fourRows, BorderLayout.CENTER);
//Add ScrollPane / MAKE SURE TO ADD TO new JScrollPane WHERE IT NEEDS TO BE / TEXT
scrollPane = new JScrollPane(allContent);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setOpaque(false);
scrollPane.getViewport().setOpaque(false);
//JFrame programFrame Constructors
programFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
programFrame.setContentPane(warlordsBackground);
programFrame.pack();
programFrame.setVisible(true);
programFrame.setResizable(false);
} // public static void createGUI() Closing
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createGUI();
} //public void run() Closing
});
}
}
最后,我的 JScrollPane。我需要对此做点什么,所以不要介意。我这样设置我的项目文件,如果它有帮助的话 -
在我的图像工作后,我需要弄清楚如何获得自定义字体(BEBAS__.ttf),所以如果你也有一些资源,我将不胜感激。