2013-05-10 45 views
1

我有一個JButton數組的問題。我想在標籤框中顯示按鈕的值。例如,如果我點擊「1」按鈕,我想在JLabel框中看到「1」,然後點擊「4」在JLabel框中看到「14」。JLabel將不會顯示JButton數組值

這是我的代碼:使用

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Font; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.AbstractButton; 
import javax.swing.BorderFactory; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.border.Border; 
import javax.swing.border.TitledBorder; 

public class FormSimulateur extends JFrame implements ActionListener { 
    JPanel panneau = new JPanel(); 
    JPanel clavier, pannAffichage, pannAppelRacc, pannDureeTarif; 
    String [] tab = {"1", "2", "3","4","5","6","7","8","9","0","*","#"}; 
    JButton [] btn_chiffre = new JButton[tab.length]; 
    ListenForButton lfb = new ListenForButton(); 
    JButton appel, raccrocher; 
    Boolean NumOperateur = false; 
    JLabel affichage, duree, tarif; 
    public FormSimulateur() { 
     setTitle("SIMULATEUR D'APPEL TELEPHONIQUE"); 
     setSize(400,350); 
     setLocationRelativeTo(null); 
     setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     setResizable(false); 
     setVisible(true); 
     //panneau.setBackground(Color.lightGray); 
     setContentPane(panneau); 
     Fenetre(); 

    } 
    private void Fenetre() { 
     //définition et paramètrage de l'écran d'affichage du numéro composé 
     affichage = new JLabel("0"); 
     affichage.setHorizontalAlignment(JLabel.RIGHT); 
     affichage.setPreferredSize(new Dimension(255,30)); 
     affichage.setOpaque(true); 
     affichage.setBackground(Color.white); 
     affichage.setFont(new Font("Cambria Math", Font.BOLD, 28)); 
     affichage.setVisible(true); 
     //définition d'un panneau pour le clavier du téléphone 
     clavier = new JPanel(); 
     clavier.setPreferredSize(new Dimension(200,200)); 
     //définition d'un panneau pour l'affichage de l'écran 
     pannAffichage = new JPanel(); 
     pannAffichage.setPreferredSize(new Dimension(260,40)); 
     pannAffichage.setBackground(Color.WHITE); 
     //création des boutons chiffres 
     for(int i = 0; i < tab.length; i++){ 
      btn_chiffre[i] = new JButton(tab[i]); 
      btn_chiffre[i].addActionListener(lfb); 
      btn_chiffre[i].setPreferredSize(new Dimension(50,40)); 
      clavier.add(btn_chiffre[i]); 
     } 
     //creation du panneau pour les boutons Appel et Raccrocher 
     appel = new JButton(); 
     appel.setPreferredSize(new Dimension(150,40)); 
     appel.setText("Appel"); 
     appel.setBackground(Color.GREEN); 
     appel.setFont(new Font("Arial", Font.BOLD,16)); 
     appel.addActionListener(this); 
     appel.setVisible(true); 
     raccrocher = new JButton(); 
     raccrocher.setPreferredSize(new Dimension(150,40)); 
     raccrocher.setText("Raccrocher"); 
     raccrocher.setBackground(Color.red); 
     raccrocher.setFont(new Font("Arial", Font.BOLD,16)); 
     raccrocher.addActionListener(this); 
     raccrocher.setVisible(true); 
     pannAppelRacc = new JPanel(); 
     pannAppelRacc.setPreferredSize(new Dimension(350,50)); 
     //pannAppelRacc.setBackground(Color.CYAN); 

     //définition d'un panneau pour l'affichage de la duréé de communication et du tarif 
     pannDureeTarif = new JPanel(); 
     pannDureeTarif.setPreferredSize(new Dimension(150,95)); 
     //définition des labels Duree et Tarif 
     duree = new JLabel(); 
     duree.setBackground(Color.WHITE); 
     duree.setPreferredSize(new Dimension(100,30)); 
     duree.setOpaque(true); 
     duree.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
     duree.setFont(new Font("Courier New",Font.BOLD,14)); 
     tarif = new JLabel(); 
     tarif.setBackground(Color.WHITE); 
     tarif.setPreferredSize(new Dimension(100,30)); 
     tarif.setOpaque(true); 
     tarif.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
     tarif.setFont(new Font("Courier New",Font.BOLD,14)); 

     //affichage du panneau 
     pannAffichage.add(affichage); 
     pannAffichage.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
     pannAppelRacc.add(appel); 
     pannAppelRacc.add(raccrocher); 
     pannDureeTarif.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
     pannDureeTarif.setBorder(BorderFactory.createTitledBorder("Coût et durée")); 
     pannDureeTarif.add(duree); 
     pannDureeTarif.add(tarif); 
     //pannAffichage.setBorder(BorderFactory.createTitledBorder("LCD")); 
     panneau.add(pannAffichage, BorderLayout.NORTH); 
     panneau.add(clavier, BorderLayout.EAST); 
     panneau.add(pannDureeTarif, BorderLayout.WEST); 
     panneau.add(pannAppelRacc, BorderLayout.SOUTH); 

    } 
    public static void main(String[] args) { 

     FormSimulateur fr = new FormSimulateur(); 
     fr.setVisible(true); 


    } 
    private class ListenForButton implements ActionListener{ 

