Visualizzazione dei risultati da 1 a 8 su 8
  1. #1

    [JAVA] - Disabilitare i tasti sulla finestra

    Salve a tutti, ho un piccolo problema...

    Ho creato un nuovo progetto grafico in netbeans, la classe della finestra principale del programma è la seguente, ossia quella creata in automatico da netbeans:

    codice:
    public class Genetica extends FrameView {
    ...
    }
    Siccome il netbeans mi mette una JDialog che funge da About Box che presenta il solo tasto X per chiudere la finestra, vorrei sapere se c'è la possibilità di applicare questa proprietà anche alla finestra principale...

    Ho provato in moltissimi modi, ma non trovo nessun metodo simile a questo getRootPane().setDefaultButton(closeButton);

    codice:
    public GeneticaAboutBox(java.awt.Frame parent) {
            super(parent);
            initComponents();
            getRootPane().setDefaultButton(closeButton);
        }
    Un'ultima cosa: come faccio a creare un controllo che, se c'è una nuova finestra su quella principale già aperta, mi impedisca di aprirne un'altra...?

  2. #2
    Nessuno mi sa aiutare?


  3. #3

    Re: [JAVA] - Disabilitare i tasti sulla finestra

    Un'ultima cosa: come faccio a creare un controllo che, se c'è una nuova finestra su quella principale già aperta, mi impedisca di aprirne un'altra...?
    setti la finestra come modal ovvero Jdialog.setModal(true).
    per l'altro problema non uso netBean quindi non so cosa dirti.comunque puoi sempre creare un nuovo jButton e asssegnargli un ascoltatore che faccia il System.exit().
    spero di esserti stata d'aiuto

  4. #4

    Re: Re: [JAVA] - Disabilitare i tasti sulla finestra

    Originariamente inviato da darksoullight88
    setti la finestra come modal ovvero Jdialog.setModal(true).
    per l'altro problema non uso netBean quindi non so cosa dirti.comunque puoi sempre creare un nuovo jButton e asssegnargli un ascoltatore che faccia il System.exit().
    spero di esserti stata d'aiuto
    Il problema è che la mia finestra non è una JDialog, bensì un javax.swing.JFrame.

    Grazie comunque per l'aiuto.

  5. #5

    mumble..

    mumble..se non ho capito male tu hai 2 jFrame e quello più recente deve essere "modale"??
    scusa ma non sono sicura di aver capito correttamente.

  6. #6

    Re: mumble..

    Originariamente inviato da darksoullight88
    mumble..se non ho capito male tu hai 2 jFrame e quello più recente deve essere "modale"??
    scusa ma non sono sicura di aver capito correttamente.
    Esatto

  7. #7

    esempio

    questo è un esempio molto spartano di come potresti risolvere il tuo problema
    codice:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.awt.Rectangle;
    import java.awt.event.KeyEvent;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    /**
     * @author marialaura
     * 
     */
    public class DueJFrame extends JFrame {
    
    	private static final long serialVersionUID = 1L;
    	private JPanel jContentPane = null;
    	private JButton jButton = null;
    	private JFrame jFrame1 = null; // @jve:decl-index=0:visual-constraint="358,10"
    	private JPanel jContentPane1 = null;
    	private JButton jButton1 = null;
    
    	/**
    	 * This is the default constructor
    	 */
    	public DueJFrame() {
    		super();
    		initialize();
    	}
    
    	/**
    	 * This method initializes this
    	 * 
    	 * @return void
    	 */
    	private void initialize() {
    		this.setSize(300, 200);
    		this.setResizable(false);
    		this.setPreferredSize(new Dimension(300, 200));
    		this.setMinimumSize(new Dimension(300, 200));
    		this.setMaximumSize(new Dimension(300, 200));
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setBackground(Color.white);
    		this.setContentPane(getJContentPane());
    		this.setTitle("Primo");
    		this.setVisible(true);
    	}
    
    	/**
    	 * This method initializes jContentPane
    	 * 
    	 * @return javax.swing.JPanel
    	 */
    	private JPanel getJContentPane() {
    		if (jContentPane == null) {
    			GridBagConstraints gridBagConstraints = new GridBagConstraints();
    			gridBagConstraints.gridx = 0;
    			gridBagConstraints.gridheight = 1;
    			gridBagConstraints.gridwidth = 1;
    			gridBagConstraints.insets = new Insets(5, 5, 5, 5);
    			gridBagConstraints.ipadx = 5;
    			gridBagConstraints.ipady = 5;
    			gridBagConstraints.gridy = 0;
    			jContentPane = new JPanel();
    			jContentPane.setLayout(new GridBagLayout());
    			jContentPane.setBackground(Color.white);
    			jContentPane.add(getJButton(), gridBagConstraints);
    		}
    		return jContentPane;
    	}
    
    	/**
    	 * This method initializes jButton
    	 * 
    	 * @return javax.swing.JButton
    	 */
    	private JButton getJButton() {
    		if (jButton == null) {
    			try {
    				jButton = new JButton();
    				jButton.setFont(new Font("Tahoma", Font.BOLD, 14));
    				jButton.setText("PROSSIMO");
    				jButton.setPreferredSize(new Dimension(150, 30));
    				jButton.addMouseListener(new java.awt.event.MouseAdapter() {
    					public void mouseClicked(java.awt.event.MouseEvent e) {
    						prossimo();
    					}
    				});
    			} catch (java.lang.Throwable e) {
    				e.printStackTrace();
    			}
    		}
    		return jButton;
    	}
    
    	/**
    	 * This method initializes jFrame1
    	 * 
    	 * @return javax.swing.JFrame
    	 */
    	private JFrame getJFrame1() {
    		if (jFrame1 == null) {
    			try {
    				jFrame1 = new JFrame();
    				jFrame1.setBounds(new Rectangle(0, 0, 300, 200));
    				jFrame1.setBackground(Color.white);
    				jFrame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				jFrame1.setMaximumSize(new Dimension(300, 200));
    				jFrame1.setMinimumSize(new Dimension(300, 200));
    				jFrame1.setPreferredSize(new Dimension(300, 200));
    				jFrame1.setTitle("Secondo");
    				jFrame1.setResizable(false);
    				jFrame1.setContentPane(getJContentPane1());
    			} catch (java.lang.Throwable e) {
    				e.printStackTrace();
    			}
    		}
    		return jFrame1;
    	}
    
    	/**
    	 * This method initializes jContentPane1
    	 * 
    	 * @return javax.swing.JPanel
    	 */
    	private JPanel getJContentPane1() {
    		if (jContentPane1 == null) {
    			try {
    				GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
    				gridBagConstraints1.gridx = 0;
    				gridBagConstraints1.insets = new Insets(5, 5, 5, 5);
    				gridBagConstraints1.ipadx = 2;
    				gridBagConstraints1.ipady = 2;
    				gridBagConstraints1.gridy = 0;
    				jContentPane1 = new JPanel();
    				jContentPane1.setLayout(new GridBagLayout());
    				jContentPane1.setBackground(Color.white);
    				jContentPane1.add(getJButton1(), gridBagConstraints1);
    			} catch (java.lang.Throwable e) {
    				e.printStackTrace();
    			}
    		}
    		return jContentPane1;
    	}
    
    	/**
    	 * This method initializes jButton1
    	 * 
    	 * @return javax.swing.JButton
    	 */
    	private JButton getJButton1() {
    		if (jButton1 == null) {
    			try {
    				jButton1 = new JButton();
    				jButton1.setFont(new Font("Tahoma", Font.BOLD, 14));
    				jButton1.setPreferredSize(new Dimension(150, 30));
    				jButton1.setText("INDIETRO");
    				jButton1.setMnemonic(KeyEvent.VK_UNDEFINED);
    				jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
    					public void mouseClicked(java.awt.event.MouseEvent e) {
    						indietro();
    					}
    				});
    			} catch (java.lang.Throwable e) {
    				e.printStackTrace();
    			}
    		}
    		return jButton1;
    	}
    
    	private void prossimo() {
    		// disabiliti il primo
    		this.setEnabled(false);
    		// mostri il secondo frame
    		getJFrame1().setVisible(true);
    	}
    
    	private void indietro() {
    		// abilito il primo
    		this.setEnabled(true);
    		// nascondo il sencondo
    		getJFrame1().dispose();
    	}
    
    	public static void main(String[] args) {
    		new DueJFrame();
    	}
    }
    spero ti possa aiutare.

  8. #8
    Grazie mille per la risposta tempestiva, lo guarderò subito...

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.