[英]Help with SpringLayout?
使用Java的SpringLayout管理器時,我遇到一個非常奇怪的問題。 我想讓工具欄出現在我的程序中。 它在我的程序的較早版本上可以正常工作,但現在不起作用。 基本上,如果我從JPanel的實例中刪除layout參數,則即使沒有自定義,我在JPanel中添加的元素也會顯示出來。 如果我在實例化中具有該參數,則工具欄根本不會出現。 我不知道我頂起了什么或做錯了什么。 JPanel正進入一個中央JFrame,我已經從BorderLayout更改為另一個SpringLayout,到什么都沒有,它似乎並沒有影響這個問題。
public class purchaserApp
{
static JFrame mainWindow; //Main window
static JFrame addView = new JFrame ("Add An Item..."); //Add item window
static JFrame aboutView = new JFrame ("About Purchaser"); //About window
static JFrame helpView = new JFrame ("Purchaser Help"); //Help window
static JPanel toolBar, contentArea, previewPane; //Panels for GUI
static JRootPane root;
static JToolBar toolBar2;
//SpringLayout mainLayout;
JTable table;
public purchaserApp()
{
makemainWindow();
makeMenu();
makeToolbar();
// makeMainContentView();
mainWindow.setVisible(true);
}
public void makeToolbar()
{
SpringLayout tbLayout = new SpringLayout();
toolBar = new JPanel(tbLayout); //this is the offending line of code, if I remove "tbLayout" the buttons show up in the GUI but obviously without the customizations I made below...
JButton toolBarButtons[];
String buttonNames[] = {"Add", "Edit", "Delete"};
//Instantiate buttons for toolbar
toolBarButtons = new JButton[3];
//Resize
Dimension d = new Dimension (40,55);
//Add/modify/whatever
for (i = 0; i < 3; i++)
{
toolBarButtons[i] = new JButton(buttonNames[i]);
toolBarButtons[i].setPreferredSize(d);
tbLayout.putConstraint(SpringLayout.NORTH, toolBarButtons[i], 5, SpringLayout.NORTH, toolBar);
}
//Adjust constraints
tbLayout.putConstraint(SpringLayout.WEST, toolBarButtons[0], 5, SpringLayout.WEST, toolBar);
tbLayout.putConstraint(SpringLayout.WEST, toolBarButtons[1], 5, SpringLayout.EAST, toolBarButtons[0]);
tbLayout.putConstraint(SpringLayout.EAST, toolBarButtons[2], -5, SpringLayout.EAST, toolBar); //due to x-axis, we must go negative to get inside the frame
for (i = 0; i < 3; i++)
toolBar.add(toolBarButtons[i]);
mainWindow.add(toolBar,BorderLayout.NORTH); //Lies! Does not add
}
我在這里包括了類和令人討厭的方法。 任何幫助將不勝感激,因為我確信我不是第一個遇到此問題的人。 如果這是一個相對簡單的修復程序,我也表示歉意,但我沒有看到。 我仍然對Java GUI(和一般Java)還是陌生的,所以請原諒。
為什么要嘗試使用面板創建工具欄? 使用自己的自定義布局的JToolBar有什么問題?
即使您確實創建了自定義工具欄面板,我也不會使用SpringLayout,它太復雜了。 只需使用FlowLayout或水平BoxLayout。
閱讀有關布局管理器的Swing教程,以獲取每個布局管理器的工作示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.