2013-04-19 18 views
1
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.*; 
import java.awt.*; 

public class filecabinet extends JFrame implements ActionListener { 

    private String folder = ""; 
    //create buttons 

    private JButton a = new JButton("A"); 
    private JButton b = new JButton("B"); 
    private JButton c = new JButton("C"); 
    private JButton d = new JButton("D"); 
    private JButton e = new JButton("E"); 
    private JButton f = new JButton("F"); 
    private JButton g = new JButton("G"); 
    private JButton h = new JButton("H"); 
    private JButton i = new JButton("I"); 
    private JButton j = new JButton("J"); 
    private JButton k = new JButton("K"); 
    private JButton l = new JButton("L"); 
    private JButton m = new JButton("M"); 
    private JButton n = new JButton("N"); 
    private JButton o = new JButton("O"); 
    private JButton p = new JButton("P"); 
    private JButton q = new JButton("Q"); 
    private JButton r = new JButton("R"); 
    private JButton s = new JButton("S"); 
    private JButton t = new JButton("T"); 
    private JButton u = new JButton("U"); 
    private JButton v = new JButton("V"); 
    private JButton w = new JButton("W"); 
    private JButton x = new JButton("X"); 
    private JButton y = new JButton("Y"); 
    private JButton z = new JButton("Z"); 
    //create panels 
    private JPanel aF = new JPanel(); 
    private JPanel gL = new JPanel(); 
    private JPanel mR = new JPanel(); 
    private JPanel sX = new JPanel(); 
    private JPanel yZ = new JPanel(); 
    private JPanel readout = new JPanel(); 
    private JLabel notify = new JLabel("You have selected folder " + folder); 

    public void ComposePane(){ 
     //set buttons to panels 
     aF.add(a); 
     aF.add(b); 
     aF.add(c); 
     aF.add(d); 
     aF.add(e); 
     aF.add(f); 
     //set layout of panels 
     aF.setLayout(new GridLayout(2,3)); 
     gL.add(g); 
     gL.add(h); 
     gL.add(i); 
     gL.add(j); 
     gL.add(k); 
     gL.add(l); 
     gL.setLayout(new GridLayout(2,3)); 
     mR.add(m); 
     mR.add(n); 
     mR.add(o); 
     mR.add(p); 
     mR.add(q); 
     mR.add(r); 
     mR.setLayout(new GridLayout(2,3)); 
     sX.add(s); 
     sX.add(t); 
     sX.add(u); 
     sX.add(v); 
     sX.add(w); 
     sX.add(x); 
     sX.setLayout(new GridLayout(2,3)); 
     yZ.add(y); 
     yZ.add(z); 
     yZ.add(readout); 
     readout.add(notify); 
     yZ.setLayout(new GridLayout(2,3)); 
    } 
    public filecabinet(){ 
     setTitle("File Cabinet"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLayout(new GridLayout(5,1)); 
     ComposePane(); 
     add(aF); 
     add(gL); 
     add(mR); 
     add(sX); 
     add(yZ); 
     addActionListener(); 
    } 


    public void actionPerformed(ActionEvent e) { 
     folder = ((JButton) e.getSource()).getText(); 


    } 
    public void addActionListener(){ 
     a.addActionListener(this); 
     b.addActionListener(this); 
     c.addActionListener(this); 
     d.addActionListener(this); 
     e.addActionListener(this); 
     f.addActionListener(this); 
     g.addActionListener(this); 
     h.addActionListener(this); 
     i.addActionListener(this); 
     j.addActionListener(this); 
     k.addActionListener(this); 
     l.addActionListener(this); 
     m.addActionListener(this); 
     n.addActionListener(this); 
     o.addActionListener(this); 
     p.addActionListener(this); 
     q.addActionListener(this); 
     r.addActionListener(this); 
     s.addActionListener(this); 
     t.addActionListener(this); 
     u.addActionListener(this); 
     v.addActionListener(this); 
     w.addActionListener(this); 
     x.addActionListener(this); 
     y.addActionListener(this); 
     z.addActionListener(this); 

    } 
    public static void main(String[]args){ 
     filecabinet frame = new filecabinet(); 
     final int WIDTH = 600; 
     final int HEIGHT = 600; 
     frame.setSize(WIDTH, HEIGHT); 
     frame.setVisible(true); 
    } 

} 

我寧願不寫出來,一個偵聽每個按鈕 ,我是遊蕩,如果我能得到包含在按鈕上的文本。我如何獲得JLabel以顯示其按鈕被選中

例如「您選擇了foler X」,X是選中的按鈕。

+6

'我是遊蕩,如果我能剛剛獲得包含在button.'文 - 我不明白的問題。你的ActionListener已經有了這樣的代碼。您仍然需要將ActionListener添加到每個按鈕。 – camickr

+2

'notify.setText(「您選擇了文件夾」+((JButton)e.getSource())。getText());' – MadProgrammer

+3

如果您使用數組和/或集合,您的代碼會簡化得多。這樣做的主要優點是通過最大限度地減少代碼重複,可以使代碼更容易調試和維護。 –

回答

2

對於這一點:

寧願不寫出來,一個偵聽每個按鈕

創建接受JPanel作爲參數,然後迭代中的每個組件JPanel的方法如果組件是JButton

這一個:

I was wandering if I could just get the text contained in the button. 

只需在JLabel中設置值即可。你已經得到了正確的按鈕內容?

所以,完整的代碼是:

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.*; 
import java.awt.*; 

public class filecabinet extends JFrame implements ActionListener { 

