It simply creates a JFrame window. How to make it so that there is no standard "maximize" button?

  • Standard frame.setResizable(false); will not work? Or just need to hide? - Alexey Shimansky
  • it is necessary to hide it, i.e. so that this button is not visible at all - oleg
  • If you have been given the correct answer - vote for it \ check it correct (gray tick on the side of the answer). This will help others understand that the answer came up to solve the problem. - Alexey Shimansky

1 answer 1

Something no special information. There is an option only to kill the default scenery setUndecorated(true); and then add your custom panel, buttons again and hang up handlers on these buttons.

Here is the code, I tried to explain with comments, as far as I could. It seems everything is clear.

 import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; /** * Created by iprogrammer on 13.06.2016. */ public class Main extends JFrame { JPanel p; JMenuBar mb; JButton close, min; int pX,pY; public Main() { createAndShowGUI(); } private void createAndShowGUI() { // Custom look and feel try { UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel"); }catch(Exception e){ } setDefaultCloseOperation(EXIT_ON_CLOSE); // Π£Π±ΠΈΠ²Π°Π΅ΠΌ Π΄Π΅Ρ„ΠΎΠ»Ρ‚Π½ΡƒΡŽ Π΄Π΅ΠΊΠΎΡ€Π°Ρ†ΠΈΡŽ (Ρ‚.Π΅. ΠΊΠ½ΠΎΠΏΠΊΠΈ) setUndecorated(true); // Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ ПанСль свСрху mb = new JMenuBar(); mb.setLayout(new BorderLayout()); // Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ панСль p = new JPanel(); p.setOpaque(false); p.setLayout(new GridLayout(1,2)); // Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ ΠΊΠ½ΠΎΠΏΠΊΠΈ close = new JButton("x"); min = new JButton("-"); // ΠžΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ ΠΊΠ½ΠΎΠΏΠΊΠΈ ΠΌΠΈΠ½ΠΈΠΌΠΈΠ·Π°Ρ†ΠΈΠΈ min.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae) { setState(ICONIFIED); // ΠœΠΈΠ½ΠΈΠΌΠΈΠ·Π°Ρ†ΠΈΡ ΠΎΠΊΠ½Π° } }); // ΠžΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ ΠΊΠ½ΠΎΠΏΠΊΠΈ закрытия close.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae) { System.exit(0); // Π·Π°ΠΊΡ€Ρ‹Ρ‚ΡŒ ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΡƒ } }); min.setFocusPainted(false); close.setFocusPainted(false); // ДобавляСм ΠΊΠ½ΠΎΠΏΠΊΠΈ p.add(min); p.add(close); // УстанавливаСм ΠΊΠ½ΠΎΠΏΠΊΠΈ справа mb.add(p, BorderLayout.EAST); setJMenuBar(mb); /******** ЭВО Π§Π’ΠžΠ‘ ΠŸΠ•Π Π•Π’Π―Π“Π˜Π’ΠΠ’Π¬ ОКНО (ΠΠΠ§ΠΠ›Πž) ************/ // Add mouse listener for JMenuBar mb mb.addMouseListener(new MouseAdapter(){ public void mousePressed(MouseEvent me) { // Π±Π΅Ρ€Π΅ΠΌ ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹ Ρ‚Ρ‹ΠΊΠ° ΠΈ сохраняСм ΠΈΡ… pX=me.getX(); pY=me.getY(); } }); // УстанавливаСм MouseMotionListener Ρ‡Ρ‚ΠΎΠ± ΠΏΠΎΠΉΠΌΠ°Ρ‚ΡŒ пСрСтягиваниС mb.addMouseMotionListener(new MouseAdapter(){ public void mouseDragged(MouseEvent me) { setLocation(getLocation().x+me.getX()-pX,getLocation().y+me.getY()-pY); } }); /******** ЭВО Π§Π’ΠžΠ‘ ΠŸΠ•Π Π•Π’Π―Π“Π˜Π’ΠΠ’Π¬ ОКНО (ΠšΠžΠΠ•Π¦) ************/ // это просто надпись final JLabel label = new JLabel("Hello World", SwingConstants.CENTER); label.setFont(label.getFont().deriveFont(40.0f)); getContentPane().add(label); // УстанавливаСм Ρ€Π°Π·ΠΌΠ΅Ρ€ setSize(400,400); setVisible(true); //setShape(new java.awt.geom.RoundRectangle2D.Double(0,0,getWidth(),getHeight(),5,5)); setLocationRelativeTo(null); } public static void main(String args[]) { new Main(); } } 

It turns out something like this:

enter image description here

The rest, I think, you can finish yourself.