garagougne
Messages postés3Date d'inscriptionmardi 7 décembre 2004StatutMembreDernière intervention 7 mars 2005
-
4 mars 2005 à 10:37
garagougne
Messages postés3Date d'inscriptionmardi 7 décembre 2004StatutMembreDernière intervention 7 mars 2005
-
7 mars 2005 à 14:19
Bonjour,
je suis nouveau et ne connais pastres bien encore le fonctionnement de java.
J'essaie de faire une applet pour mettre en forme du
texte (choisir la police, le style, la taille et la couleur avec un aperçu).
Je bloque car le code semble correct et cela ne fonctionne pas:
voici le code:
public class AfficheurPolice extends javax.swing.JDialog
{
}
/** Creates new form AfficheurPolice */
public AfficheurPolice (java.awt.Frame parent, boolean modal)
{
super (parent, modal);
initComponents ();
initialisation();
}
/** 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.
*/
private void initComponents()//GEN-BEGIN:initComponents
{
jPanel1 = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
jLabelPolice = new javax.swing.JLabel();
jTextFieldPolice = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
jListPolice = new javax.swing.JList();
jPanel3 = new javax.swing.JPanel();
jLabelStyle = new javax.swing.JLabel();
jTextFieldStyle = new javax.swing.JTextField();
jScrollPane2 = new javax.swing.JScrollPane();
jListStyle = new javax.swing.JList();
jPanel4 = new javax.swing.JPanel();
jLabelTaille = new javax.swing.JLabel();
jTextFieldTaille = new javax.swing.JTextField();
jScrollPane3 = new javax.swing.JScrollPane();
jListTaille = new javax.swing.JList();
jPanel5 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jPanel8 = new javax.swing.JPanel();
jPanel6 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jComboBoxCouleur = new javax.swing.JComboBox();
jPanel7 = new javax.swing.JPanel();
jLabelApercu = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Modifier l'apparence des messages");
setModal(true);
setResizable(false);
addWindowListener(new java.awt.event.WindowAdapter()
{
public void windowClosing(java.awt.event.WindowEvent evt)
{
closeDialog(evt);
}
});
/**
* @param args the command line arguments
*/
public static void main (String args[])
{
new AfficheurPolice (new javax.swing.JFrame (), true).show ();
}
private void initialisation(){
tabMesCouleurs = new AfficheurPolice.MesCouleurs[12];
tabMesCouleurs[0] = new MesCouleurs("bleu", 0, 0, 255);
tabMesCouleurs[1] = new MesCouleurs("vert", 0, 255, 0);
tabMesCouleurs[2] = new MesCouleurs("rouge", 255, 0, 0);
tabMesCouleurs[3] = new MesCouleurs("noir", 0, 0, 0);
tabMesCouleurs[4] = new MesCouleurs("orange", 255,164 , 72);
tabMesCouleurs[5] = new MesCouleurs("jaune", 255, 255, 0);
tabMesCouleurs[6] = new MesCouleurs("gris", 224, 223, 227);
tabMesCouleurs[7] = new MesCouleurs("violet", 153, 0, 107);
tabMesCouleurs[8] = new MesCouleurs("bleu fonce", 0, 0, 153);
tabMesCouleurs[9] = new MesCouleurs("vert fonce", 0, 147, 0);
tabMesCouleurs[10] = new MesCouleurs("jaune fonce", 225, 225, 0);
tabMesCouleurs[11] = new MesCouleurs("orange fonce", 255, 128, 0);
class ComboBoxCouleurRenderer extends javax.swing.JLabel implements javax.swing.ListCellRenderer {
public ComboBoxCouleurRenderer() {
setOpaque(true);
setHorizontalAlignment(CENTER);
setVerticalAlignment(CENTER);
}
/*
* This method finds the image and text corresponding
* to the selected value and returns the label, set up
* to display the text and image.
*/
public java.awt.Component getListCellRendererComponent(javax.swing.JList list,Object value,int index,boolean isSelected,boolean cellHasFocus) {
//Get the selected index. (The index param isn't
//always valid, so just use the value.)
//System.out.println("class value:"+value.getClass ().getName ());
//System.out.println(value.toString ());
if(value !=null){
MesCouleurs c = (MesCouleurs) value;
javax.swing.Icon icon = new ColoredBox(c.obtenirCouleur ());
setIcon(icon);
if (icon != null) {
setText(value.toString ());
setFont(list.getFont());
} else {
setText("value.toString () (no image available)");
}
private String verificateurDeContenu(String s,String Exp){
java.util.regex.Pattern p = java.util.regex.Pattern.compile (Exp);
java.util.regex.Matcher m = p.matcher (s);
String s2 = "";
boolean trouve = false;
System.out.println("analyse s="+s);
while(m.find()){
trouve = true;
int dep = m.start ();
int fin = m.end();
System.out.println("dep="+dep);
if(dep >0)
s2 += s.substring (0,dep);
System.out.println("fin ="+fin);
if(fin 44){
jTextField.setText((String) jListTaille.getSelectedValue ());
}
apercu();
}*/
}
private void apercu(){
String t = "AaBbCcEeZz";
java.awt.Font f = new java.awt.Font(jTextFieldPolice.getText (), jListStyle.getSelectedIndex (),Integer.parseInt (jTextFieldTaille.getText ()));
jLabelApercu.setFont (f);
jLabelApercu.setText (t);
}
class ColoredBox implements javax.swing.Icon {
java.awt.Color color;
public ColoredBox (java.awt.Color c) {
color = c;
}
public void paintIcon (java.awt.Component c, java.awt.Graphics g, int x, int y) {
g.setColor(color);
g.fillRect (x, y, getIconWidth(), getIconHeight());
}
public int getIconWidth() {
return 10;
}
public int getIconHeight() {
return 10;
}
}
private class MesCouleurs{
public MesCouleurs(String nom, int R,int G,int B){
red = R;
green = G;
blue = B;
nomCouleur = nom;
laCouleur = new java.awt.Color(red,green,blue);
}
/*public void DefinirMesCouleurs(String nom, int R,int G,int B){
new MesCouleurs( nom, R, G, B);
}*/
public java.awt.Color obtenirCouleur(){
wildcat31
Messages postés46Date d'inscriptionjeudi 16 décembre 2004StatutMembreDernière intervention28 août 20053 7 mars 2005 à 13:34
Après documentation, c'est beaucoup mieux si tu gardes la classe telle qu'elle est (en enlevant la méthode main), et tu rajoutes une nouvelle classe qui est une applet, et qui joue aussi le rôle du main :
import java.applet.Applet;
public class AppletAfficheurPolice extends Applet {
public void init()
{
new AfficheurPolice (new javax.swing.JFrame (), true).show();
}
}
wildcat31
Messages postés46Date d'inscriptionjeudi 16 décembre 2004StatutMembreDernière intervention28 août 20053 7 mars 2005 à 10:31
Bonjour,
J'ai recopié ton code, et il marche bien. Une fenêtre apparaît avec les choix pour la police, la taille, etc, et l'aperçu change quand on modifie un choix. Je ne vois pas de problème.
Ton problème est-il lié au fait que tu veux une applet, alors que toi tu as fait une application java, et non une applet ?
garagougne
Messages postés3Date d'inscriptionmardi 7 décembre 2004StatutMembreDernière intervention 7 mars 2005 7 mars 2005 à 12:25
Bonjour,
merci pour votre reponse, en fait je lance le code sous dos en java AfficheurPOlice et effectivement tout se passe bien, mais j'aurais aimé faire une applet. Inutile de seleurrer le code n'est pas de moi je l'ai trouvé sur ce site. Ayant trop peu de connaissance en java, j'aurais juste aime que l'on m'explique comment transformer cette application en applet.
wildcat31
Messages postés46Date d'inscriptionjeudi 16 décembre 2004StatutMembreDernière intervention28 août 20053 7 mars 2005 à 13:04
Voici un essai de transformation. Je m'y connais pas beaucoup en Applet, mais c'est l'idée. Après y'a sûrement des choses à changer, parce que j'ai supprimé certains trucs dont je ne connaissais pas l'équivalent en Applet.
package test1;
import java.applet.Applet;
public class AfficheurPolice extends Applet
{
static String envfonts[];
/** Creates new form AfficheurPolice */
public void init ()
{
initComponents ();
initialisation();
}
/** 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.
*/
private void initComponents()//GEN-BEGIN:initComponents
{
jPanel1 = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
jLabelPolice = new javax.swing.JLabel();
jTextFieldPolice = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
jListPolice = new javax.swing.JList();
jPanel3 = new javax.swing.JPanel();
jLabelStyle = new javax.swing.JLabel();
jTextFieldStyle = new javax.swing.JTextField();
jScrollPane2 = new javax.swing.JScrollPane();
jListStyle = new javax.swing.JList();
jPanel4 = new javax.swing.JPanel();
jLabelTaille = new javax.swing.JLabel();
jTextFieldTaille = new javax.swing.JTextField();
jScrollPane3 = new javax.swing.JScrollPane();
jListTaille = new javax.swing.JList();
jPanel5 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jPanel8 = new javax.swing.JPanel();
jPanel6 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jComboBoxCouleur = new javax.swing.JComboBox();
jPanel7 = new javax.swing.JPanel();
jLabelApercu = new javax.swing.JLabel();
class ComboBoxCouleurRenderer extends javax.swing.JLabel implements javax.swing.ListCellRenderer {
public ComboBoxCouleurRenderer() {
setOpaque(true);
setHorizontalAlignment(CENTER);
setVerticalAlignment(CENTER);
}
/*
* This method finds the image and text corresponding
* to the selected value and returns the label, set up
* to display the text and image.
*/
public java.awt.Component getListCellRendererComponent(javax.swing.JList list,Object value,int index,boolean isSelected,boolean cellHasFocus) {
//Get the selected index. (The index param isn't
//always valid, so just use the value.)
//System.out.println("class value:"+value.getClass ().getName ());
//System.out.println(value.toString ());
if(value !=null){
MesCouleurs c = (MesCouleurs) value;
javax.swing.Icon icon = new ColoredBox(c.obtenirCouleur ());
setIcon(icon);
if (icon != null) {
setText(value.toString ());
setFont(list.getFont());
} else {
setText("value.toString () (no image available)");
}
private String verificateurDeContenu(String s,String Exp){
java.util.regex.Pattern p = java.util.regex.Pattern.compile (Exp);
java.util.regex.Matcher m = p.matcher (s);
String s2 = "";
boolean trouve = false;
System.out.println("analyse s="+s);
while(m.find()){
trouve = true;
int dep = m.start ();
int fin = m.end();
System.out.println("dep="+dep);
if(dep >0)
s2 += s.substring (0,dep);
System.out.println("fin ="+fin);
if(fin 44){
jTextField.setText((String) jListTaille.getSelectedValue ());
}
apercu();
}*/
}
private void apercu(){
String t = "AaBbCcEeZz";
java.awt.Font f = new java.awt.Font(jTextFieldPolice.getText (), jListStyle.getSelectedIndex (),Integer.parseInt (jTextFieldTaille.getText ()));
jLabelApercu.setFont (f);
jLabelApercu.setText (t);
}
class ColoredBox implements javax.swing.Icon {
java.awt.Color color;
public ColoredBox (java.awt.Color c) {
color = c;
}
public void paintIcon (java.awt.Component c, java.awt.Graphics g, int x, int y) {
g.setColor(color);
g.fillRect (x, y, getIconWidth(), getIconHeight());
}
public int getIconWidth() {
return 10;
}
public int getIconHeight() {
return 10;
}
}
private class MesCouleurs{
public MesCouleurs(String nom, int R,int G,int B){
red = R;
green = G;
blue = B;
nomCouleur = nom;
laCouleur = new java.awt.Color(red,green,blue);
}
/*public void DefinirMesCouleurs(String nom, int R,int G,int B){
new MesCouleurs( nom, R, G, B);
}*/
public java.awt.Color obtenirCouleur(){