    private String folder = ""; 
    // create buttons 

    private JButton a = new JButton("A"); 
    private JButton b = new JButton("B"); 
    private JButton c = new JButton("C"); 
    private JButton d = new JButton("D"); 
    private JButton e = new JButton("E"); 
    private JButton f = new JButton("F"); 
    private JButton g = new JButton("G"); 
    private JButton h = new JButton("H"); 
    private JButton i = new JButton("I"); 
    private JButton j = new JButton("J"); 
    private JButton k = new JButton("K"); 
    private JButton l = new JButton("L"); 
    private JButton m = new JButton("M"); 
    private JButton n = new JButton("N"); 
    private JButton o = new JButton("O"); 
    private JButton p = new JButton("P"); 
    private JButton q = new JButton("Q"); 
    private JButton r = new JButton("R"); 
    private JButton s = new JButton("S"); 
    private JButton t = new JButton("T"); 
    private JButton u = new JButton("U"); 
    private JButton v = new JButton("V"); 
    private JButton w = new JButton("W"); 
    private JButton x = new JButton("X"); 
    private JButton y = new JButton("Y"); 
    private JButton z = new JButton("Z"); 
    // create panels 
    private JPanel aF = new JPanel(); 
    private JPanel gL = new JPanel(); 
    private JPanel mR = new JPanel(); 
    private JPanel sX = new JPanel(); 
    private JPanel yZ = new JPanel(); 
    private JPanel readout = new JPanel(); 
    private JLabel notify = new JLabel("You have selected folder " + folder); 

    public void ComposePane() { 
     // set buttons to panels 
     aF.add(a); 
     aF.add(b); 
     aF.add(c); 
     aF.add(d); 
     aF.add(e); 
     aF.add(f); 
     // set layout of panels 
     aF.setLayout(new GridLayout(2, 3)); 
     gL.add(g); 
     gL.add(h); 
     gL.add(i); 
     gL.add(j); 
     gL.add(k); 
     gL.add(l); 
     gL.setLayout(new GridLayout(2, 3)); 
     mR.add(m); 
     mR.add(n); 
     mR.add(o); 
     mR.add(p); 
     mR.add(q); 
     mR.add(r); 
     mR.setLayout(new GridLayout(2, 3)); 
     sX.add(s); 
     sX.add(t); 
     sX.add(u); 
     sX.add(v); 
     sX.add(w); 
     sX.add(x); 
     sX.setLayout(new GridLayout(2, 3)); 
     yZ.add(y); 
     yZ.add(z); 
     yZ.add(readout); 
     readout.add(notify); 
     yZ.setLayout(new GridLayout(2, 3)); 
    } 

    public filecabinet() { 
     setTitle("File Cabinet"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLayout(new GridLayout(5, 1)); 
     ComposePane(); 
     add(aF); 
     add(gL); 
     add(mR); 
     add(sX); 
     add(yZ); 
     addActionListener(aF); 
     addActionListener(gL); 
     addActionListener(mR); 
     addActionListener(sX); 
     addActionListener(yZ); 
    } 

    public void actionPerformed(ActionEvent e) { 
     folder = ((JButton) e.getSource()).getText(); 
      // set the label with folder value 
     notify.setText("You have selected folder " + folder); 

    } 

    // Attach event handler to each JButton in JPanel 
    public void addActionListener(JPanel panel) { 
     Component[] components = panel.getComponents(); 
     for (Component component : components) { 
      if (component instanceof JButton) { 
       ((JButton) component).addActionListener(this); 
      } 
     } 
    } 

    public static void main(String[] args) { 
     filecabinet frame = new filecabinet(); 
     final int WIDTH = 600; 
     final int HEIGHT = 600; 
     frame.setSize(WIDTH, HEIGHT); 
     frame.setVisible(true); 
    } 

} 
+0

爲什麼不寫一個方法需要一個字母和一個ActionListener,並返回一個與動作監聽器關聯的按鈕? – ditkin

+0

謝謝你的幫助。我只是在學習。我讚賞你的時間。 – user2217794

1

你也可以繼承JButton並寫出它接受作爲參數參照動作偵聽器,您JFrame子類的構造函數。

例如,你的子類可能僅僅是:

public class YourButton extends JButton { 
    public YourButton(String title, ActionListener listener) { 
     super(title); 
     this.addActionListener(listener); 
    } 
} 

所以不是:

private JButton a = new JButton("A"); 

你會打電話:

private YourButton a = new YourButton("A", this); 

此外,正如其他人的建議,你的actionPerformed(ActionEvent e)方法要求您更新您的通知JLabel實例反映所選按鈕的標題。

希望有幫助!

2
  • 快速查看您的代碼,將您的代碼降低到這個長度。想象一下,如果你給它更多的洞察力,你可以實現什麼。此外,當你在你的代碼中看到,你一次又一次地重複一些 行同樣的事情,你可以確實想到一個 模式,可以以合法的方式爲你完成, 更少的按鍵。

試試你這個修改後的代碼:-)

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.*; 
import java.awt.*; 

public class Filecabinet extends JFrame implements ActionListener { 

