簡體   English   中英

如何為包含多個JPanel,JButton和JLabel的JFrame設置背景色?

[英]How to set a background color to a JFrame containing multiple JPanels, JButtons and JLabels?

問題

首先,我正在使用Eclipse。 因此,我正在構建一個帶有多個面板,按鈕和標簽的框架,並且我希望使用除默認白色之外的另一種顏色。 我嘗試了幾種解決方案,如下所示:

在這里和那里提出的解決方案都沒有一個對我有用。 現在是時候展示一些代碼,以了解我想做什么以及是否有可能這樣做。

所以這是我的JFrame類,稱為Body.java 當我使用很多類(擴展JPanel,大約15個左右)時,我注釋了Body中所有無法編譯的代碼。 我只是在框架上保留了一個小班級,之后我將在每個班級上應用該解決方案。 您當然可以問我更多的課程! 我運行了該代碼,一切正常(編譯和執行完成)。 我留下了所有內容,因此,如果您獲得更多的類,則可以編譯此代碼。

請注意,因為我是法國人,所以所有班級的名字都是法文,也有一些注釋(但我翻譯了所有必要的注釋以了解我在做什么,並添加了一些注釋)。 顯示的內容也將以法語顯示。

編碼

Simplicity.java

這是我的控制器,只是用來啟動框架

package controller;

import view.Body;

public class Simplicity {

    public Simplicity() {

    }

    public static void main(String[] args) {

        Body fen = new Body();
        fen.setVisible(true);

    }

}

這是框架的代碼:

Body.java

package view;

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;

/**
 * @author David Rei
 *
 */

/*
 * This is a project in which we had to develop a UI for smartphones.
 */

public class Body extends JFrame{

    private static final long serialVersionUID = 1L;

    // initial size : width = 230, height = 390.
    private static final int FRAME_WIDTH = 250;
    private static final int FRAME_HEIGHT = 400;

    // My fixed panels
    private static JPanel niveauBase = new JPanel(); // Basic panel -> everything will go on it
    private static JPanel infosReseauxHeure = new JPanel(); // Panel fixed
    private JPanel panelChangeable = new JPanel(); // Scrolling panel

    // up of page info
    private static JTextPane infos = new JTextPane();

    // Creation of the different pages (panels)
    private Tutoriel tuto;
    /*
    private Accueil1 ac1;
    private Accueil2 ac2;
    private Medias1 med1;
    private Multimedias1 multimed1;
    private Outils1 out1;
    private Outils2 out2;
    private Jeux1 jeux1;
    private Jeux2 jeux2;
    private SocialInternet1 socInt1;
    private SocialInternet2 socInt2;

    private Appeler appeler;
    private Urgences urgences;

    private AppelEnCours appelEnCours;
    private DemarrageApp demarrageApp;
    private EnConstruction enConstruction;
    */
    public Body() {
        super();
        buildFrame();
        this.buildContentPane();
        this.setContentPane(niveauBase);
    }
    public void buildFrame() {
        this.setTitle("Simplicity");
        this.setResizable(false);
        this.setSize(FRAME_WIDTH, FRAME_HEIGHT);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.getContentPane().setBackground(new Color(255, 105, 86));

        // toutes les dimensions sont en relatif par rapport à la taille de la JFrame. de fait, en changeant celle-ci, on peut adapter l'interface à différents écrans      

        // on met en place ce qui ne changera jamais d'un panel à l'autre --> les infos de base et le support
            // support
        niveauBase.setLayout(null);

            // basic info
        infosReseauxHeure.setBounds(0, 0, this.getWidth(), (int)(0.12*this.getHeight()));
        infosReseauxHeure.setLayout(new GridLayout(0,1));
        infosReseauxHeure.add(infos);

            // adding the text
        niveauBase.add(infosReseauxHeure);

        // defining the limits of the changing Panel, bc they are always the same
        panelChangeable.setLayout(null);
        // My first try of changing the background 
//      panelChangeable.setBackground(new Color(255, 105, 86));
        panelChangeable.setBounds(0, infosReseauxHeure.getHeight(), this.getWidth(), (int) (0.88*this.getHeight()));
        niveauBase.add(panelChangeable);

        // the basic frame, which will never change, is ready.

    }
    public void buildContentPane() {

        // Instantiation of the pages

        tuto = new Tutoriel(this);
        /*
        ac1 = new Accueil1(this);
        ac2 = new Accueil2(this);
        med1 = new Medias1(this);
        multimed1 = new Multimedias1(this);
        out1 = new Outils1(this);
        out2 = new Outils2(this);
        jeux1 = new Jeux1(this);
        jeux2 = new Jeux2(this);
        socInt1 = new SocialInternet1(this);
        socInt2 = new SocialInternet2(this);

        appeler = new Appeler(this);
        urgences = new Urgences(this);

        appelEnCours = new AppelEnCours(this);
        demarrageApp = new DemarrageApp(this);
        enConstruction = new EnConstruction(this);
        */

        // Tutoriel is the first to be displayed
        CaractsPanelChangeant(tuto);
        panelChangeable.add(tuto);

        // all the other panels have the same displaying characteristics
        // navigating panels
        /*
        addPanels(ac1);
        addPanels(ac2);
        addPanels(med1);
        addPanels(multimed1);
        addPanels(out1);
        addPanels(out2);
        addPanels(jeux1);
        addPanels(jeux2);
        addPanels(socInt1);
        addPanels(socInt2);

        // applications panels
        addPanels(appeler);
        addPanels(urgences);

        // diverse panels
        addPanels(appelEnCours);
        addPanels(demarrageApp);
        addPanels(enConstruction);
        */
    }
    // fixating the same size and origins for all the panels
    public void CaractsPanelChangeant(JPanel a) {

        a.setBounds(0, 0, panelChangeable.getWidth(), panelChangeable.getHeight());
    }
    // regroup the same actions for the panels : bounds, adding and invisible
    public void addPanels(JPanel a) {
        CaractsPanelChangeant(a);
        panelChangeable.add(a);
        a.setVisible(false);
        /*
         * I tried to change the background here too
        a.setOpaque(true);
        a.setBackground(new Color(255, 105, 86));
        */
    }

