Я пытаюсь использовать ограничения gridx
и gridy
для позиционирования моей кнопки.Но они не работают!Если я изменю переменные gridx
и gridy
, ничего не произойдет.Если я изменю заливку на GridBagConstraints
на NONE
, она все равно не будет работать.
Я что-то здесь упускаю?
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);
}
}
Если это трудно прочитать, вот в чем проблема:
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);