    private String folder = ""; 
    //create buttons 
    private JButton[] button = new JButton[26]; 

    //create panels 
    private JPanel aF = new JPanel(); 
    private JPanel gL = new JPanel(); 
    private JPanel mR = new JPanel(); 
    private JPanel sX = new JPanel(); 
    private JPanel yZ = new JPanel(); 
    private JPanel readout = new JPanel(); 
    private JLabel notify = new JLabel("You have selected folder " + folder); 

    public void ComposePane(){ 
     init_Buttons(button); 
     //set buttons to panels 
     for (int i = 0; i < 6; i++) 
      aF.add(button[i]); 
     //set layout of panels 
     aF.setLayout(new GridLayout(2,3)); 
     for (int i = 6; i < 12; i++) 
      gL.add(button[i]); 
     gL.setLayout(new GridLayout(2,3)); 
     for (int i = 12; i < 18; i++) 
      mR.add(button[i]); 
     mR.setLayout(new GridLayout(2,3)); 
     for (int i = 18; i < 24; i++) 
      sX.add(button[i]); 
     sX.setLayout(new GridLayout(2,3)); 
     for (int i = 24; i < 26; i++) 
      yZ.add(button[i]); 
     yZ.add(readout); 
     readout.add(notify); 
     yZ.setLayout(new GridLayout(2,3)); 
    } 

    private void init_Buttons(JButton[] button) 
    { 
     int value = 65; 
     for (int i = 0; i < button.length; i++) 
     { 
      button[i] = new JButton(Character.toString((char) value++)); 
      button[i].addActionListener(this); 
     } 
    } 
    public Filecabinet(){ 
     setTitle("File Cabinet"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLayout(new GridLayout(5,1)); 
     ComposePane(); 
     add(aF); 
     add(gL); 
     add(mR); 
     add(sX); 
     add(yZ); 
    } 


    public void actionPerformed(ActionEvent e) { 
     folder = ((JButton) e.getSource()).getText(); 

     notify.setText(folder); 
    } 

    public static void main(String[]args){ 

     SwingUtilities.invokeLater(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       Filecabinet frame = new Filecabinet(); 
       final int WIDTH = 600; 
       final int HEIGHT = 600; 
       //frame.setSize(WIDTH, HEIGHT); 
       frame.pack(); 
       frame.setVisible(true); 
      } 
     });   
    } 

} 
3

再次,無論是使用陣列和/或一個for循環來鞏固你的代碼擺脫代碼冗餘。例如:

import java.awt.BorderLayout; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.*; 

@SuppressWarnings("serial") 
public class FileCabinet2 extends JPanel { 
    public static final char FIRST_LETTER = 'A'; 
    public static final char LAST_LETTER = 'Z'; 
    private static final float lARGE_FONT_POINTS = 32f; 
    private static final int GRID_COLS = 3; 
    private JLabel chosenLabel = new JLabel(); 

    public FileCabinet2() { 
     JPanel letterPanel = new JPanel(new GridLayout(0, GRID_COLS)); 
     ActionListener btnListener = new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent evt) { 
      chosenLabel.setText(evt.getActionCommand()); 
     } 
     }; 
     for (char c = FIRST_LETTER; c <= LAST_LETTER; c++) { 
     String buttonTitle = String.valueOf(c); 
     JButton button = new JButton(buttonTitle); 
     setFontPoints(button, lARGE_FONT_POINTS); 
     letterPanel.add(button); 

     button.addActionListener(btnListener); 
     } 

     JLabel selectionLabel = new JLabel("Selection: ", SwingConstants.RIGHT); 
     setFontPoints(selectionLabel, lARGE_FONT_POINTS); 
     setFontPoints(chosenLabel, lARGE_FONT_POINTS); 


     JPanel bottomPanel = new JPanel(new GridLayout(1, 0)); 
     bottomPanel.add(selectionLabel); 
     bottomPanel.add(chosenLabel); 
     bottomPanel.setBorder(BorderFactory.createEtchedBorder()); 

     setLayout(new BorderLayout()); 
     add(letterPanel, BorderLayout.CENTER); 
     add(bottomPanel, BorderLayout.PAGE_END); 
    } 

    private void setFontPoints(JComponent jComp, float points) { 
     jComp.setFont(jComp.getFont().deriveFont(points)); 
    } 

    private static void createAndShowGui() { 
     FileCabinet2 mainPanel = new FileCabinet2(); 

     JFrame frame = new JFrame("File Cabinet"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
     }); 
    } 
} 

現在,如果你想改變多少鍵列有所有你需要做的是改變一個常數,GRID_COLS,和相同的字體的大小,只是改變LARGE_FONT_POINTS。

enter image description here

相關問題