-1

阴影设计师

定制窗帘设计师收取每个窗帘 50 美元的基本费用。此外,某些款式、尺码和颜色的收费如下:

这个程序的目的是创建一个应用程序,允许用户从列表或组合框中选择样式、大小、颜色和阴影数量。应显示总费用。

款式:常规色调:加 0 美元 折叠色调:加 10 美元 罗马色调:加 15 美元

尺寸: 25 英寸宽:加 0 美元 27 英寸宽:加 2 美元 32 英寸宽:加 4 美元 40 英寸宽:加 6 美元

颜色: 自然色:加 5 美元 蓝色:加 0 美元 蓝绿色:加 0 美元 红色:加 0 美元 绿色:加 0 美元

我采用了两种方法,但下面的第一个程序代码存在算法问题,它没有将样式、尺寸和颜色选择的额外成本添加到计算总价格中。我不确定它是否是每个添加的选择,例如总费用 = 样式选择 + 尺寸选择 + 颜色选择。我只是不确定如何让它在代码中计算和使用正确的算法。下面的 shadedesigner.java 代码

此代码之后的第二个代码是第二种方法,其中配置面板控制我相信列表或组合框容器和面板操作到 shadedesignerwindow.java。配置面板被假定为 shaddesignerwindow 程序的实现或接口。以下是我到目前为止的内容,但我不确定如何让配置面板按照他们想要的方式使用 shadedesignerwindow。

