PDA

View Full Version : [JAVA] JList, switch/case, if e il ciclo infinito della pazzia


Sergei
01-05-2012, 17:08
Salve gente :)

Volevo ultimare il mio piccolo notepad e aggiungere le ultime funzioni. Praticamente devo poter scegliere da una finestra il carattere, lo stile e la grandezza.
Premetto che sono neofita e premetto anche che mi sono totalmente incartato e non sto capendo più niente :D

All'inizio avevo realizzato una finestra con 3 jlist, una per il font, una per lo stile (non utilizzata) e una per la grandezza del testo e le avevo collegate ad un anteprima nella stessa finestra. Per utilizzare le due list avevo usato un doppio if. Allora ho pensato di aggiungere anche un altro if con lo switch ma non funge (ma si può fare un triplo ciclo if?). E poi c'è anche il problema di come settare la selezione dello stile e farla funzionare nella textarea. Forse mi spiego meglio con il codice :D
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class TipoCaratteri extends JFrame implements ActionListener,
ListSelectionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel pannello3;
JPanel panteprima;
JList tipoCaratteri, stile, punti;
JTextArea anteprima;
JButton conferma, annulla;
JLabel lbTipo, lbStile, lbPunti;
JScrollPane scrolla, scrollanumeri;
String[] fonts = { "Arial", "Mangal", "Lucida Console", "Courier",
"Comic Sans MS", "Script" };
String[] styles = { "Normale", "Grassetto", "Corsivo", "Grassetto corsivo" };
String selezione;
NotepadGUI setCaratteri;
Integer grandezza[] = { 8, 9, 10, 11, 12, 14, 16, 20, 22, 24, 26, 28, 36,
48, 72 };
Font font;
String ant, n3;
int n2 = 12;
int punto, index;

public TipoCaratteri(NotepadGUI car) {
super("Tipo di carattere");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 606, 461);
pannello3 = new JPanel();
pannello3.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(pannello3);
pannello3.setLayout(null);
setVisible(true);
setCaratteri = car;

tipoCaratteri = new JList(fonts);
tipoCaratteri.setBounds(33, 100, 115, 154);
tipoCaratteri.setSelectedIndex(2); // imposto font iniziale
// aggiungo lo scroll
scrolla = new JScrollPane(tipoCaratteri);
scrolla.setBounds(33, 100, 115, 154); // ha la stessa posizione della
// JList
pannello3.add(scrolla); // va inserito lo scroll e non la jlist perche'
// gia' contenuta nello scroll

stile = new JList(styles);
stile.setBounds(196, 100, 115, 154);
stile.setSelectedIndex(0);
pannello3.add(stile);

punti = new JList(grandezza);
punti.setBounds(357, 100, 62, 154);
punti.setSelectedIndex(4); // imposto grandezza iniziale
// aggiungo lo scroll
scrollanumeri = new JScrollPane(punti);
scrollanumeri.setBounds(357, 100, 62, 154); // vedi sopra
pannello3.add(scrollanumeri);

conferma = new JButton("OK");
conferma.setBounds(464, 97, 89, 23);
pannello3.add(conferma);

annulla = new JButton("Annulla");
annulla.setBounds(464, 128, 89, 23);
pannello3.add(annulla);

panteprima = new JPanel();
panteprima.setBorder(new TitledBorder(UIManager
.getBorder("TitledBorder.border"), "Esempio",
TitledBorder.LEADING, TitledBorder.TOP, null, null));
panteprima.setBounds(296, 309, 168, 77);
pannello3.add(panteprima);

anteprima = new JTextArea();
anteprima.setBackground(UIManager.getColor("Button.background"));
anteprima.setText("AaBbYyZz");
panteprima.add(anteprima);

lbTipo = new JLabel("Tipo di carattere:");
lbTipo.setBounds(33, 67, 115, 14);
pannello3.add(lbTipo);

lbStile = new JLabel("Stile:");
lbStile.setBounds(196, 67, 115, 14);
pannello3.add(lbStile);

lbPunti = new JLabel("Punti:");
lbPunti.setBounds(357, 67, 46, 14);
pannello3.add(lbPunti);

// aziono le liste
tipoCaratteri.addListSelectionListener(this);
punti.addListSelectionListener(this);
stile.addListSelectionListener(this);

// aziono i pulsanti
conferma.addActionListener(this);
annulla.addActionListener(this);

}

@Override
public void valueChanged(ListSelectionEvent e) {

// parte per l'anteprima

if (e.getValueIsAdjusting() == false) {
String ant = (String) tipoCaratteri.getSelectedValue();
font = new Font(ant, Font.PLAIN, 12);
anteprima.setFont(font);

if (e.getValueIsAdjusting() == false) {
int index = stile.getSelectedIndex();
switch (index) {
case 0:
font = new Font(ant, Font.PLAIN, 12);
anteprima.setFont(font);
break;
case 1:
font = new Font(ant, Font.BOLD, 12);
anteprima.setFont(font);
break;
case 2:
font = new Font(ant, Font.ITALIC, 12);
anteprima.setFont(font);
break;
case 3:
font = new Font(ant, Font.BOLD + Font.ITALIC, 12);
anteprima.setFont(font);
break;

}

if (e.getValueIsAdjusting() == false) {

n2 = (Integer) punti.getSelectedValue();
font = new Font(ant, Font.BOLD, n2);
anteprima.setFont(font);
}
}
}

// parte che effettua il cambiamento nella textarea nel momento in cui
// viene cliccato il tasto OK (conferma)
selezione = (String) tipoCaratteri.getSelectedValue();
punto = (Integer) punti.getSelectedValue();

}

@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == conferma) {
setCaratteri.ta.setFont(new Font(selezione, Font.PLAIN, punto));
setVisible(false);
}

}

}


Oppure devo trovare un altro modo. Ho visto che Font.BOLD (o altro) è di tipo int, però se realizzo un array con dentro Font.BOLD etc di tipo Integer chiaramente lo trasforma in numero e... niente, sto impazzendo :D

Sergei
04-05-2012, 21:57
nessuna idea? non se neanche cosa scrivere per cercare un problema simile su google :doh: