- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在制作的游戏中无法正确布置某些组件。我的 GUI 由带有 BorderLayout 的外部 JFrame 组成,在 BorderLayout 的东侧我有一个带有 GridBagLayout 的 JPanel。我试图在 JPanel 中定位 4 个按钮(最终还有其他一些东西,但当它不起作用时我将其缩减为按钮),并将它们的 gridx 和 gridy 值设置为将它们放入 2 行 2 列中。但是,由于某种原因它不起作用。它没有将按钮放在两行两列中,而是仅显示一个按钮 ( screenshot )。
这是我的代码:
private class SidePanel extends JPanel
{
private GridLayout sidePaneLayout;
private JButton startButton;
private JButton quitButton;
private JButton saveButton;
private JButton loadButton;
private ButtonHandler buttonHandler;
private JLabel stardate;
private JLabel condition;
private JLabel position;
private JLabel warpFactor;
private JLabel energy;
private JLabel torpedos;
private JLabel shields;
private JLabel klingonsLeft;
private JPanel statusPanel;
public SidePanel()
{
super();
this.setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
startButton = new JButton("New Game");
constraints.gridx = 0;
constraints.gridy = 0;
this.add(startButton, constraints);
quitButton = new JButton("Quit Game");
constraints.gridx = 0;
constraints.gridy = 1;
this.add(startButton, constraints);
saveButton = new JButton("Save Game");
constraints.gridx = 1;
constraints.gridy = 0;
this.add(startButton, constraints);
loadButton = new JButton("Load Game");
constraints.gridx = 1;
constraints.gridy = 1;
this.add(startButton, constraints);
/*Dimension buttonSize = new Dimension(155, 60);
startButton.setPreferredSize(buttonSize);
quitButton.setPreferredSize(buttonSize);
saveButton.setPreferredSize(buttonSize);
loadButton.setPreferredSize(buttonSize);*/
stardate = new JLabel("Stardate: ");
condition = new JLabel("Condition: ");
position = new JLabel("Position: ");
warpFactor = new JLabel ("Warp Factor: ");
energy = new JLabel("Energy: ");
torpedos = new JLabel("Torpedos: ");
shields = new JLabel("Shields: ");
klingonsLeft = new JLabel("Klingons Left: ");
statusPanel = new JPanel(new GridLayout(0, 2));
statusPanel.add(stardate);
statusPanel.add(condition);
statusPanel.add(position);
statusPanel.add(warpFactor);
statusPanel.add(energy);
statusPanel.add(torpedos);
statusPanel.add(shields);
statusPanel.add(klingonsLeft);
constraints.gridx = 0;
constraints.gridy = 2;
//this.add(statusPanel, constraints);
}
知道为什么这不起作用吗?我在独立的 JFrame 中使用 GridBagLayout 做了一些其他测试,它们都有效。但是当我尝试在该程序的内部 JPanel 中使用它时,它失败了。
最佳答案
您添加了 startButton
按钮 4 次,这就是它只显示一次的原因。
this.add(startButton, constraints);
<小时/>
现在经过一些修正后,如下所示。
constraints.insets=new Insets(5, 5, 5, 5);// add top, left, bottom, right insets
...
constraints.gridx = 0;
constraints.gridy = 2;
constraints.gridwidth=2; // span two column
this.add(statusPanel, constraints);
关于java - GridBagLayout 不遵守 gridx 和 gridy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23529245/
我想我应该把这个发布给其他可能遇到同样问题的人。我遇到过一种情况,我正在将数据加载到网格存储中,但每一行都显示相同的确切信息(并且存储中的数据不同)。事实证明,您必须在存储(在我的例子中是内存存储)中
我没有成功地显示我的 GridX。我目前在 firebug 中收到 4 个与 dojo.js 有关的错误,其中两个 Error: scriptError 一个 Error: multipleDefin
根据文档: https://github.com/oria/gridx/wiki/Create-the-Simplest-Gridx Never forget to call grid.startup
GridX 是否可以根据该行中的数据为给定的行设置类? 我想用特殊颜色显示添加或更改的行。该信息将作为特殊字段存储在对象中。 至于现在,我只发现了如何个性化单元格的渲染(使用装饰器,或覆盖setCel
我正在尝试创建一个程序化过滤器。我有一个 dijit.tree 和一个 dojo gridx 在 jsp 上使用相同的源。当用户单击树 Node 时,我想使用该 Node 作为过滤器并在 gridx
我有一个使用 Gridx 创建的网格,它将用于监视和管理条目。当用户单击表中的一行时,会弹出一个对话框,允许用户选择忽略或响应。单击任一选项时,我必须更新 SQL 数据库中的一个条目(从中提取此信息的
根据文档: https://github.com/oria/gridx/wiki/Create-the-Simplest-Gridx Never forget to call grid.startup
我正在使用 dojo 1.9 和 gridx。网格使用内存存储进行初始化。但是当数据发生变化时,我更新了商店,但我没有看到任何变化应用于网格。它没有 refresh()方法(如 dgrid)。但是,我
我正在使用 Dojo 1.9 和 GridX 1.2。我只是将 ComboBox 配置为网格中单元格的编辑器。 我在示例中发现了以下配置语法: editor: "dijit/form/Comb
我正在尝试在单击相关行时检索存储在 dojo.gridx 表中的数据值。只要第一次调用以下函数,它就可以正常工作,当使用新数据再次调用该函数时,这些数据会正确显示在屏幕上,但是当我单击任何行时,检索到
我能够将标题中的文本居中,使用这个: [colid="startstop"].gridxCell{ text-align: center; } 我认为这会使属于 startstop 列的所有行
我编写了一个简单的程序来从外部 JSON 文件获取数据并将其显示在 Dojo Gridx 中。但是,它不起作用。 Dojo 代码: require(["dojo/text!json_to_gridx/
我正在使用 Dojo 1.9 和 GridX 1.2。我想检测用户更改给定行(或单元格)的事件。然后将执行其他操作,例如一行将更改背景颜色并启用“保存”按钮。 我正在搜索示例和文档,但没有找到示例。如
是否 GridX 支持单元格中文本的 HTML 转义吗? 我未能从 dojox.grid.DataGrid 中找到类似 escapeHTMLInData 的属性。 GridX 是否有内置的 HTML
我正在使用 dojo.gridx 来显示我的值。有时用户可以创建一个新行。这样我就添加了一个新按钮,当点击 newRow 按钮时,将调用 onclick 方法。 在该方法中创建了新的行代码。我的代码如
我在制作的游戏中无法正确布置某些组件。我的 GUI 由带有 BorderLayout 的外部 JFrame 组成,在 BorderLayout 的东侧我有一个带有 GridBagLayout 的 JP
我使用带有 jsonrest 的 Gridx 作为存储和分页模块。过滤只能在客户端完成吗?目前它向 jsonrest 存储发送请求并查询服务器。 最佳答案 是的,可以做到,看这个: https://g
我正在尝试使用 gridx 和 gridy 约束来定位我的按钮。但它们不起作用!如果我更改 gridx 和 gridy 变量,什么也不会发生。如果我将 fill to GridBagConstrain
我正在尝试使用 gridx 和 gridy 约束来创建如下所示的界面(忽略“下一步”按钮)。前两个 JLabel-JTextField 对工作正常,但我不确定我为其余 10 个字段设置了什么 grid
我有 gridx/Grid ( http://oria.github.io/gridx/ ),我想在标题单元格中添加一些小部件,例如文本框、下拉菜单等。有没有办法将小部件放入标题单元格中? 最佳答案
我是一名优秀的程序员,十分优秀!