好的,那天的另一个问题。 我想要一个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);
我很确定将布局放在JPanel fourRows上,然后执行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
});
}
}
答案 0 :(得分:1)
仅使用NORTH
的{{1}},CENTER
和SOUTH
。在Borderlayout
中,将CENTER
与4列JPanel
放在一起。