    // JPanel getters
    public static int getInfosReseauxHeureHeight() {
        return infosReseauxHeure.getHeight();
    }
    /*
    public Accueil1 getAc1() {
        return ac1;
    }
    public Accueil2 getAc2() {
        return ac2;
    }
    public Medias1 getMed1() {
        return med1;
    }
    public Multimedias1 getMultimed1() {
        return multimed1;
    }
    public Outils1 getOut1() {
        return out1;
    }
    public Outils2 getOut2() {
        return out2;
    }
    public Jeux1 getJeux1() {
        return jeux1;
    }
    public Jeux2 getJeux2() {
        return jeux2;
    }
    public SocialInternet1 getSocInt1() {
        return socInt1;
    }
    public SocialInternet2 getSocInt2() {
        return socInt2;
    }

    public Appeler getAppeler() {
        return appeler;
    }
    public Urgences getUrgences() {
        return urgences;
    }
    public AppelEnCours getAppelEnCours() {
        return appelEnCours;
    }
    public DemarrageApp getDemarrageApp() {
        return demarrageApp;
    }
    public EnConstruction getEnConstruction() {
        return enConstruction;
    }
    */
    public Tutoriel getTuto() {
        return tuto;
    }

    // other getters
    public static int getFrameWidth() {
        return FRAME_WIDTH;
    }
    public static int getFrameHeight() {
        return FRAME_HEIGHT;
    }
    public JPanel getPanelChangeable() {
        return panelChangeable;
    }

    // setters
    public void setPanelChangeable(JPanel a) {
        this.panelChangeable = a;
    }
    /*
    public void switchAccueil(){
        ac1.setVisible(!ac1.isVisible());
        ac2.setVisible(!ac2.isVisible());
    }
    */
}

這就是我的JFrame。 現在是我之前談到的小類,這是使用我們的項目的教程(開頭是Body.java注釋)。

Tutoriel.java

package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Insets;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import model.SelectAction;


/**
 * @author David Rei
 *
 */

/*
 * This class displays a tutorial when the application is launched.
 * It is then setVisible(false), to not appear again.
 * Here, I use it to show you one of my panels, which background has to be a color.
 * 
 */
public class Tutoriel extends JPanel {

    private static final long serialVersionUID = 1L;

    private Body body;

    // panels
    private JPanel tout;
    private JPanel centre;
    private JPanel messageCentre1;
    private JPanel messageCentre2;

    // buttons
    private JButton ok1;
    private JButton ok2;

    private JLabel messageHaut;
    private JLabel tuto1;
    private JLabel FGauche;
    private JLabel FDroite;
    private JLabel tuto2;
    private JCheckBox tutoOff;

