2016-01-20 80 views
-2

我想知道是否可以通過這種方式來重新分配我的代碼,以刪除幾乎相同的語句的重複,禁止變量名稱以及使用方法初始化的內容。這是有問題的代碼:Java swing的代碼重構

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Container; 
import java.awt.GridLayout; 

import javax.swing.AbstractButton; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

@SuppressWarnings("serial") 
public class Menu extends JFrame { 
    private JButton jbtChoc1 = new JButton("cross.gif"); 
    private JButton jbtChoc2 = new JButton("nought.gif"); 
    private JButton jbtChoc3 = new JButton("cross.gif"); 
    private JButton jbtChoc4 = new JButton("nought.gif"); 
    private JButton jbtChoc5 = new JButton("cross.gif"); 
    private JButton jbtChoc6 = new JButton("nought.gif"); 
    private JLabel foodLabelChoice = new JLabel("Main Dishes"); 
    private ImageIcon food1Image = new ImageIcon("cross.gif"); 
    private ImageIcon food2Image = new ImageIcon("nought.gif"); 
    private ImageIcon food3Image = new ImageIcon("cross.gif"); 
    private ImageIcon food4Image = new ImageIcon("nought.gif"); 
    private ImageIcon food5Image = new ImageIcon("cross.gif"); 
    private ImageIcon food6Image = new ImageIcon("nought.gif"); 

    /** 
    * Constructor for the Menu. 
    */ 

    public Menu() { 
    Container cont = getContentPane(); 
    cont.setLayout(new BorderLayout(5, 5));; 
    cont.setBackground(Color.white); 

    cont.add(foodLabelChoice, BorderLayout.NORTH); 


    JPanel girdSetup = new JPanel(new GridLayout(2, 3, 5, 5)); 
    jbtChoc1.setIcon(food1Image); 
    girdSetup.add(jbtChoc1); 
    jbtChoc1.setVerticalTextPosition(AbstractButton.BOTTOM); 
    jbtChoc1.setHorizontalTextPosition(AbstractButton.CENTER); 

    jbtChoc2.setIcon(food2Image); 
    girdSetup.add(jbtChoc2); 
    jbtChoc2.setVerticalTextPosition(AbstractButton.BOTTOM); 
    jbtChoc2.setHorizontalTextPosition(AbstractButton.CENTER); 

    jbtChoc3.setIcon(food3Image); 
    girdSetup.add(jbtChoc3); 
    jbtChoc3.setVerticalTextPosition(AbstractButton.BOTTOM); 
    jbtChoc3.setHorizontalTextPosition(AbstractButton.CENTER); 

    jbtChoc4.setIcon(food4Image); 
    girdSetup.add(jbtChoc4); 
    jbtChoc4.setVerticalTextPosition(AbstractButton.BOTTOM); 
    jbtChoc4.setHorizontalTextPosition(AbstractButton.CENTER); 

    jbtChoc5.setIcon(food5Image); 
    girdSetup.add(jbtChoc5); 
    jbtChoc5.setVerticalTextPosition(AbstractButton.BOTTOM); 
    jbtChoc5.setHorizontalTextPosition(AbstractButton.CENTER); 

    jbtChoc6.setIcon(food6Image); 
    girdSetup.add(jbtChoc6); 
    jbtChoc6.setVerticalTextPosition(AbstractButton.BOTTOM); 
    jbtChoc6.setHorizontalTextPosition(AbstractButton.CENTER); 

    cont.add(girdSetup, BorderLayout.CENTER); 
    } 

    /** 
    * Main method for test. 
    * 
    * @param args Initial setup. 
    */ 

    public static void main(String[] args) { 
    Menu frame = new Menu(); 
    frame.setTitle("Menu"); 
    frame.setSize(950, 400); 
    frame.setVisible(true); 
    frame.setLocationRelativeTo(null); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    } 
} 

我想刪除在構造函數中的JButton,並ImageIcon的聲明及其設置的重複,讓我簡單地調用一個方法,而不是代碼複製/粘貼。

+0

您已將此確切問題發佈在[codereview](http://codereview.stackexchange.com/questions/117369/code-refactoring-for-java-swing)上的正確位置。 StackOverflow不僅僅是代碼評論的地方。 –

回答

2

你可以做到這一點通過包裝他們的陣列,像這樣:

private static final String[] imageNames = {"cross.gif", 
              "nought.gif", 
              "cross.gif", 
              "nought.gif", 
              "cross.gif", 
              "nought.gif"}; 
private JButton[] jbtChocs = new JButton[imageNames.length]; 
private ImageIcon[] foodImages = new ImageIcon[imageNames.length]; 

public Menu() { 
    /* ... */ 
    for(int i = 0; i < imageNames.length; i++){ 
     jbtChocs[i] = new JButton(imageNames[i]); 
     foodImages[i] = new ImageIcon(imageNames[i]); 
     jbtChocs[i].setIcon(foodImages[i]); 
     girdSetup.add(jbtChocs[i]); 
     jbtChocs[i].setVerticalTextPosition(AbstractButton.BOTTOM); 
     jbtChocs[i].setHorizontalTextPosition(AbstractButton.CENTER); 
    } 
} 

由於行爲是所有的人都一樣,這可以讓你簡單地遍歷每個對象,適用於每一個相同的動作一。