好的,今天还有一个问题。我想要一个 BorderLayout,我的徽标在顶部,而不是三列,而是四列在中心。有没有办法让我编辑 BorderLayout 管理器,或者我只需要自己制作一个?(如果我必须自己做一个,我从哪里开始,因为我以前从来没有自己做?)
目前我的代码,还没有添加任何文本或任何类似的东西(尽管我尝试在图像中添加,但出于某种奇怪的原因,它不起作用,希望我能解决这个问题。)
public static void createGUI(){
JFrame programFrame = new JFrame("Warlords Organizer");
programFrame.setLayout(new BorderLayout());
Icon backgroundIcon = new ImageIcon(IMAGE_PATH);
JLabel contentLabel = new JLabel(backgroundIcon);
contentLabel.setLayout(new BorderLayout());
File imageFile = new File(IMAGE_PATH);
File imageFile2 = new File(IMAGE_PATH2);
//Warlords Logo JLabel
Icon logoIcon = new ImageIcon(IMAGE_PATH2);
JLabel warlordsLogo = new JLabel(logoIcon);
warlordsLogo.setLayout(new BorderLayout());
//JFrame programFrame Constructors
programFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
programFrame.setContentPane(contentLabel);
programFrame.pack();
programFrame.setVisible(true);
programFrame.setResizable(false);
} // public static void createGUI() Closing
(徽标的问题不在于文件路径,因为我在未发布的代码中引用了该路径。)
所以,是的,一次排序 2 个问题;我做错了什么标志?以及如何编辑(或制作)布局以使其适合顶部的位置(如 BorderLayout PAGE_START),但中心有 4 列?
编辑:如果我没有足够的信息需要帮助,我深表歉意,我不确定我可以提供哪些其他代码。我决定用这个,我希望它有效 -
//Makes the Initial BorderLayout
JPanel allContent = new JPanel();
allContent.setLayout(new BorderLayout());
//New JPanel for GridLayout
JPanel fourRows = new JPanel(new GridLayout(0,4));
fourRows.setLayout(new GridLayout());
allContent.add(warlordsLogo, BorderLayout.NORTH);
allContent.add(fourRows, BorderLayout.CENTER);
我很确定将布局放在 JPanelfourRows 上,然后再做fourRows.setLayout 是多余的。
我的最终意图是在顶部放置徽标,在中间放置四列,我可以在其中添加面板和按钮。我使用 (0,4) 是因为我不确定最终会得到多少行,按照这里 - https://stackoverflow.com/a/5657131/1676781
我能做些什么来修复我的代码?(这里是)-
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class MainFrame {
//Image Paths
private static final String IMAGE_PATH = "imageFolder/warlordsOrganizerBackground.png";
private static final String IMAGE_PATH2 = "imageFolder/warlordsLogo.png";
//Making the parts for the GUI
public static void createGUI(){
JFrame programFrame = new JFrame("Warlords Organizer");
programFrame.setLayout(new BorderLayout());
Icon backgroundIcon = new ImageIcon(IMAGE_PATH);
JLabel contentLabel = new JLabel(backgroundIcon);
File imageFile = new File(IMAGE_PATH);
File imageFile2 = new File(IMAGE_PATH2);
//Warlords Logo JLabel
Icon logoIcon = new ImageIcon(IMAGE_PATH2);
JLabel warlordsLogo = new JLabel(logoIcon);
//Makes the Initial BorderLayout
JPanel allContent = new JPanel();
allContent.setLayout(new BorderLayout());
//New JPanel for GridLayout
JPanel fourRows = new JPanel(new GridLayout(0,4));
fourRows.setLayout(new GridLayout());
allContent.add(warlordsLogo, BorderLayout.NORTH);
allContent.add(fourRows, BorderLayout.CENTER);
//Add ScrollPane MAKE SURE TO ADD TO new JScrollPane WHERE IT NEEDS TO BE / TEXT
JScrollPane scrollPane = new JScrollPane();
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(contentLabel);
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
});
}
}