    //private Color fond = new Color(255, 105, 86);


    public Tutoriel(Body body) {
        super();
        this.body = body;
        buildTuto();
    }
    public void buildTuto() {

        this.setLayout(new GridLayout(0,1));
        tout = new JPanel();
        tout.setLayout(new BorderLayout(0,0));
        this.add(tout);

        // tutorial announce
        messageHaut = new JLabel("TUTORIEL");
        messageHaut.setForeground(new Color(0, 0, 0));
        messageHaut.setFont(new Font("Arial", Font.BOLD, 20));
        messageHaut.setHorizontalAlignment(JLabel.CENTER);
        tout.add(messageHaut, BorderLayout.NORTH);

        // central panel, with the tutorial
        centre = new JPanel();
        centre.setLayout(null);
        tout.add(centre, BorderLayout.CENTER);

        // first tutorial
        messageCentre1 = new JPanel();
        messageCentre1.setLayout(null);
        messageCentre1.setBounds(0, 0, 250, 300);
        centre.add(messageCentre1);

        tuto1 = new JLabel("<html>POUR NAVIGUER ENTRE<br>LES DIFFÉRENTES PAGES<br>UTILISER LES FLÈCHES</html>");
        tuto1.setForeground(new Color(0, 0, 0));
        tuto1.setFont(new Font("Arial", Font.BOLD, 16));
        tuto1.setHorizontalAlignment(JLabel.CENTER);
        tuto1.setVerticalAlignment(JLabel.CENTER);
        tuto1.setBounds(20, -10, 200, 150);
        messageCentre1.add(tuto1);

        // arrows
        FGauche = new JLabel();
        FGauche.setIcon(new ImageIcon(getClass().getResource("/img/left-blue.png")));
        FGauche.setBackground(new Color(255, 153, 0));
        FGauche.setBounds(80, 150, 40, 40);
        messageCentre1.add(FGauche);

        FDroite = new JLabel();
        FDroite.setIcon(new ImageIcon(getClass().getResource("/img/right-blue.png")));
        FDroite.setBackground(new Color(255, 153, 0));
        FDroite.setBounds(140, 150, 40, 40);
        messageCentre1.add(FDroite);

        // OK button, first tutorial
        ok1 = new JButton(new SelectAction(body));
        ok1.setActionCommand("tuto1");
        ok1.setText("OK");
        ok1.setMargin(new Insets(5, 5, 5, 5));
        ok1.setForeground(new Color(0, 0, 0));
        ok1.setBackground(new Color(255, 153, 0));
        ok1.setBounds(105, 230, 40, 40);
        messageCentre1.add(ok1);

        // second tutorial
        messageCentre2 = new JPanel();
        messageCentre2.setLayout(null);
        messageCentre2.setBounds(0, 0, 250, 300);
        centre.add(messageCentre2);

        tuto2 = new JLabel("<html>POUR RETOURNER<br>A L'ACCUEIL, APPUYER<br>SUR LA TOUCHE G</html>");
        tuto2.setForeground(new Color(0, 0, 0));
        tuto2.setFont(new Font("Arial", Font.BOLD, 16));
        tuto2.setHorizontalAlignment(JLabel.CENTER);
        tuto2.setVerticalAlignment(JLabel.CENTER);
        tuto2.setBounds(20, -10, 200, 150);
        messageCentre2.add(tuto2);

        // OK button, second tutorial
        ok2 = new JButton(new SelectAction(body));
        ok2.setActionCommand("tuto2");
        ok2.setText("OK");
        ok2.setMargin(new Insets(5, 5, 5, 5));
        ok2.setForeground(new Color(0, 0, 0));
        ok2.setBackground(new Color(255, 153, 0));
        ok2.setBounds(105, 230, 40, 40);
        messageCentre2.add(ok2);

        // visibility of tutorial panels
        //messageCentre1.setVisible(false);
        messageCentre2.setVisible(false);

        // deactivate the tutorial (and reach the main page)
        tutoOff = new JCheckBox(new SelectAction(body));
        tutoOff.setActionCommand("tutoOff");
        tutoOff.setText("NE PLUS AFFICHER AU DÉMARRAGE");
        tutoOff.setForeground(new Color(0, 0, 0));
        tout.add(tutoOff, BorderLayout.SOUTH);
    }
    /*
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(fond);
        g.drawOval(0, 0, 50, 50);
    }
    */
    public JPanel getMessageCentre1() {
        return messageCentre1;
    }
    public JPanel getMessageCentre2() {
        return messageCentre2;
    } 

}

