GridBagConstraints c = new GridBagConstraints(); c.gridx = 0; c.gridy = 0; c.anchor = GridBagConstraints.SOUTH; frame.add(new JButton("Button"), c);
GridBagConstraints c = new GridBagConstraints(); c.gridx = 0; c.gridy = 0; c.anchor = GridBagConstraints.NORTHEAST; frame.add(new JLabel("Label"), c);This code creates a GridBagConstraints object, sets its anchor property to NORTHEAST, and adds a JLabel component to the frame at row 0, column 0. The JLabel will be aligned to the top-right corner of its cell. The GridBagConstraints.anchor class is part of the java.awt package library.