Shade Designer
定制窗帘设计师收取每个阴影50美元的基本费用。此外, 对于某些样式,大小和颜色添加费用,如下所示:
此程序的对象及其说明和数据是创建一个应用程序,允许用户从列表或组合框中选择阴影的样式,大小,颜色和数量。应显示总费用。
样式: 常规阴影:添加$ 0 折叠色调:加10美元 罗马色调:加15美元
大小: 25英寸宽:加$ 0 27英寸宽:加2美元 32英寸宽:加4美元 40英寸宽:加6美元
颜色: 天然:加5美元 蓝色:加0美元 蓝绿色:加0美元 红色:加0美元 绿色:加0美元
我采取了两种方法,但下面的第一个程序代码有一个算法问题,它不会将样式,大小和颜色选择的额外成本添加到计算机的总价格中。我不确定它是否是每个添加的选择,如总费用=样式选择+尺寸选择+颜色选择。我只是不确定如何计算和使用代码中的正确算法。 下面的shadedesigner.java代码
此后的第二个代码是第二种方法,其中configpanel控制我相信列表或组合框容器和面板操作到shadedesignerwindow.java。配置面板可以作为shaddesignerwindow程序的实现或接口。下面是我到目前为止,但我不知道如何使用shadedesignerwindow让configpanel以他们想要的方式工作。
下面是我为shadedesigner程序构建的代码。如果选择了样式,大小和颜色,我无法获得正确的算法,以便将额外的选择添加到阴影的总电荷中。 重新发布更正4/8/2012编辑下面的代码。
* 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 ()
}
}
confgpanel假设使用下面的代码
* 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而不是两个。 谢谢您的帮助。 Jamin A. Bishop
答案 0 :(得分: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
}
}