解決方案?

我知道paintComponentpaint有一些東西,但是我試圖了解它們是如何工作的,我試圖在上面的鏈接中實現解決方案,但我無法實現。

因此,如果您仍然在這里,這就是我確切想要做的:我想在JPanel niveauBase上應用一種顏色,因為所有內容都將放置在該面板上。 因此,如果可以在上面塗上顏色,則不必在每個面板上都塗上顏色。

我想我已經解釋了所有內容,但隨時可以要求更多的解釋或細節。

親愛的StackOverflowers,謝謝您的寶貴時間!

  • 將主內容窗格JPanel的背景設置為所需的任何顏色Color.BLUE
  • 通過對所有主JPanel調用setOpaque(false)使它們在主GUI中顯示為不透明(盡管我確實很難做到“此處提出的解決方案”都沒有告訴您這一點。這將允許背景顏色)要顯示的contentPane。

還有一些建議:

  • 盡管null布局和setBounds()似乎是Swing新手喜歡創建復雜GUI的最簡單和最佳方法,但您創建的Swing GUI越多,使用它們時將遇到的困難就越大。 當GUI調整大小時,它們不會重新調整組件的大小;它們是需要增強或維護的皇家女巫;放置在滾動窗格中時,它們會完全失敗;在所有平台或與原始分辨率不同的屏幕分辨率下查看時,它們看起來都是令人毛骨悚然的。

例如,一些帶有藍色的代碼通過以下方式顯示:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
//!! import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextPane;

public class Simplicity {

    public Simplicity() {

    }

    public static void main(String[] args) {

        Body fen = new Body();
        fen.setVisible(true);

    }

}

class Body extends JFrame{

   private static final long serialVersionUID = 1L;

   private static final int FRAME_WIDTH = 250;
   private static final int FRAME_HEIGHT = 400;

   private static JPanel niveauBase = new JPanel(); // Basic panel -> everything will go on it
   private static JPanel infosReseauxHeure = new JPanel(); // Panel fixed
   private JPanel panelChangeable = new JPanel(); // Scrolling panel

   // up of page info
   private static JTextPane infos = new JTextPane();

   // Creation of the different pages (panels)
   private Tutoriel tuto;

   public Body() {
       super();
       buildFrame();
       this.buildContentPane();
       this.setContentPane(niveauBase);
   }
   public void buildFrame() {
       this.setTitle("Simplicity");
       this.setResizable(false);
       this.setSize(FRAME_WIDTH, FRAME_HEIGHT);
       this.setLocationRelativeTo(null);
       this.setDefaultCloseOperation(EXIT_ON_CLOSE);
       this.getContentPane().setBackground(new Color(255, 105, 86));

       // toutes les dimensions sont en relatif par rapport à la taille de la JFrame. de fait, en changeant celle-ci, on peut adapter l'interface à différents écrans      

       // on met en place ce qui ne changera jamais d'un panel à l'autre --> les infos de base et le support
           // support
       niveauBase.setLayout(null);
       niveauBase.setBackground(Color.blue); //!!

           // basic info
       infosReseauxHeure.setOpaque(false);
       infosReseauxHeure.setBounds(0, 0, this.getWidth(), (int)(0.12*this.getHeight()));
       infosReseauxHeure.setLayout(new GridLayout(0,1));
       infosReseauxHeure.add(infos);

           // adding the text
       niveauBase.add(infosReseauxHeure);

       // defining the limits of the changing Panel, bc they are always the same
       panelChangeable.setOpaque(false);
       panelChangeable.setLayout(null);
       // My first try of changing the background 
//     panelChangeable.setBackground(new Color(255, 105, 86));
       panelChangeable.setBounds(0, infosReseauxHeure.getHeight(), this.getWidth(), (int) (0.88*this.getHeight()));
       niveauBase.add(panelChangeable);

       // the basic frame, which will never change, is ready.

   }
   public void buildContentPane() {

       // Instantiation of the pages

       tuto = new Tutoriel(this);
       /*
       ac1 = new Accueil1(this);
       ac2 = new Accueil2(this);
       med1 = new Medias1(this);
       multimed1 = new Multimedias1(this);
       out1 = new Outils1(this);
       out2 = new Outils2(this);
       jeux1 = new Jeux1(this);
       jeux2 = new Jeux2(this);
       socInt1 = new SocialInternet1(this);
       socInt2 = new SocialInternet2(this);

       appeler = new Appeler(this);
       urgences = new Urgences(this);

       appelEnCours = new AppelEnCours(this);
       demarrageApp = new DemarrageApp(this);
       enConstruction = new EnConstruction(this);
       */

       // Tutoriel is the first to be displayed
       CaractsPanelChangeant(tuto);
       panelChangeable.add(tuto);

       // all the other panels have the same displaying characteristics
       // navigating panels
       /*
       addPanels(ac1);
       addPanels(ac2);
       addPanels(med1);
       addPanels(multimed1);
       addPanels(out1);
       addPanels(out2);
       addPanels(jeux1);
       addPanels(jeux2);
       addPanels(socInt1);
       addPanels(socInt2);

       // applications panels
       addPanels(appeler);
       addPanels(urgences);

       // diverse panels
       addPanels(appelEnCours);
       addPanels(demarrageApp);
       addPanels(enConstruction);
       */
   }
   // fixating the same size and origins for all the panels
   public void CaractsPanelChangeant(JPanel a) {

       a.setBounds(0, 0, panelChangeable.getWidth(), panelChangeable.getHeight());
   }
   // regroup the same actions for the panels : bounds, adding and invisible
   public void addPanels(JPanel a) {
       CaractsPanelChangeant(a);
       panelChangeable.add(a);
       a.setVisible(false);
       /*
        * I tried to change the background here too
       a.setOpaque(true);
       a.setBackground(new Color(255, 105, 86));
       */
   }

