gpt4 book ai didi

java - 尝试使用 Java Swing 显示标签

转载 作者:行者123 更新时间:2023-11-30 08:59:58 26 4
gpt4 key购买 nike

我正在通过 Java 取得进展,并开始了一个“包罗万象”的小项目,将它们拼凑在一起,但我几乎一开始就被一个问题挂断了。

目前,该应用程序基本上只是应该打开一个由三个选项卡组成的 GUI,每个选项卡具有不同的内容;一种用于设计桌面 Activity ,一种用于运行它,一种用于数据库管理。

我相信我已经正确添加了所有组件,但是当我运行它进行测试时,我得到一个空的、未调整大小的应用程序 Pane 。基本上只是调整大小按钮和一条线。我可以调整它的大小,但尽管它显示了菜单栏,但我们没有看到 Pane 、没有选项卡、没有任何内容。我一直在 kajiggering 一次让它们显示,甚至从 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);
}
}

我不认为它在 main 方法中,但这是我调用和运行 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);
}
});
}

}

最佳答案

您没有向您的框架添加任何内容。因此你得到一个空的。因此,在 initComponents 方法的末尾调用 add 方法将您之前创建的组件添加到框架中:

private void initComponents() 
{

/* ... */

setJMenuBar(menuBar);
add(gmGuiTabs);
pack();
}

这将显示例如您的选项卡式 Pane 。

关于java - 尝试使用 Java Swing 显示标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27011203/

26 4 0