好的,這是另一個問題。 我想要一個BorderLayout,我在頂部有標識,而不是三個,但中間是四列。有沒有辦法讓我編輯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
});
}
}
*「不是三個,而是四個中心的列」*「BorderLayout」的「CENTER」中只有一個「列」。爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/)。 – 2013-04-30 09:47:26
@AndrewThompson,我想他說的是佈局的垂直中心而不是'BorderLayout.CENTER'。所以'EAST','CENTER'和'WEST'。 – DeadlyJesus 2013-04-30 09:50:39
@DeadlyJesus *「我認爲他在說」* .. SSCCE說話比語言更響亮。 (而且我比信息更感興趣的信息。) – 2013-04-30 09:53:15