   // JPanel getters
   public static int getInfosReseauxHeureHeight() {
       return infosReseauxHeure.getHeight();
   }
   /*
   public Accueil1 getAc1() {
       return ac1;
   }
   public Accueil2 getAc2() {
       return ac2;
   }
   public Medias1 getMed1() {
       return med1;
   }
   public Multimedias1 getMultimed1() {
       return multimed1;
   }
   public Outils1 getOut1() {
       return out1;
   }
   public Outils2 getOut2() {
       return out2;
   }
   public Jeux1 getJeux1() {
       return jeux1;
   }
   public Jeux2 getJeux2() {
       return jeux2;
   }
   public SocialInternet1 getSocInt1() {
       return socInt1;
   }
   public SocialInternet2 getSocInt2() {
       return socInt2;
   }

   public Appeler getAppeler() {
       return appeler;
   }
   public Urgences getUrgences() {
       return urgences;
   }
   public AppelEnCours getAppelEnCours() {
       return appelEnCours;
   }
   public DemarrageApp getDemarrageApp() {
       return demarrageApp;
   }
   public EnConstruction getEnConstruction() {
       return enConstruction;
   }
   */
   public Tutoriel getTuto() {
       return tuto;
   }

   // other getters
   public static int getFrameWidth() {
       return FRAME_WIDTH;
   }
   public static int getFrameHeight() {
       return FRAME_HEIGHT;
   }
   public JPanel getPanelChangeable() {
       return panelChangeable;
   }

   // setters
   public void setPanelChangeable(JPanel a) {
       this.panelChangeable = a;
   }
   /*
   public void switchAccueil(){
       ac1.setVisible(!ac1.isVisible());
       ac2.setVisible(!ac2.isVisible());
   }
   */
}

class Tutoriel extends JPanel {

   private static final long serialVersionUID = 1L;

   private Body body;

   // panels
   private JPanel tout;
   private JPanel centre;
   private JPanel messageCentre1;
   private JPanel messageCentre2;

   // buttons
   private JButton ok1;
   private JButton ok2;

   private JLabel messageHaut;
   private JLabel tuto1;
   private JLabel FGauche;
   private JLabel FDroite;
   private JLabel tuto2;
   private JCheckBox tutoOff;

   //private Color fond = new Color(255, 105, 86);


