Расположение элементов в ячейке таблицы GridBagLayout - Java
Формулировка задачи:
Подскажите , непонимаю почему , меня неслушается менеджер расмещения хочу подвинуть наверх таблицу в самый верх она при полном экране сползает, неподскажите в чём дело?
JFrame mainwindow = new JFrame("Пробное окно");
mainwindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagConstraints c = new GridBagConstraints();
mainwindow.setLayout(new GridBagLayout());
mainwindow.setLocation(0,0);
mainwindow.pack();
mainwindow.setVisible(true);
JPanel mypanel = new JPanel();
JTable table = new JTable(data, columnNames);
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
table.addMouseListener(new Newaction());
JScrollPane scrollPane = new JScrollPane(table);
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.NORTH;
c.weightx = 1;
c.gridwidth=2;
c.gridx = 0;
c.gridy = 0;
mainwindow.getContentPane().add(scrollPane,c);
JButton buttom= new JButton("Button 2");
c.gridx = 0;
c.gridy = 1;
c.anchor = GridBagConstraints.SOUTH;
c.gridwidth = 1;
c.fill=GridBagConstraints.VERTICAL ;
mainwindow.getContentPane().add(buttom, c);
JButton buttom1= new JButton("Кнопка выбора 1");
c.anchor = GridBagConstraints.NORTH;
c.gridx = 1;
c.gridy = 1;
c.fill=GridBagConstraints.VERTICAL ;
c.gridwidth = 1;
mainwindow.getContentPane().add(buttom1 ,c);
mainwindow.pack();Решение задачи: «Расположение элементов в ячейке таблицы GridBagLayout»
textual
Листинг программы
package inter1;
/**
*
* @author user
*/
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class Inter1 extends JFrame {
private JTextField search= new JTextField();
private JList listView = new JList();
public Inter1() {
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{0.0, 1.0, 0.0, Double.MIN_VALUE};
setLayout(gridBagLayout);
JPanel infoPanel = new JPanel();
infoPanel.setBorder(BorderFactory.createTitledBorder("Информация"));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridheight = 2;
gbc.gridwidth = 4;
gbc.insets = new Insets(0, 0, 5, 5);
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
add(infoPanel, gbc);
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 4;
gbc.gridy = 0;
add(this.search, gbc);
JButton button = new JButton("find");
gbc.gridx = 5;
gbc.gridy = 0;
add(button, gbc);
button = new JButton("1");
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.insets = new Insets(0, 0, 0, 5);
gbc.gridx = 0;
gbc.gridy = 2;
add(button, gbc);
button = new JButton("2");
gbc.gridx = 1;
add(button, gbc);
button = new JButton("3");
gbc.gridx = 2;
add(button, gbc);
button = new JButton("4");
gbc.gridx = 3;
add(button, gbc);
JScrollPane scrollPane = new JScrollPane(this.listView);
gbc.gridwidth = 2;
gbc.gridheight = 2;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 4;
gbc.gridy = 1;
add(scrollPane, gbc);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Inter1 f = new Inter1();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.pack();
f.setVisible(true);
}
});
}
}
}