Disable Jframe Maximize Boot

Asked

Viewed 592 times

0

I set the size to 600x600, but I wanted to know how I do so that when I click on the maximize, I don’t run out of the background or even take out the maximize button and just let it minimize and close.

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

code:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package calculadoraimc;

import static javax.swing.JFrame.EXIT_ON_CLOSE;

/**
 *
 * @author SpiriT
 */
public class NewJFrame extends javax.swing.JFrame {

    /**
     * Creates new form NewJFrame
     */
    public NewJFrame() {
        initComponents();
        setSize(600,600);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        TextField_Peso = new javax.swing.JTextField();
        TextField_Altura = new javax.swing.JTextField();
        Peso_Label = new javax.swing.JLabel();
        Altura_Label = new javax.swing.JLabel();
        TextField_Nome = new javax.swing.JTextField();
        Nome_Label = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        JFundo = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Calculadora de IMC");
        setBackground(new java.awt.Color(102, 102, 255));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setMaximumSize(new java.awt.Dimension(600, 600));
        setPreferredSize(new java.awt.Dimension(600, 600));
        getContentPane().setLayout(null);

        jButton1.setBackground(new java.awt.Color(255, 255, 255));
        jButton1.setFont(new java.awt.Font("Arial Black", 0, 24)); // NOI18N
        jButton1.setText("Calcular");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(190, 490, 240, 60);

        TextField_Peso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TextField_PesoActionPerformed(evt);
            }
        });
        getContentPane().add(TextField_Peso);
        TextField_Peso.setBounds(260, 290, 180, 30);
        getContentPane().add(TextField_Altura);
        TextField_Altura.setBounds(260, 330, 180, 30);

        Peso_Label.setFont(new java.awt.Font("Arial Black", 2, 24)); // NOI18N
        Peso_Label.setForeground(new java.awt.Color(255, 255, 255));
        Peso_Label.setText("Peso");
        getContentPane().add(Peso_Label);
        Peso_Label.setBounds(170, 290, 110, 30);

        Altura_Label.setFont(new java.awt.Font("Arial Black", 2, 24)); // NOI18N
        Altura_Label.setForeground(new java.awt.Color(255, 255, 255));
        Altura_Label.setText("Altura");
        getContentPane().add(Altura_Label);
        Altura_Label.setBounds(160, 330, 110, 30);
        getContentPane().add(TextField_Nome);
        TextField_Nome.setBounds(260, 370, 180, 30);

        Nome_Label.setFont(new java.awt.Font("Arial Black", 2, 24)); // NOI18N
        Nome_Label.setForeground(new java.awt.Color(255, 255, 255));
        Nome_Label.setText("Nome");
        getContentPane().add(Nome_Label);
        Nome_Label.setBounds(160, 370, 110, 30);

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/calculadoraimc/ftc.png"))); // NOI18N
        jLabel2.setText("jLabel2");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(120, 90, 380, 150);

        JFundo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/calculadoraimc/fd.jpg"))); // NOI18N
        JFundo.setText("jLabel1");
        getContentPane().add(JFundo);
        JFundo.setBounds(0, 0, 612, 644);

        pack();
    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    String nome = TextField_Nome.getText();
    double peso = Double.parseDouble(TextField_Peso.getText());
    double altura = Double.parseDouble(TextField_Altura.getText()); 
    IMC i = new IMC(nome, peso, altura);

     TextField_Nome.setText(" ");
     TextField_Peso.setText(" ");
     TextField_Altura.setText(" ");
     i.calcularIMC();
    }                                        

    private void TextField_PesoActionPerformed(java.awt.event.ActionEvent evt) {                                               
        // TODO add your handling code here:
    }                                              

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JLabel Altura_Label;
    private javax.swing.JLabel JFundo;
    private javax.swing.JLabel Nome_Label;
    private javax.swing.JLabel Peso_Label;
    private javax.swing.JTextField TextField_Altura;
    private javax.swing.JTextField TextField_Nome;
    private javax.swing.JTextField TextField_Peso;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel2;
    // End of variables declaration                   
}
  • I don’t quite understand your question, could you rephrase ?

  • is so that the screen will not be maximize or decrease the screen size .

1 answer

4


If you do not want the screen to be maximized, simply disable the resize using the method setResizable():

seuframe.setResizable(false);

with this, the window will only have the default size, no longer allowing it to be maximized.

However, it is possible to adapt the fund so that it resize together with the screen, thus, it is not necessary to disable resizing.

Remembering that the above link method will only resize the background, if you want the form components to track resizing, you should use a Layoutmanager as the one that best suits your screen.

  • this second option is a little complex for me, I will try to read everything and follow, but about this your frame.setResizable I put the name of my jframe? Ex Jbackground.setResizable(false); and gave an error:

  • 1

    @Gabrielcosta do not paste codes in the comments please as it is not the appropriate field. You will replace seuframe by the variable that represents your Jframe.

  • http://prntscr.com/ln49op my frame is Newjfram, but this error has appeared, sorry I will delete the comment from the code .

  • @Gabrielcosta you know what a variable is? NewJFrame is not a variable if this print is from the same question code. This is the class name. As the class inherits from Jframe, simply call the above method inside the constructor using the this.

  • If you don’t know what it is or how to use the this, I recommend you stop your application and read the linked topics, as this is basic knowledge of the language and necessary: https://answall.com/a/108960/28595 and https://answall.com/q/186/28595

  • Actually, I ended up getting lost, and also I’m not able to find the name of the variable, and in case I would call inside the Newjframe constructor?

  • I’ll take a look, but I already use this the function of it I’m not sure, but I use smp for for for eg: calling a variable that is not within the method, I use this.

  • @Gabrielcosta yes, this can be called inside the builder, as I have already commented.

  • http://prntscr.com/ln4g9q I can’t figure out which one to use with setResizable.

  • 1

    @Gabrielcosta this.setResizable(false); inside the builder.

  • Ahh, yeah, I was thinking I had to name some variable, didn’t know I wouldn’t need it, I tested it without this, with this, and it was normal.

  • @Gabrielcosta works without the this because the compiler calls the this implicitly if you do not inform. But it is essential to use this, because there are cases where there are polymorphism of inherited methods.

  • Actually that part of this I smp use within methods, one thing I’m trying to change a part of the code within that form, but it doesn’t allow, you know how I could modify something manually?

  • 1

    @Gabrielcosta if this code was generated by netbeans, it doesn’t even let.

Show 9 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.