下面是我为 shadedesigner 程序构建的代码。一切正常且合规,但如果选择了样式、尺寸和颜色,我无法开发正确的算法来将附加选择添加到阴影的总费用中。重新发布更正 2012 年 4 月 8 日编辑的代码如下。

   *    Filename: ShadeDesigner.java
 *  Programmer: Jamin A. Bishop
 *  Date: 3/31/2012
 * @version 1.00 2012/4/2
 */
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class ShadeDesigner extends JFrame
{
    private String[] styles = {"Regular Shades", "Folding Shades", "Roman Shades"};
    private String[] size = {"25 Inches Wide", "27 Inches Wide",
                              "32 Inches Wide", "40 Inches Wide"};
    private String[] colors = {"Natural", "Blue", "Teal",
                               "Red", "Green"};
    private JLabel banner;// To display a banner
    private JPanel bannerPanel;// To hold the banner
    private JPanel stylesPanel;// 
    private JPanel sizePanel;// 
    private JPanel colorPanel;
    private JPanel buttonPanel;//

    private JList stylesList;
    private JList sizeList;
    private JList colorList;

    private JTextField Styles;
    private JTextField Size;
    private JTextField Color;

    private JButton calcButton;
    private JButton ExitButton; 

    private double totalCharges = 50.00;

    //Constants
    private final int ROWS = 5;
    private final double regularCost = 0.00;//base price for the blinds
    private final double foldingCost = 10.00;//extra cost for folding blinds
    private final double romanCost = 15.00;//extra cost for roman blinds
    private final double twentyfiveInCost = 0.00; //extra cost for 25" blinds
    private final double twentySevenInCost = 2.00;//extra cost for 27" blinds
    private final double thirtyTwoInCost = 4.00;//extra cost for 32" blinds
    private final double fourtyInCost = 6.00;//extra cost for 40" blinds
    private final double naturalColorCost = 5.00;//extra cost for color


    public ShadeDesigner()
    {
        //display a title
        setTitle("Shade Designer");

        // Specify what happens when the close button is clicked.
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Create the banner on a panel and add it to the North region.
        buildBannerPanel();
        add(bannerPanel, BorderLayout.NORTH);

        stylesPanel();
        add(stylesPanel, BorderLayout.WEST);

        sizePanel();
        add(sizePanel, BorderLayout.CENTER);

        colorPanel();
        add(colorPanel, BorderLayout.EAST);

        buttonPanel();
        add(buttonPanel, BorderLayout.SOUTH);

        pack();
        setVisible(true);
    }

    //build the bannerpanel
    private void buildBannerPanel()
    {
        bannerPanel = new JPanel();
        bannerPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        banner = new JLabel("Shade Designer");
        banner.setFont(new Font("SanSerif", Font.BOLD, 24));
        bannerPanel.add(banner);
    }

    //stylepanel    
    private void stylesPanel()
    {
        JLabel styleTitle = new JLabel("Select a Style.");
        stylesPanel = new JPanel();
        stylesPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
        stylesList = new JList (styles);
        stylesList.setVisibleRowCount(ROWS);
        JScrollPane stylesScrollPane = new JScrollPane(stylesList);
        stylesList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        stylesList.addListSelectionListener(new stylesListListener());
        stylesPanel.setLayout(new BorderLayout());
        stylesPanel.add(styleTitle, BorderLayout.NORTH);
        stylesPanel.add(stylesScrollPane, BorderLayout.CENTER);
        Styles = new JTextField (5);
        Styles.setEditable(false);
        //stylesPanel.add(StylesLabel, BorderLayout.CENTER);
        stylesPanel.add(Styles, BorderLayout.SOUTH);
    }
    private class stylesListListener implements ListSelectionListener
    {
        public void valueChanged (ListSelectionEvent e)
        {
            String selection = (String) stylesList.getSelectedValue();
            Styles.setText(selection);
        }
    }

    //size panel
    private void sizePanel()
    {
        JLabel sizeTitle = new JLabel("Select a Size.");
        sizePanel = new JPanel();
        sizePanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
        sizeList = new JList (size);
        sizeList.setVisibleRowCount(ROWS);
        JScrollPane stylesScrollPane = new JScrollPane(sizeList);
        sizeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        sizeList.addListSelectionListener(new sizeListListener());
        sizePanel.setLayout(new BorderLayout());
        sizePanel.add(sizeTitle, BorderLayout.NORTH);
        sizePanel.add(stylesScrollPane, BorderLayout.CENTER);
        //sizeLabel = new JLabel("Style Selected: ");
        Size = new JTextField (5);
        Size.setEditable(false);
        //stylesPanel.add(StylesLabel, BorderLayout.CENTER);
        sizePanel.add(Size, BorderLayout.SOUTH);
    }
    private class sizeListListener implements ListSelectionListener
    {
        public void valueChanged (ListSelectionEvent e)
        {
            String selection = (String) sizeList.getSelectedValue();
            Size.setText(selection);
        }
    }

    //color panel
    private void colorPanel()
    {
        JLabel colorTitle = new JLabel("Select a Color.");
        colorPanel = new JPanel();
        colorPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
        colorList = new JList (colors);
        colorList.setVisibleRowCount(ROWS);
        JScrollPane colorScrollPane = new JScrollPane(colorList);
        colorList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        colorList.addListSelectionListener(new colorListListener());
        colorPanel.setLayout(new BorderLayout());
        colorPanel.add(colorTitle, BorderLayout.NORTH);
        colorPanel.add(colorScrollPane, BorderLayout.CENTER);
        //sizeLabel = new JLabel("Style Selected: ");
        Color = new JTextField (5);
        Color.setEditable(false);
        //stylesPanel.add(StylesLabel, BorderLayout.CENTER);
        colorPanel.add(Color, BorderLayout.SOUTH);
    }
    private class colorListListener implements ListSelectionListener
    {
        public void valueChanged (ListSelectionEvent e)
        {
            String selection = (String) colorList.getSelectedValue();
            Color.setText(selection);
        }
    }

    //button panel
    private void buttonPanel()
    {
        calcButton= new JButton ("Calculate Charges");
        calcButton.addActionListener(new calcButtonListener());
        ExitButton = new JButton ("Exit");
        ExitButton.addActionListener(new ExitButtonListener());
        buttonPanel = new JPanel();
        buttonPanel.add(calcButton);
        buttonPanel.add(ExitButton);    
    }

    private class calcButtonListener implements  ActionListener
    {
        //actionPerformed method parementer e an actionevent object

    public void actionPerformed(ActionEvent e)
    {
             // Create a DecimalFormat object.
             DecimalFormat dollar = new DecimalFormat("#,##0.00");

             // Display the total.
             JOptionPane.showMessageDialog(null, "TotalCharges: $" +
                                                 dollar.format(totalCharges));
          }
       }

       private class ExitButtonListener implements  ActionListener
       {
            public void actionPerformed(ActionEvent e)
            {
                //Exit the applicaiton
                System.exit(0);
            }
       }

    //static void main for the string
    public static void main(String[] args)
       {
          new ShadeDesigner();
       }

}

