Размеры фрейма - Java
Формулировка задачи:
вопрос такой, запускается программа , есть внутренний фрейм , его можно перемещать и увеличивать и уменьшать , с перемещением я разобрался а вот с размерами не получается , метод getSize() дает мне начальный размер фрейма(тот который настроен при создании), подскажите как можно узнать размер измененного фрейма
Решение задачи: «Размеры фрейма»
textual
Листинг программы
- package launch;
- import java.awt.*;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import javax.swing.*;
- import javax.swing.border.EmptyBorder;
- import GUI.AboutFrame;
- import GUI.GalleryFrame;
- import GUI.InfoFrame;
- import GUI.OptMenuFrame;
- import base.Library;
- import tuning.LogTracker;
- import tuning.Params;
- import tuning.UserManager;
- public class MainMenu extends JFrame
- {
- private static final long serialVersionUID = 1L;
- JPanel downTextedPanel;
- JPanel Opisanie;
- JPanel buttonsPanel;
- JPanel exitPanel;
- JPanel leftPanelBase;
- JPanel rightPanelBase;
- JPanel picturePane;
- JPanel playButtonPanel;
- JLabel downText;
- public static int i = 0;
- Container basePictureCont;
- Container playButtonCont;
- static JButton startGameButton;
- static JButton optionsButton;
- static JButton theGalleryButton;
- static JButton autorsOfgameButton;
- static JButton exitGameButton;
- static JButton aboutUsButton;
- JPanel graphicPicture = new graphicPanels.GraphicFrame();
- JPanel graphicShellmenu = new graphicPanels.GraphicMainMenu();
- public static MouseListener soundPlayer;
- GridBagLayout GBL = new GridBagLayout();
- public MainMenu()
- {
- {
- setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
- setUndecorated(true);
- setBackground(Library.opacityBG);
- setResizable(false);
- // if (!(Toolkit.getDefaultToolkit().getScreenSize().getWidth() == 1920)&&(Toolkit.getDefaultToolkit().getScreenSize().getHeight() == 1080))
- // {
- // JOptionPane.showMessageDialog(
- // null,
- // "Не поддерживаемое разрешение экрана!",
- // "ВНИМАНИЕ",
- // JOptionPane.YES_OPTION);
- // LogTracker.tracking("Подходящее разрешение экрана не найдено!", "Главное меню: Подходящее разрешение экрана не найдено! Игра будет закрыта.");
- // System.exit(12);
- // }
- // else
- // {
- setPreferredSize(Params.sizeFullScreen);
- // }
- graphicPicture.setFocusable(false);
- soundPlayer = new MouseListener()
- {
- @Override
- public void mouseClicked(MouseEvent e)
- {
- base.Media.mediaFiles2("buttonOver");
- }
- @Override
- public void mouseEntered(MouseEvent e)
- {
- base.Media.mediaFiles2("buttonOver");
- }
- @Override
- public void mouseExited(MouseEvent e)
- {
- base.Media.mediaFiles2("buttonOver");
- }
- @Override
- public void mousePressed(MouseEvent e)
- {
- }
- @Override
- public void mouseReleased(MouseEvent e)
- {
- base.Media.mediaFiles2("confirm");
- }
- };
- if (Params.gamesCur == 1)
- {
- this.setCursor(Library.cursor);
- }
- downTextedPanel = new JPanel();
- downTextedPanel.setBackground(Library.opacityBG);
- {
- downText = new JLabel("\u266B " + UserManager.userName + " \u266B");
- downText.setFont(Library.font7);
- downText.setForeground(Library.lightOrange);
- //downText.setBorder(new EmptyBorder(0, 0, 10, 0));
- downTextedPanel.add(downText, BorderLayout.NORTH);
- }
- startGameButton = new JButton("Играть");
- startGameButton.setIcon(Library.i1);
- startGameButton.setPressedIcon(Library.i1);
- startGameButton.setRolloverIcon(Library.i1press);
- startGameButton.setFont(Library.font4);
- startGameButton.setToolTipText("Начать новую игру!");
- //startGameButton.setBorder();
- startGameButton.setFocusPainted(false);
- startGameButton.addMouseListener(soundPlayer);
- startGameButton.setOpaque(false);
- startGameButton.addActionListener(e ->
- {
- try
- {
- Thread.sleep(1000);
- }
- catch (Exception e1)
- {
- e1.printStackTrace();
- }
- FrameManager.showFrame("NewGame");
- MainMenu.this.dispose();
- });
- optionsButton = new JButton("Настройки");
- // optionsButton.setIcon(Library.i4);
- // optionsButton.setPressedIcon(Library.i4);
- // optionsButton.setRolloverIcon(Library.i4press);
- optionsButton.setFont(Library.font2);
- optionsButton.setToolTipText("Настройки игры ");
- //optionsButton.setBorder(Library.compoundTitle2);
- optionsButton.setFocusPainted(false);
- optionsButton.addMouseListener(soundPlayer);
- optionsButton.addActionListener(e ->
- {
- new OptMenuFrame();
- MainMenu.this.dispose();
- });
- theGalleryButton = new JButton("Галлерея");
- // theGalleryButton.setIcon(Library.i5);
- // theGalleryButton.setPressedIcon(Library.i5);
- // theGalleryButton.setRolloverIcon(Library.i5press);
- theGalleryButton.setFont(Library.font2);
- theGalleryButton.setToolTipText("Галлерея медиа");
- //theGalleryButton.setBorder(Library.buttonBorder);
- theGalleryButton.setFocusPainted(false);
- theGalleryButton.addMouseListener(soundPlayer);
- theGalleryButton.addActionListener(e ->
- {
- new GalleryFrame();
- MainMenu.this.setVisible(false);
- });
- autorsOfgameButton = new JButton("Авторы");
- // autorsOfgameButton.setIcon(Library.i6);
- // autorsOfgameButton.setPressedIcon(Library.i6);
- // autorsOfgameButton.setRolloverIcon(Library.i6press);
- autorsOfgameButton.setFont(Library.font2);
- autorsOfgameButton.setToolTipText("Все, кто участвовал в создании игры");
- //autorsOfgameButton.setBorder(Library.buttonBorder);
- autorsOfgameButton.setFocusPainted(false);
- autorsOfgameButton.addMouseListener(soundPlayer);
- autorsOfgameButton.addActionListener(e ->
- {
- new AboutFrame();
- MainMenu.this.setVisible(false);
- });
- aboutUsButton = new JButton("Об игре");
- // aboutUsButton.setIcon(Library.i7);
- // aboutUsButton.setPressedIcon(Library.i7);
- // aboutUsButton.setRolloverIcon(Library.i7press);
- aboutUsButton.setFont(Library.font2);
- aboutUsButton.setToolTipText("Про проект");
- //aboutUsButton.setBorder(Library.buttonBorder);
- aboutUsButton.setFocusPainted(false);
- aboutUsButton.addMouseListener(soundPlayer);
- aboutUsButton.addActionListener(e ->
- {
- new InfoFrame();
- MainMenu.this.setVisible(false);
- });
- exitGameButton = new JButton("Выход");
- // exitGameButton.setIcon(Library.i8);
- // exitGameButton.setPressedIcon(Library.i8);
- // exitGameButton.setRolloverIcon(Library.i8press);
- exitGameButton.setFont(Library.font2);
- exitGameButton.setToolTipText("Досвидания!");
- // exitGameButton.setBorder(Library.buttonBorder);
- exitGameButton.setFocusPainted(false);
- exitGameButton.addMouseListener(soundPlayer);
- exitGameButton.addActionListener(e ->
- {
- LogTracker.tracking("Завершение работы программы.", "Завершение работы программы.");
- System.exit(0);
- });
- int X1 = Toolkit.getDefaultToolkit().getScreenSize().width/1000;
- int Y1 = Toolkit.getDefaultToolkit().getScreenSize().height/1000;
- graphicShellmenu.setLayout(GBL);
- graphicShellmenu.setBorder(new EmptyBorder(Y1*35,X1*50,Y1*35,X1*50));
- GridBagConstraints c = new GridBagConstraints();
- c.gridx = 0;
- c.gridy = 0;
- c.gridwidth = 3;
- c.gridheight = 1;
- c.weightx = 0;
- c.weighty = 0;
- c.anchor = GridBagConstraints.NORTHWEST;
- c.fill = GridBagConstraints.BOTH;
- c.insets = new Insets(0,0,Y1*30,0);
- c.ipadx = 0;
- c.ipady = 0;
- graphicShellmenu.add(startGameButton, c);
- c.gridx = 0;
- c.gridy = 1;
- c.gridwidth = 1;
- c.gridheight = 8;
- c.weightx = 109;
- c.weighty = 0;
- c.anchor = GridBagConstraints.NORTHWEST;
- c.fill = GridBagConstraints.BOTH;
- c.insets = new Insets(Y1*5, 0, Y1*10, X1*40);
- c.ipadx = 0;
- c.ipady = 0;
- graphicShellmenu.add(graphicPicture, c);
- c.gridx = 1;
- c.gridy = 1;
- c.gridwidth = 2;
- c.gridheight = 2;
- c.weightx = 40;
- c.weighty = 1;
- c.anchor = GridBagConstraints.NORTHWEST;
- c.fill = GridBagConstraints.BOTH;
- c.insets = new Insets(0,X1*20,0,0);
- c.ipadx = 0;
- c.ipady = 0;
- graphicShellmenu.add(optionsButton, c);
- c.gridx = 1;
- c.gridy = 3;
- c.gridwidth = 2;
- c.gridheight = 2;
- c.weightx = 0;
- c.weighty = 1;
- c.anchor = GridBagConstraints.NORTHWEST;
- c.fill = GridBagConstraints.BOTH;
- c.insets = new Insets(0,X1*20,0,0);
- c.ipadx = 0;
- c.ipady = 0;
- graphicShellmenu.add(theGalleryButton, c);
- c.gridx = 1;
- c.gridy = 5;
- c.gridwidth = 2;
- c.gridheight = 2;
- c.weightx = 0;
- c.weighty = 1;
- c.anchor = GridBagConstraints.NORTHWEST;
- c.fill = GridBagConstraints.BOTH;
- c.insets = new Insets(0,X1*20,0,0);
- c.ipadx = 0;
- c.ipady = 0;
- graphicShellmenu.add(autorsOfgameButton, c);
- c.gridx = 1;
- c.gridy = 7;
- c.gridwidth = 2;
- c.gridheight = 2;
- c.weightx = 0;
- c.weighty = 1;
- c.anchor = GridBagConstraints.NORTHWEST;
- c.fill = GridBagConstraints.BOTH;
- c.insets = new Insets(0,X1*20,Y1*15,0);
- c.ipadx = 0;
- c.ipady = 0;
- graphicShellmenu.add(aboutUsButton, c);
- c.gridx = 1;
- c.gridy = 9;
- c.gridwidth = 2;
- c.gridheight = 1;
- c.weightx = 0;
- c.weighty = 0;
- c.anchor = GridBagConstraints.CENTER;
- c.fill = GridBagConstraints.BOTH;
- c.insets = new Insets(Y1*20, X1*20, Y1*5, 0);
- c.ipadx = 0;
- c.ipady = -20;
- graphicShellmenu.add(exitGameButton, c);
- c.gridx = 0;
- c.gridy = 9;
- c.gridwidth = 2;
- c.gridheight = 1;
- c.weightx = 0;
- c.weighty = 0;
- c.anchor = GridBagConstraints.NORTH;
- c.fill = GridBagConstraints.HORIZONTAL;
- c.insets = new Insets(Y1*20, 0 , Y1*10,0);
- c.ipadx = 0;
- c.ipady = 0;
- graphicShellmenu.add(downTextedPanel, c);
- add(graphicShellmenu);
- pack();
- setLocationRelativeTo(null);
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д