问题描述
我试图使用的gridx
和 gridy
约束来定位我的按钮。但他们没有工作!如果我更改的gridx
和 gridy
变量,没有任何反应。如果我将填充更改为的GridBagConstraints
到无
,它仍然无法正常工作。
我失去了一些东西在这里?
进口java.awt中的*。
进口的javax.swing *。公共类窗口扩展的JFrame {
私有静态最后的serialVersionUID长1L =; JFrame的帧=新的JFrame(GUI);
JTextField的用户名=新的JTextField(20); 公共无效的CreateWindow(){
JPanel的窗格=新JPanel();
pane.setLayout(新的GridBagLayout());
GridBagConstraints的C =新的GridBagConstraints(); c.fill = GridBagConstraints.NONE; JButton的按钮=的新的JButton(按钮1);
c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 3; //没有任何影响
c.gridy = 5; //没有任何影响
c.anchor = GridBagConstraints.NORTHWEST; //如果我删除此,它仍然无法正常工作。
pane.add(按钮,c);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setSize(400,600);
frame.setResizable(真);
frame.setLocationRelativeTo(NULL);
frame.add(面板);
frame.setVisible(真);
}
}
如果这是难以辨认,这里是问题所在:
的JPanel面板=新JPanel();
pane.setLayout(新的GridBagLayout());
GridBagConstraints的C =新的GridBagConstraints(); c.fill = GridBagConstraints.NONE; JButton的按钮=的新的JButton(按钮1);
c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 3; //没有任何影响
c.gridy = 5; //没有任何影响
c.anchor = GridBagConstraints.NORTHWEST; //如果我删除此,它仍然无法正常工作。
pane.add(按钮,c);
它已经一段时间,因为我做了挥杆的布局,但不为gridx和gridY只有有特效,如果你在你的JPanel多个JComponent的吗?看来你只有一个JButton,所以GridBagLayout中没有任何的布局做的。
I am trying to use the gridx
and gridy
constraints to position my button. But they do not work! If I change the gridx
and gridy
variables, nothing happens. If I change the fill to GridBagConstraints
to NONE
, it still does not work.
Am I missing something here?
import java.awt.*;
import javax.swing.*;
public class Window extends JFrame{
private static final long serialVersionUID = 1L;
JFrame frame = new JFrame("GUI");
JTextField username = new JTextField(20);
public void CreateWindow(){
JPanel pane = new JPanel();
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.NONE;
JButton button = new JButton("Button 1");
c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 3; //Has no effect
c.gridy = 5; //Has no effect
c.anchor = GridBagConstraints.NORTHWEST;//If I remove this, it still does not work.
pane.add(button, c);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setSize(400, 600);
frame.setResizable(true);
frame.setLocationRelativeTo(null);
frame.add(pane);
frame.setVisible(true);
}
}
If that is hard to read, here is where the problem lies:
JPanel pane = new JPanel();
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.NONE;
JButton button = new JButton("Button 1");
c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 3; //Has no effect
c.gridy = 5; //Has no effect
c.anchor = GridBagConstraints.NORTHWEST; //If I remove this, it still does not work.
pane.add(button, c);
It's been a while since I've done swing layouts, but don't gridX and gridY only have effects if you have more than one JComponent in your JPanel? It seems you only have a JButton, so the GridBagLayout doesn't have any layout to do.
这篇关于Java的GridBagConstraints对象的gridx和gridy不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!