*   File Name: ConfigPanel.java
 *  Date: 3/26/2012
 *  Programmer: Jamin A. Bishop 
 * @version 1.00 2012/3/22
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;

class ConfigPanel extends JPanel
{
    private JLabel SelectStyleLabel;
    private JLabel SelectSizeLabel;
    private JLabel SelectColorLabel;
    private JPanel StylePricePanel;
    private JPanel SizePricePanel;
    private JPanel ColorPricePanel;
    private JComboBox SelectStyleBox;
    private JComboBox SelectSizeBox;
    private JComboBox SelectColorBox;

    private final int ROWS = 5;
    private final int RegShadeadd = 0.00;
    private final int FoldShadeadd = 10.00;
    private final int RomanShadeadd = 15.00;
    private final int 25inchadd = 0.00;
    private final int 27inchadd = 2.00;
    private final int 32inchadd = 4.00;
    private final int 40inchadd = 6.00;
    private final int Naturaladd = 5.00;
    private final int Blueadd = 0.00;
    private final int Tealadd = 0.00;
    private final int Redadd = 0.00;
    private final int Greenadd = 0.00;

    //create the arrays to hold the values of the combo box's
    private String[] StylePrice = {"Regular Shades", "Folding Shades", "Roman Shades"};
    private String[] SizePrice = {"25 inches Wide", "27 inches Wide", "32 inches Wide", "40 inches Wide"};
    private String[] ColorPrice = {"Nature", "Blue", "Teal", "Red", "Green"};

    // constructor
    public ConfigPanel ()
    {
        //build the panel
        buildStylePricePanel();
        buildSizePricePanel();
        buildColorPricePanel();

        //add the panel to the content pane
        add(StylePricePanel, BorderLayout.WEST);
        add(SizePricePanel, BorderLayout.CENTER);
        add(ColorPricePanel, BorderLayout.EAST);

    }

    //methods to hold the strings
    public double getStylePrice ()
    {
        String selection = (String) StylePrice.getName(StylePrice);
        StylePrice.setText(selection);
    }

    public double getSizePrice ()
    {

    }

    public double getColorPrice ()
    }
}

配置面板应该可以使用下面的代码

*   Filename: ShadeDesignerWindow.java
 *  Programmer: Jamin A. Bishop
 *  Date: 3/31/2012
 * @version 1.00 2012/3/31
 */


public class ShadeDesignerWindow extends JFrame
{
    private JLabel banner;           // To display a banner
    private ConfigPanel configPanel; // To offer various configurations
    private JPanel bannerPanel;      // To hold the banner
    private JPanel buttonPanel;      // To hold the buttons
    private JButton calcButton;      // To calculate total charges
    private JButton exitButton;      // To exit the application

    //constructor

