我正在通过java进行并开始了一个小小的“全能”项目,将它拼凑在一起,而且我几乎没有出现问题。
目前,应用程序基本上只是打开一个由三个选项卡组成的GUI,每个选项卡都有不同的内容;一个用于设计桌面活动,一个用于运行它,另一个用于数据库管理。
我相信我已经正确地添加了所有组件,但是当我运行它来测试时,我得到一个空的,未实现的应用程序窗格。基本上只是调整大小按钮和一行。我可以调整它的大小,但是虽然它显示了菜单栏,但我们没有窗格,没有标签,没有nuthin。我一直在讨价还价,让它们显示甚至从netbeans中的gui构建器重建它,并根据生成的代码对其进行重新构建(我有一些自定义组件要包含并希望学习手动编写GUI) 。
有谁能发现我在这里做错了什么?
package com.bmagus.tabletop.gui;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class GMGui extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public GMGui() {
initComponents();
lookAndFeel();
}
//Component variables
private JCheckBox dbSearchMod1;
private JComboBox pcSelector;
private JComboBox npcSelector;
private JMenu fileMenu;
private JMenu editMenu;
private JMenu helpMenu;
private JMenuBar menuBar;
private JMenuItem fileExit;
private JPanel designPanel;
private JPanel runPanel;
private JPanel dbPanel;
private JScrollPane designNotesScroll;
private JScrollPane runNotesScroll;
private JTabbedPane gmGuiTabs;
private JTextArea designNotes;
private JTextArea runNotes;
private JTextField dbSearchField;
private void setGbc(GridBagConstraints localGbc, int x, int y, int height, int width, double xweight, double yweight){
localGbc.gridx=x;
localGbc.gridy=y;
localGbc.gridheight=height;
localGbc.gridwidth=width;
localGbc.fill = GridBagConstraints.BOTH;
localGbc.weightx=xweight;
localGbc.weighty=yweight;
localGbc.insets = new Insets(20, 20, 20, 20);
}
private void lookAndFeel()
{
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(GMGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(GMGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(GMGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(GMGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
}
private void initComponents() {
GridBagConstraints gbc = new GridBagConstraints();
GridBagLayout guiLayout = new GridBagLayout();
gmGuiTabs = new JTabbedPane();
designPanel = new JPanel();
designNotesScroll = new JScrollPane();
designNotes = new JTextArea();
runPanel = new JPanel();
runNotesScroll = new JScrollPane();
runNotes = new JTextArea();
pcSelector = new JComboBox();
npcSelector = new JComboBox();
dbPanel = new JPanel();
dbSearchField = new JTextField();
dbSearchMod1 = new JCheckBox();
menuBar = new JMenuBar();
fileMenu = new JMenu();
fileExit = new JMenuItem();
editMenu = new JMenu();
helpMenu = new JMenu();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//Campaign Design Tab
designPanel.setLayout(guiLayout);
designNotes.setColumns(20);
designNotes.setRows(5);
designNotesScroll.setViewportView(designNotes);
setGbc(gbc, 0,0,5,3,1.0d,1.0d);
gmGuiTabs.addTab("Campaign Design", designPanel);
//Run Game tab
runPanel.setLayout(guiLayout);
runNotes.setColumns(20);
runNotes.setRows(5);
runNotesScroll.setViewportView(runNotes);
setGbc(gbc, 0,0,5,3,1.0d,1.0d);//Todo
runPanel.add(runNotesScroll, gbc);
pcSelector.setModel(new DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
setGbc(gbc,6,0,1,1,0.0d,0.0d);//Todo
runPanel.add(pcSelector, gbc);
npcSelector.setModel(new DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
setGbc(gbc,0,1,1,1,0d,0d);//todo
runPanel.add(npcSelector, gbc);
gmGuiTabs.addTab("Run Game", runPanel);
//Database Tab
dbPanel.setLayout(guiLayout);
dbSearchField.setText("jTextField1");
setGbc(gbc,0,0,3,1,0d,0d);//Todo
dbPanel.add(dbSearchField, gbc);
dbSearchMod1.setText("jCheckBox1");
setGbc(gbc,1,0,1,1,0d,0d);//Todo
dbPanel.add(dbSearchMod1, gbc);
gmGuiTabs.addTab("Database", dbPanel);
//MenuBar Elements
fileMenu.setText("File");
fileExit.setText("jMenuItem1");
fileMenu.add(fileExit);
menuBar.add(fileMenu);
editMenu.setText("Edit");
menuBar.add(editMenu);
helpMenu.setText("Help");
menuBar.add(helpMenu);
setJMenuBar(menuBar);
}
}
我不认为它在主方法中,但这是我调用并运行GUI的方法。
package com.bmagus.tabletop;
import java.awt.EventQueue;
import com.bmagus.tabletop.gui.GMGui;
public class Game {
public static void main(String[] args){
EventQueue.invokeLater(new Runnable() {
@Override
public void run(){
GMGui gmgui = new GMGui();
gmgui.setVisible(true);
}
});
}
}
答案 0 :(得分:2)
您不能在框架中添加任何内容。因此你得到一个空的。因此,请在initComponents
方法的末尾调用add
方法,将您之前创建的组件添加到框架中:
private void initComponents()
{
/* ... */
setJMenuBar(menuBar);
add(gmGuiTabs);
pack();
}
这将显示您的选项卡式窗格。