У меня очень странная проблема, когда я использую диспетчер SpringLayout в Java.Я очень хочу, чтобы панель инструментов появилась в моей программе.Это работало на более раннем этапе моей программы, и теперь это не работает.По сути, если я удаляю параметр макета из своего экземпляра JPanel, отображаются элементы, добавленные в 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 в целом), поэтому, пожалуйста, извините.