    public void actionPerformed(ActionEvent e) { 
     //String number = ((JButton) e.getSource()).getText(); 
      String nbre = e.getActionCommand(); 
      int i = 0; 
      do{ 
      if(nbre.equals(tab[i])){ 
       affichage.setText(nbre); 
      } 
      if (!nbre.equals(tab[i])){ 
       nbre = affichage.getText() + nbre; 
      } 
      i++; 
      //affichage.setText(nbre); 
      } 
      while(i < tab.length); 
    } 

} 
    @Override 
    public void actionPerformed(ActionEvent arg0) { 
     // TODO Auto-generated method stub 

    } 
} 
+0

+1 [SSCCE](http://www.sscce.org)。儘管一個更好的問題可能會包含對當前代碼行爲的描述。 – 2013-05-10 12:18:09

回答

0

@DuncanJone響應:

添加第一個變量是更好

public class FormSimulateur extends JFrame implements ActionListener { 

     boolean first=true;  

     ... 

     private class ListenForButton implements ActionListener { 

      public void actionPerformed(ActionEvent e) { 
       if(first){ 
        affichage.setText(e.getActionCommand()); 
        first = false; 
       } 
       else{ 
        affichage.setText(affichage.getText() + e.getActionCommand()); 
       } 
      } 
     } 
    } 
+0

非常感謝ricardo,您是否測試了我的應用程序?在顯示這個號碼呼叫之前,我想加載一個有登記號碼的文件。這個想法是,如果用戶撥打一個號碼並點擊按鈕「Appel」,系統驗證如果一個號碼是文件中的號碼之一?如果沒問題,系統會顯示此消息「已連接...」,否則系統會顯示「Bad number!」 – 2013-05-10 14:56:35

+0

編寫代碼,如果你有問題發佈你的問題,任何人肯定會幫助 – 2013-05-10 15:22:48

+0

第一件事是加載一個文件,所有的電話號碼是錄音機,第二個是讓用戶撥號是號碼,然後單擊按鈕「 APPEL」。當所有號碼都可以的時候,即號碼長度爲9位的號碼是電話屏幕,並且撥打的號碼只是號碼,對於計時觸發和打印使用者打過電話後的時間,我應該計算成本通話時間已過,並將其打印到相應的JLabel中。 – 2013-05-10 16:10:46

2

除非我誤解了你的要求,你不需要一個循環。只是此代碼:

private class ListenForButton implements ActionListener { 

    public void actionPerformed(ActionEvent e) { 
    String nbre = e.getActionCommand(); 
    affichage.setText(affichage.getText() + nbre); 
    } 
} 
+0

是的,不需要循環。除非文本按鈕不是在JLabel上寫入的內容。 – 2013-05-10 12:21:50

+0

好吧,我看到的代碼肯定不需要循環,因爲按鈕是使用選項卡上的文本創建的[] – 2013-05-10 12:23:21

+0

@Duncan Jones +1也許使用JTextField#setEnabled(false)而不是JLabel – mKorbel 2013-05-10 12:34:44