Sto scrivendo per esercizio una semplice applicazione java che data una stringa in ingresso e due caratteri c1 e c2, cerca c1 nella stringa e lo sostituisce con c2 ed infine stampa la stringa ottenuta.
Il problema è che non riesco ad inserire i widget nella posizione che vorrei io.. in pratica vorrei dare una dimensione fissa al Frame e posizionare i vari widget (2 label, 1 bottone e 4 entry) alle coordinate che voglio io.
Ho provato con il metodo setLocation(WIDTH, WIDTH); chiamato sul widget in questione, dopo averlo creato e prima di aggiungerlo al pannello, ma non riesco a posizionarlo
Nel caso voleste dare un'occhiata al mio codice:
Classe che eredita da JPanel
Maincodice:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package sostituisci; import java.awt.event.*; import javax.swing.*; /** * * @author Gianluca */ public class Sostituisci extends JPanel implements ActionListener{ private JTextField entry1; private JTextField entry2; private JTextField entry3; private JTextField entry4; private JButton button1; public Sostituisci(){ super(); entry1 = new JTextField(25); this.add(entry1); this.add(new JLabel("Vecchio Carattere")); entry2 = new JTextField(1); this.add(entry2); this.add(new JLabel("Nuovo Carattere")); entry3 = new JTextField(1); this.add(entry3); entry4 = new JTextField(25); entry4.setEditable(false); this.add(entry4); button1 = new JButton("Sostituisci"); this.add(button1); button1.addActionListener(this); } @Override public void actionPerformed(ActionEvent e){ try{ StringBuilder parola = new StringBuilder(entry1.getText()); char vecchioCarattere = entry2.getText().charAt(0); char nuovoCarattere = entry3.getText().charAt(0); for ( int i = 0; i < parola.length(); i++ ){ if ( parola.charAt(i) == vecchioCarattere ){ parola.setCharAt(i, nuovoCarattere); } } entry4.setText(parola.toString()); } catch(Exception ex){ System.out.println("Inserisci dei valori validi."); } } }
codice:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package sostituisci; import javax.swing.*; /** * * @author Gianluca */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here JFrame frame = new JFrame("Sostituisci"); Sostituisci panel = new Sostituisci(); frame.add(panel); frame.setBounds(350, 200, 350, 200); frame.setResizable(false); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }

 
			
			
 
					
					
					
						 Rispondi quotando
  Rispondi quotando