    public ShadeDesignerWindow() 
    {
        //Title Display
        super("Shade Designer");

         // Specify what happens when the close button is clicked.
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      // Create the banner on a panel.
      bannerPanel = new JPanel();
      bannerPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
      banner = new JLabel("Shade Designer");
      banner.setFont(new Font("SanSerif", Font.BOLD, 24));
      bannerPanel.add(banner);

      // Create a configuration panel.
      configPanel = new ConfigPanel();

      // Build the button panel.
      buildButtonPanel();

      // Add the banner and other panels to the content pane.
      add(bannerPanel, BorderLayout.NORTH);
      add(configPanel, BorderLayout.CENTER);
      add(buttonPanel, BorderLayout.SOUTH);

      // Pack and display the window.
      pack();
      setVisible(true);
    }

    //buildButtonPanel method

    private void buildButtonPanel()
   {
      // Create a button to calculate the charges.
      calcButton = new JButton("Calculate Charges");

      // Add an action listener to the button.
      calcButton.addActionListener(new CalcButtonListener());

      // Create a button to exit the application.
      exitButton = new JButton("Exit");

      // Add an action listener to the button.
      exitButton.addActionListener(new ExitButtonListener());

      // Put the buttons in their own panel.
      buttonPanel = new JPanel();
      buttonPanel.add(calcButton);
      buttonPanel.add(exitButton);
   }

   //calcButtonListner and calcbutton component


    private class CalcButtonListener implements ActionListener
    {
        //actionPerformed method

        public void actionPerformed(ActionEvent e)
      {
         double totalCharges = 50.0;   // Total charges

         // Create a DecimalFormat object to format output.
         DecimalFormat dollar = new DecimalFormat("#,##0.00");

         // Get the total charges
         totalCharges += configPanel.getStylePrice() + configPanel.getSizePrice() +
                         configPanel.getColorPrice();

         // Display the message.
         JOptionPane.showMessageDialog(null, "Total Charges: $" + 
                                             dollar.format(totalCharges));
      }
   } // End of inner class 

    //ExitButtonListener and exitButton component

    private class ExitButtonListener implements ActionListener
    {
        //actionPerformed method

        public void actionPerformed(ActionEvent e)
      {
         System.exit(0);
      }
   } // End of inner class

   //The main method instantiates the ShadeDesigner class

    public static void main(String[] args)
   {
      ShadeDesignerWindow sdw = new ShadeDesignerWindow();
   }    
}

我需要帮助才能使上述任一代码正常工作。我没有得到整个 configpanel 方法或它应该做什么。我从一个文件程序开始,因为那是我学习 Java 编程的方式。即使有实现,它总是一个swing文件或gui而不是两个。谢谢您的帮助。贾明·A·毕晓普

4

1 回答 1

1

就像ConfigPanel从原始程序中分离出来的那样,将样式、尺寸和颜色作为单独的类提取出来,每个类都管理自己的名称和数量。让它们中的每一个都实现一个合适的接口:

interface MarginalBillable {
    double getMarginalCostOfCurrentSelection();
}

然后ConfigPanel可以只有一种方法返回所有当前选择的总数。为了获得额外的荣誉,请将三个面板的父级设为abstract

class ConfigPanel {

    StylePanel stylePanel = new StylePanel();
    SizePanel sizePanel = new SizePanel();
    ColorPanel colorPanel = new ColorPanel();

    double getTotalMarginalAmount() {
        return stylePanel.getMarginalCostOfCurrentSelection()
            + sizePanel.getMarginalCostOfCurrentSelection()
            + colorPanel.getMarginalCostOfCurrentSelection();
    }
}

interface MarginalBillable {

    double getMarginalCostOfCurrentSelection();
}

class StylePanel extends JPanel implements MarginalBillable {

    double value;

    @Override
    public double getMarginalCostOfCurrentSelection() {
        return value; //based on current settings
    }
}

class SizePanel extends JPanel implements MarginalBillable {

    double value;

    @Override
    public double getMarginalCostOfCurrentSelection() {
        return value; //based on current settings
    }
}

class ColorPanel extends JPanel implements MarginalBillable {

    double value;

    @Override
    public double getMarginalCostOfCurrentSelection() {
        return value; //based on current settings
    }
}
于 2012-04-08T14:01:12.910 回答