我怎样才能简单复制我的代码?

时间:2014-02-26 22:34:36

标签: java swing

我有以下代码;我希望它也希望“动作执行”中的代码在它悬停时发生。而不是再次复制和粘贴代码并且有两倍的次数,有没有办法轻松设置它?

代码:

private void btnGreenActionPerformed(java.awt.event.ActionEvent evt) {                                         
    Color btnGrn = new Color(159, 191, 143);          //Sets the colour to a class
    Color txtGrn = new Color(201, 255, 191);          //Sets the colour to a class
    Color txtGry = new Color(89, 89, 89);             //Sets the colour to a class
    this.getContentPane().setBackground(new java.awt.Color(127,191,95)); //Sets background color to green
    btnConvert.setBackground(btnGrn);                 //Changes the colors to green
    btnReset.setBackground(btnGrn);                   //Changes the colors to green
    btnClose.setBackground(btnGrn);                   //Changes the colors to green
    btnInfo.setBackground(btnGrn);                    //Changes the colors to green
    txtIncome.setBackground(txtGrn);                  //Changes the colors to green
    txtPayable.setBackground(txtGrn);                 //Changes the colors to green
    txtStatus.setBackground(txtGrn);                  //Changes the colors to green
    txtIncome.setForeground(txtGry);                  //Changes the colors to grey
    txtPayable.setForeground(txtGry);                 //Changes the colors to grey
    txtStatus.setForeground(txtGry);                  //Changes the colors to grey
}                                        

注意:除了颜色值之外,我有7个按钮都是相同的。

3 个答案:

答案 0 :(得分:3)

我会在你的面板类中添加一个内部类,来检查颜色,然后创建与你有颜色方案一样多的实例,包括你喜欢的任何颜色。这样的事情。

顺便说一句,这都在你的小组课程中。我并没有将它设想为自己的.java文件中的独立类;因为它完全受制于面板的特性。一个更可重用的版本看起来会有所不同。

private class ButtonColorScheme {
    final Color paneBackground;
    final Color buttonBackground;
    final Color textBackground;
    final Color textForeground;

    ButtonColorScheme(Color paneBackground, Color buttonBackground, Color textBackground, Color textForeground) {
        this.paneBackground = paneBackground;
        this.buttonBackground = buttonBackground;
        this.textBackground = textBackground;
        this.textForeground = textForeground;
    }

    void apply() {
        getContentPane().setBackground(paneBackground);
        btnConvert.setBackground(buttonBackground);
        btnReset.setBackground(buttonBackground);
        btnClose.setBackground(buttonBackground);
        btnInfo.setBackground(buttonBackground);
        txtIncome.setBackground(textBackground);
        txtPayable.setBackground(textBackground);
        txtStatus.setBackground(textBackground);
        txtIncome.setForeground(textForeground);
        txtPayable.setForeground(textForeground);
        txtStatus.setForeground(textForeground);
    }
}

private final ButtonColorScheme greenAndGrey = new ButtonColorScheme(
    new Color(127,191,95), new Color(159, 191, 143), new Color(201, 255, 191), new Color(89, 89, 89));     

private final ButtonColorScheme redAndBlack = new ButtonColorScheme(
    new Color(191,120,95), new Color(202, 160, 143), new Color(255, 180, 191), Color.BLACK);

public void btnGreenActionPerformed(ActionEvent evt){
   greenAndGrey.apply();
}

public void btnRedActionPerformed(ActionEvent evt){
   redAndBlack.apply();
}

答案 1 :(得分:1)

在这种情况下,方法非常有用。将此代码放入方法中 添加所需的任何参数(例如,您可能决定将这些值转换为 159,191,143等进入方法的参数,称为r,g,b)。然后
只需使用您需要的参数调用您的方法(例如,r = 166,g = 202,b = 192)。

答案 2 :(得分:1)

只需创建一个方法,

private void setWidgetColors(Color ... colours){
    int i=0;
    Color btnGrn = i++ <= colours.length?colours[i-1]:new Color(159, 191, 143);          //Sets the colour to a class
    Color txtGrn = i++ <= colours.length?colours[i-1]:new Color(201, 255, 191);          //Sets the colour to a class
    Color txtGry = i++ <= colours.length?colours[i-1]:new Color(89, 89, 89);       

         //Sets the colour to a class
    this.getContentPane().setBackground(i++ <= colours.length ? colours[3] : new Color(127,191,95)); //Sets background color to green
    btnConvert.setBackground(btnGrn);                 //Changes the colors to green
    btnReset.setBackground(btnGrn);                   //Changes the colors to green
    btnClose.setBackground(btnGrn);                   //Changes the colors to green
    btnInfo.setBackground(btnGrn);                    //Changes the colors to green
    txtIncome.setBackground(txtGrn);                  //Changes the colors to green
    txtPayable.setBackground(txtGrn);                 //Changes the colors to green
    txtStatus.setBackground(txtGrn);                  //Changes the colors to green
    txtIncome.setForeground(txtGry);                  //Changes the colors to grey
    txtPayable.setForeground(txtGry);                 //Changes the colors to grey
    txtStatus.setForeground(txtGry);                  //Changes the colors to grey
}

然后

private void btnGreenActionPerformed(ActionEvent evt){
    Color btnGrn = new Color(159, 191, 143);          //Sets the colour to a class
    Color txtGrn = new Color(201, 255, 191);          //Sets the colour to a class
    Color txtGry = new Color(89, 89, 89);
    Color backgroundColor = new Color(127,191,95); 
    setWidgetColors(btnGrn,txtGrn,txtGry,backgroundColor);
}