   public Tutoriel(Body body) {
       super();
       this.body = body;
       buildTuto();
       setOpaque(false);
   }
   public void buildTuto() {

       this.setLayout(new GridLayout(0,1));
       tout = new JPanel();
       tout.setOpaque(false); //!!
       tout.setLayout(new BorderLayout(0,0));
       this.add(tout);

       // tutorial announce
       messageHaut = new JLabel("TUTORIEL");
       messageHaut.setForeground(new Color(0, 0, 0));
       messageHaut.setFont(new Font("Arial", Font.BOLD, 20));
       messageHaut.setHorizontalAlignment(JLabel.CENTER);
       tout.add(messageHaut, BorderLayout.NORTH);

       // central panel, with the tutorial
       centre = new JPanel();
       centre.setOpaque(false);
       centre.setLayout(null);
       tout.add(centre, BorderLayout.CENTER);

       // first tutorial
       messageCentre1 = new JPanel();
       messageCentre1.setLayout(null);
       messageCentre1.setOpaque(false);
       messageCentre1.setBounds(0, 0, 250, 300);
       centre.add(messageCentre1);

       tuto1 = new JLabel("<html>POUR NAVIGUER ENTRE<br>LES DIFFÉRENTES PAGES<br>UTILISER LES FLÈCHES</html>");
       tuto1.setForeground(new Color(0, 0, 0));
       tuto1.setFont(new Font("Arial", Font.BOLD, 16));
       tuto1.setHorizontalAlignment(JLabel.CENTER);
       tuto1.setVerticalAlignment(JLabel.CENTER);
       tuto1.setBounds(20, -10, 200, 150);
       messageCentre1.add(tuto1);

       // arrows
       FGauche = new JLabel("G");
       //!! FGauche.setIcon(new ImageIcon(getClass().getResource("/img/left-blue.png")));
       FGauche.setBackground(new Color(255, 153, 0));
       FGauche.setBounds(80, 150, 40, 40);
       messageCentre1.add(FGauche);

       FDroite = new JLabel("D");
       //!! FDroite.setIcon(new ImageIcon(getClass().getResource("/img/right-blue.png")));
       FDroite.setBackground(new Color(255, 153, 0));
       FDroite.setBounds(140, 150, 40, 40);
       messageCentre1.add(FDroite);

       // OK button, first tutorial
       ok1 = new JButton(new SelectAction(body));
       ok1.setActionCommand("tuto1");
       ok1.setText("OK");
       ok1.setMargin(new Insets(5, 5, 5, 5));
       ok1.setForeground(new Color(0, 0, 0));
       ok1.setBackground(new Color(255, 153, 0));
       ok1.setBounds(105, 230, 40, 40);
       messageCentre1.add(ok1);

       // second tutorial
       messageCentre2 = new JPanel();
       messageCentre2.setOpaque(false);
       messageCentre2.setLayout(null);
       messageCentre2.setBounds(0, 0, 250, 300);
       centre.add(messageCentre2);

       tuto2 = new JLabel("<html>POUR RETOURNER<br>A L'ACCUEIL, APPUYER<br>SUR LA TOUCHE G</html>");
       tuto2.setForeground(new Color(0, 0, 0));
       tuto2.setFont(new Font("Arial", Font.BOLD, 16));
       tuto2.setHorizontalAlignment(JLabel.CENTER);
       tuto2.setVerticalAlignment(JLabel.CENTER);
       tuto2.setBounds(20, -10, 200, 150);
       messageCentre2.add(tuto2);

       // OK button, second tutorial
       ok2 = new JButton(new SelectAction(body));
       ok2.setActionCommand("tuto2");
       ok2.setText("OK");
       ok2.setMargin(new Insets(5, 5, 5, 5));
       ok2.setForeground(new Color(0, 0, 0));
       ok2.setBackground(new Color(255, 153, 0));
       ok2.setBounds(105, 230, 40, 40);
       messageCentre2.add(ok2);

       // visibility of tutorial panels
       //messageCentre1.setVisible(false);
       messageCentre2.setVisible(false);

       // deactivate the tutorial (and reach the main page)
       tutoOff = new JCheckBox(new SelectAction(body));
       tutoOff.setActionCommand("tutoOff");
       tutoOff.setText("NE PLUS AFFICHER AU DÉMARRAGE");
       tutoOff.setForeground(new Color(0, 0, 0));
       tout.add(tutoOff, BorderLayout.SOUTH);
   }
   /*
   @Override
   public void paintComponent(Graphics g) {
       super.paintComponent(g);
       g.setColor(fond);
       g.drawOval(0, 0, 50, 50);
   }
   */
   public JPanel getMessageCentre1() {
       return messageCentre1;
   }
   public JPanel getMessageCentre2() {
       return messageCentre2;
   } 

}

// !! hadd to add this to get code to compile!
class SelectAction extends AbstractAction {
   private Body body;

   public SelectAction(Body body) {
      this.body = body;
   }

   @Override
   public void actionPerformed(ActionEvent evt) {
      // TODO put some junk in here!

   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM