Расположение элементов в ячейке таблицы GridBagLayout - Java

Узнай цену своей работы

Формулировка задачи:

Подскажите , непонимаю почему , меня неслушается менеджер расмещения хочу подвинуть наверх таблицу в самый верх она при полном экране сползает, неподскажите в чём дело?
Листинг программы
  1. JFrame mainwindow = new JFrame("Пробное окно");
  2. mainwindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  3. GridBagConstraints c = new GridBagConstraints();
  4. mainwindow.setLayout(new GridBagLayout());
  5. mainwindow.setLocation(0,0);
  6. mainwindow.pack();
  7. mainwindow.setVisible(true);
  8. JPanel mypanel = new JPanel();
  9. JTable table = new JTable(data, columnNames);
  10. table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
  11. table.addMouseListener(new Newaction());
  12. JScrollPane scrollPane = new JScrollPane(table);
  13. c.fill = GridBagConstraints.BOTH;
  14. c.anchor = GridBagConstraints.NORTH;
  15. c.weightx = 1;
  16. c.gridwidth=2;
  17. c.gridx = 0;
  18. c.gridy = 0;
  19. mainwindow.getContentPane().add(scrollPane,c);
  20. JButton buttom= new JButton("Button 2");
  21. c.gridx = 0;
  22. c.gridy = 1;
  23. c.anchor = GridBagConstraints.SOUTH;
  24. c.gridwidth = 1;
  25. c.fill=GridBagConstraints.VERTICAL ;
  26. mainwindow.getContentPane().add(buttom, c);
  27. JButton buttom1= new JButton("Кнопка выбора 1");
  28. c.anchor = GridBagConstraints.NORTH;
  29. c.gridx = 1;
  30. c.gridy = 1;
  31. c.fill=GridBagConstraints.VERTICAL ;
  32. c.gridwidth = 1;
  33.  
  34. mainwindow.getContentPane().add(buttom1 ,c);
  35. mainwindow.pack();

Решение задачи: «Расположение элементов в ячейке таблицы GridBagLayout»

textual
Листинг программы
  1. package inter1;
  2.  
  3. /**
  4.  *
  5.  * @author user
  6.  */
  7.  
  8.  
  9. import java.awt.GridBagConstraints;
  10. import java.awt.GridBagLayout;
  11. import java.awt.Insets;
  12.  
  13. import javax.swing.BorderFactory;
  14. import javax.swing.JButton;
  15. import javax.swing.JFrame;
  16. import javax.swing.JList;
  17. import javax.swing.JPanel;
  18. import javax.swing.JScrollPane;
  19. import javax.swing.JTextField;
  20. import javax.swing.SwingUtilities;
  21.  
  22. public class  Inter1 extends JFrame {
  23.     private JTextField search= new JTextField();
  24.     private JList listView = new JList();
  25.  
  26.     public  Inter1() {
  27.         GridBagLayout gridBagLayout = new GridBagLayout();
  28.         gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
  29.         gridBagLayout.rowWeights = new double[]{0.0, 1.0, 0.0, Double.MIN_VALUE};
  30.         setLayout(gridBagLayout);
  31.  
  32.         JPanel infoPanel = new JPanel();
  33.         infoPanel.setBorder(BorderFactory.createTitledBorder("Информация"));
  34.         GridBagConstraints gbc = new GridBagConstraints();
  35.         gbc.gridheight = 2;
  36.         gbc.gridwidth = 4;
  37.         gbc.insets = new Insets(0, 0, 5, 5);
  38.         gbc.fill = GridBagConstraints.BOTH;
  39.         gbc.gridx = 0;
  40.         gbc.gridy = 0;
  41.         add(infoPanel, gbc);
  42.  
  43.         gbc.gridheight = 1;
  44.         gbc.gridwidth = 1;
  45.         gbc.fill = GridBagConstraints.HORIZONTAL;
  46.         gbc.gridx = 4;
  47.         gbc.gridy = 0;
  48.         add(this.search, gbc);
  49.  
  50.         JButton button = new JButton("find");
  51.         gbc.gridx = 5;
  52.         gbc.gridy = 0;
  53.         add(button, gbc);
  54.  
  55.        
  56.  
  57.         button = new JButton("1");
  58.         gbc.gridwidth = 1;
  59.         gbc.gridheight = 1;
  60.         gbc.insets = new Insets(0, 0, 0, 5);
  61.         gbc.gridx = 0;
  62.         gbc.gridy = 2;
  63.         add(button, gbc);
  64.  
  65.         button = new JButton("2");
  66.         gbc.gridx = 1;
  67.         add(button, gbc);
  68.  
  69.         button = new JButton("3");
  70.         gbc.gridx = 2;
  71.         add(button, gbc);
  72.  
  73.         button = new JButton("4");
  74.         gbc.gridx = 3;
  75.         add(button, gbc);
  76.        
  77.        
  78.         JScrollPane scrollPane = new JScrollPane(this.listView);
  79.         gbc.gridwidth = 2;
  80.         gbc.gridheight = 2;
  81.         gbc.fill = GridBagConstraints.BOTH;
  82.         gbc.gridx = 4;
  83.         gbc.gridy = 1;
  84.         add(scrollPane, gbc);
  85.     }
  86.  
  87.     public static void main(String[] args) {
  88.         SwingUtilities.invokeLater(new Runnable() {
  89.  
  90.             @Override
  91.             public void run() {
  92.                  Inter1 f = new  Inter1();
  93.                 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  94.                 f.setLocationRelativeTo(null);
  95.                 f.pack();
  96.                 f.setVisible(true);
  97.             }
  98.  
  99.         });
  100.     }
  101. }
  102.    
  103. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

15   голосов , оценка 3.733 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут