Размеры фрейма - Java

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

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

вопрос такой, запускается программа , есть внутренний фрейм , его можно перемещать и увеличивать и уменьшать , с перемещением я разобрался а вот с размерами не получается , метод getSize() дает мне начальный размер фрейма(тот который настроен при создании), подскажите как можно узнать размер измененного фрейма

Решение задачи: «Размеры фрейма»

textual
Листинг программы
  1. package launch;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.MouseEvent;
  5. import java.awt.event.MouseListener;
  6. import javax.swing.*;
  7. import javax.swing.border.EmptyBorder;
  8.  
  9. import GUI.AboutFrame;
  10. import GUI.GalleryFrame;
  11. import GUI.InfoFrame;
  12. import GUI.OptMenuFrame;
  13. import base.Library;
  14. import tuning.LogTracker;
  15. import tuning.Params;
  16. import tuning.UserManager;
  17.  
  18.  
  19. public class MainMenu extends JFrame
  20. {
  21.     private static final long serialVersionUID = 1L;
  22.    
  23.     JPanel downTextedPanel;
  24.     JPanel Opisanie;
  25.    
  26.     JPanel buttonsPanel;
  27.     JPanel exitPanel;
  28.    
  29.     JPanel leftPanelBase;
  30.     JPanel rightPanelBase;
  31.    
  32.     JPanel picturePane;
  33.    
  34.     JPanel playButtonPanel;
  35.    
  36.    
  37.     JLabel downText;
  38.    
  39.     public static int i = 0;
  40.    
  41.     Container basePictureCont;
  42.     Container playButtonCont;
  43.    
  44.     static JButton startGameButton;
  45.     static JButton optionsButton;
  46.     static JButton theGalleryButton;
  47.     static JButton autorsOfgameButton;
  48.     static JButton exitGameButton;
  49.     static JButton aboutUsButton;
  50.    
  51.     JPanel graphicPicture = new graphicPanels.GraphicFrame();
  52.     JPanel graphicShellmenu = new graphicPanels.GraphicMainMenu();
  53.    
  54.     public static MouseListener soundPlayer;
  55.    
  56.     GridBagLayout GBL = new GridBagLayout();
  57.    
  58.     public MainMenu()
  59.     {
  60.         {
  61.             setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  62.             setUndecorated(true);
  63.             setBackground(Library.opacityBG);
  64.             setResizable(false);
  65.            
  66. //          if (!(Toolkit.getDefaultToolkit().getScreenSize().getWidth() == 1920)&&(Toolkit.getDefaultToolkit().getScreenSize().getHeight() == 1080))
  67. //          {
  68. //              JOptionPane.showMessageDialog(
  69. //                      null,
  70. //                      "Не поддерживаемое разрешение экрана!",
  71. //                      "ВНИМАНИЕ",
  72. //                      JOptionPane.YES_OPTION);
  73. //              LogTracker.tracking("Подходящее разрешение экрана не найдено!", "Главное меню: Подходящее разрешение экрана не найдено! Игра будет закрыта.");
  74. //              System.exit(12);
  75. //          }
  76. //          else
  77. //          {
  78.                 setPreferredSize(Params.sizeFullScreen);
  79. //          }
  80.            
  81.             graphicPicture.setFocusable(false);
  82.            
  83.                         soundPlayer = new MouseListener()
  84.                         {
  85.                             @Override
  86.                             public void mouseClicked(MouseEvent e)
  87.                             {
  88.                                 base.Media.mediaFiles2("buttonOver");
  89.                             }
  90.        
  91.                             @Override
  92.                             public void mouseEntered(MouseEvent e)
  93.                             {
  94.                                 base.Media.mediaFiles2("buttonOver");
  95.                             }
  96.        
  97.                             @Override
  98.                             public void mouseExited(MouseEvent e)
  99.                             {
  100.                                 base.Media.mediaFiles2("buttonOver");
  101.                             }
  102.        
  103.                             @Override
  104.                             public void mousePressed(MouseEvent e)
  105.                             {
  106.        
  107.                             }
  108.        
  109.                             @Override
  110.                             public void mouseReleased(MouseEvent e)
  111.                             {
  112.                                 base.Media.mediaFiles2("confirm");
  113.                             }                      
  114.                         };
  115.                        
  116.                         if (Params.gamesCur == 1)
  117.                         {
  118.                             this.setCursor(Library.cursor);
  119.                         }
  120.                            
  121.                         downTextedPanel = new JPanel();
  122.                         downTextedPanel.setBackground(Library.opacityBG);
  123.                         {
  124.                             downText = new JLabel("\u266B " + UserManager.userName + " \u266B");
  125.                             downText.setFont(Library.font7);
  126.                             downText.setForeground(Library.lightOrange);
  127.                             //downText.setBorder(new EmptyBorder(0, 0, 10, 0));
  128.                                 downTextedPanel.add(downText, BorderLayout.NORTH);
  129.                         }
  130.                                
  131.                                
  132.                         startGameButton = new JButton("Играть");
  133.                         startGameButton.setIcon(Library.i1);
  134.                         startGameButton.setPressedIcon(Library.i1);
  135.                         startGameButton.setRolloverIcon(Library.i1press);
  136.                         startGameButton.setFont(Library.font4);
  137.                         startGameButton.setToolTipText("Начать новую игру!");
  138.                         //startGameButton.setBorder();
  139.                         startGameButton.setFocusPainted(false);
  140.                         startGameButton.addMouseListener(soundPlayer);
  141.                         startGameButton.setOpaque(false);
  142.                         startGameButton.addActionListener(e ->
  143.                         {
  144.                             try
  145.                             {
  146.                                 Thread.sleep(1000);
  147.                             }
  148.                             catch (Exception e1)
  149.                             {
  150.                                 e1.printStackTrace();
  151.                             }
  152.                            
  153.                             FrameManager.showFrame("NewGame");
  154.                             MainMenu.this.dispose();
  155.                         });
  156.  
  157.                                        
  158.                         optionsButton = new JButton("Настройки");
  159. //                      optionsButton.setIcon(Library.i4);
  160. //                      optionsButton.setPressedIcon(Library.i4);
  161. //                      optionsButton.setRolloverIcon(Library.i4press);
  162.                         optionsButton.setFont(Library.font2);
  163.                         optionsButton.setToolTipText("Настройки игры ");
  164.                         //optionsButton.setBorder(Library.compoundTitle2);
  165.                         optionsButton.setFocusPainted(false);
  166.                         optionsButton.addMouseListener(soundPlayer);
  167.                         optionsButton.addActionListener(e ->
  168.                             {
  169.                                 new OptMenuFrame();
  170.                                 MainMenu.this.dispose();
  171.                             });
  172.                                        
  173.                                        
  174.                                        
  175.                         theGalleryButton = new JButton("Галлерея");
  176. //                      theGalleryButton.setIcon(Library.i5);
  177. //                      theGalleryButton.setPressedIcon(Library.i5);
  178. //                      theGalleryButton.setRolloverIcon(Library.i5press);
  179.                         theGalleryButton.setFont(Library.font2);
  180.                         theGalleryButton.setToolTipText("Галлерея медиа");
  181.                         //theGalleryButton.setBorder(Library.buttonBorder);
  182.                         theGalleryButton.setFocusPainted(false);
  183.                         theGalleryButton.addMouseListener(soundPlayer);
  184.                         theGalleryButton.addActionListener(e ->
  185.                             {
  186.                                 new GalleryFrame();
  187.                                 MainMenu.this.setVisible(false);
  188.                             });
  189.                            
  190.  
  191.                         autorsOfgameButton = new JButton("Авторы");
  192. //                      autorsOfgameButton.setIcon(Library.i6);
  193. //                      autorsOfgameButton.setPressedIcon(Library.i6);
  194. //                      autorsOfgameButton.setRolloverIcon(Library.i6press);
  195.                         autorsOfgameButton.setFont(Library.font2);
  196.                         autorsOfgameButton.setToolTipText("Все, кто участвовал в создании игры");
  197.                         //autorsOfgameButton.setBorder(Library.buttonBorder);
  198.                         autorsOfgameButton.setFocusPainted(false);
  199.                         autorsOfgameButton.addMouseListener(soundPlayer);
  200.                         autorsOfgameButton.addActionListener(e ->
  201.                             {
  202.                                 new AboutFrame();
  203.                                 MainMenu.this.setVisible(false);
  204.                             });
  205.                            
  206.                            
  207.                            
  208.                         aboutUsButton = new JButton("Об игре");
  209. //                      aboutUsButton.setIcon(Library.i7);
  210. //                      aboutUsButton.setPressedIcon(Library.i7);
  211. //                      aboutUsButton.setRolloverIcon(Library.i7press);
  212.                         aboutUsButton.setFont(Library.font2);
  213.                         aboutUsButton.setToolTipText("Про проект");
  214.                         //aboutUsButton.setBorder(Library.buttonBorder);
  215.                         aboutUsButton.setFocusPainted(false);
  216.                         aboutUsButton.addMouseListener(soundPlayer);
  217.                         aboutUsButton.addActionListener(e ->
  218.                             {
  219.                                 new InfoFrame();
  220.                                 MainMenu.this.setVisible(false);
  221.                             });
  222.                                
  223.                                        
  224.                                 exitGameButton = new JButton("Выход");
  225. //                              exitGameButton.setIcon(Library.i8);
  226. //                              exitGameButton.setPressedIcon(Library.i8);
  227. //                              exitGameButton.setRolloverIcon(Library.i8press);
  228.                                 exitGameButton.setFont(Library.font2);
  229.                                 exitGameButton.setToolTipText("Досвидания!");
  230. //                              exitGameButton.setBorder(Library.buttonBorder);
  231.                                 exitGameButton.setFocusPainted(false);
  232.                                 exitGameButton.addMouseListener(soundPlayer);
  233.                                 exitGameButton.addActionListener(e ->
  234.                                     {
  235.                                         LogTracker.tracking("Завершение работы программы.", "Завершение работы программы.");
  236.                                         System.exit(0);
  237.                                     });
  238.                                
  239.                        
  240.                         int X1 = Toolkit.getDefaultToolkit().getScreenSize().width/1000;
  241.                         int Y1 = Toolkit.getDefaultToolkit().getScreenSize().height/1000;  
  242.                                        
  243.                         graphicShellmenu.setLayout(GBL);
  244.                         graphicShellmenu.setBorder(new EmptyBorder(Y1*35,X1*50,Y1*35,X1*50));
  245.                        
  246.                         GridBagConstraints c = new GridBagConstraints();
  247.                         c.gridx = 0;
  248.                         c.gridy = 0;
  249.                         c.gridwidth = 3;
  250.                         c.gridheight = 1;
  251.                         c.weightx = 0;
  252.                         c.weighty = 0;
  253.                         c.anchor = GridBagConstraints.NORTHWEST;
  254.                         c.fill = GridBagConstraints.BOTH;
  255.                         c.insets = new Insets(0,0,Y1*30,0);
  256.                         c.ipadx = 0;
  257.                         c.ipady = 0;
  258.  
  259.                         graphicShellmenu.add(startGameButton, c);
  260.  
  261.                        
  262.                        
  263.                         c.gridx = 0;
  264.                         c.gridy = 1;
  265.                         c.gridwidth = 1;
  266.                         c.gridheight = 8;
  267.                         c.weightx = 109;
  268.                         c.weighty = 0;
  269.                         c.anchor = GridBagConstraints.NORTHWEST;
  270.                         c.fill = GridBagConstraints.BOTH;
  271.                         c.insets = new Insets(Y1*5, 0, Y1*10, X1*40);
  272.                         c.ipadx = 0;
  273.                         c.ipady = 0;
  274.                        
  275.                         graphicShellmenu.add(graphicPicture, c);
  276.  
  277.                        
  278.                        
  279.                         c.gridx = 1;
  280.                         c.gridy = 1;
  281.                         c.gridwidth = 2;
  282.                         c.gridheight = 2;
  283.                         c.weightx = 40;
  284.                         c.weighty = 1;
  285.                         c.anchor = GridBagConstraints.NORTHWEST;
  286.                         c.fill = GridBagConstraints.BOTH;
  287.                         c.insets = new Insets(0,X1*20,0,0);
  288.                         c.ipadx = 0;
  289.                         c.ipady = 0;
  290.                        
  291.                         graphicShellmenu.add(optionsButton, c);
  292.                        
  293.                        
  294.                         c.gridx = 1;
  295.                         c.gridy = 3;
  296.                         c.gridwidth = 2;
  297.                         c.gridheight = 2;
  298.                         c.weightx = 0;
  299.                         c.weighty = 1;
  300.                         c.anchor = GridBagConstraints.NORTHWEST;
  301.                         c.fill = GridBagConstraints.BOTH;
  302.                         c.insets = new Insets(0,X1*20,0,0);
  303.                         c.ipadx = 0;
  304.                         c.ipady = 0;
  305.                        
  306.                         graphicShellmenu.add(theGalleryButton, c);
  307.                        
  308.                        
  309.                         c.gridx = 1;
  310.                         c.gridy = 5;
  311.                         c.gridwidth = 2;
  312.                         c.gridheight = 2;
  313.                         c.weightx = 0;
  314.                         c.weighty = 1;
  315.                         c.anchor = GridBagConstraints.NORTHWEST;
  316.                         c.fill = GridBagConstraints.BOTH;
  317.                         c.insets = new Insets(0,X1*20,0,0);
  318.                         c.ipadx = 0;
  319.                         c.ipady = 0;
  320.                        
  321.                         graphicShellmenu.add(autorsOfgameButton, c);
  322.                        
  323.                        
  324.                         c.gridx = 1;
  325.                         c.gridy = 7;
  326.                         c.gridwidth = 2;
  327.                         c.gridheight = 2;
  328.                         c.weightx = 0;
  329.                         c.weighty = 1;
  330.                         c.anchor = GridBagConstraints.NORTHWEST;
  331.                         c.fill = GridBagConstraints.BOTH;
  332.                         c.insets = new Insets(0,X1*20,Y1*15,0);
  333.                         c.ipadx = 0;
  334.                         c.ipady = 0;
  335.                        
  336.                         graphicShellmenu.add(aboutUsButton, c);
  337.  
  338.                        
  339.                        
  340.                         c.gridx = 1;
  341.                         c.gridy = 9;
  342.                         c.gridwidth = 2;
  343.                         c.gridheight = 1;
  344.                         c.weightx = 0;
  345.                         c.weighty = 0;
  346.                         c.anchor = GridBagConstraints.CENTER;
  347.                         c.fill = GridBagConstraints.BOTH;
  348.                         c.insets = new Insets(Y1*20, X1*20, Y1*5, 0);
  349.                         c.ipadx = 0;
  350.                         c.ipady = -20;
  351.                        
  352.                         graphicShellmenu.add(exitGameButton, c);
  353.                        
  354.                        
  355.                         c.gridx = 0;
  356.                         c.gridy = 9;
  357.                         c.gridwidth = 2;
  358.                         c.gridheight = 1;
  359.                         c.weightx = 0;
  360.                         c.weighty = 0;
  361.                         c.anchor = GridBagConstraints.NORTH;
  362.                         c.fill = GridBagConstraints.HORIZONTAL;
  363.                         c.insets = new Insets(Y1*20, 0 , Y1*10,0);
  364.                         c.ipadx = 0;
  365.                         c.ipady = 0;
  366.                        
  367.                         graphicShellmenu.add(downTextedPanel, c);
  368.                        
  369.                        
  370.                 add(graphicShellmenu);
  371.                 pack();
  372.                 setLocationRelativeTo(null);
  373.          }
  374.     }
  375. }

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


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

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

10   голосов , оценка 4 из 5

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

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

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