シェードデザイナー
カスタム ウィンドウ シェード デザイナーは、シェードごとに 50 ドルの基本料金を請求します。さらに、特定のスタイル、サイズ、および色については、次のように料金が加算されます。
以下の説明とデータを使用したこのプログラムの目的は、ユーザーがリストまたはコンボ ボックスからスタイル、サイズ、色、色合いを選択できるアプリケーションを作成することです。合計料金が表示されます。
スタイル: レギュラー シェード: $0 追加 フォールディング シェード: $10 追加 ローマン シェード: $15 追加
サイズ: 幅 25 インチ: $0 追加 幅 27 インチ: $2 追加 幅 32 インチ: $4 追加 幅 40 インチ: $6 追加
色: ナチュラル: $5 を追加 青: $0 を追加 ティール: $0 を追加 赤: $0 を追加 緑: $0 を追加
私は 2 つのアプローチを取りましたが、以下の最初のプログラム コードにはアルゴリズムに問題があり、合計価格を計算するためにスタイル、サイズ、色の選択の追加コストが追加されません。合計料金=スタイル選択+サイズ選択+カラー選択というように、それぞれ加算された選択なのかわかりません。これを計算して、コード内の正しいアルゴリズムで動作させる方法がわかりません。以下の shadedesigner.java のコード
この後の 2 番目のコードは、configpanel コントロールがリストまたはコンボ ボックス コンテナとパネル操作を shadedesignerwindow.java に渡す 2 番目のアプローチです。構成パネルは、shaddesignerwindow プログラムの実装またはインターフェースとして機能すると想定されています。以下は私がこれまでに持っているものですが、shadedesignerwindow で configpanel を希望どおりに動作させる方法がわかりません。
以下は、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 ()
}
}
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プログラミングを学んだ方法であるため、私は1つのファイルプログラムから始めました。実装されていても、2 つではなく、常に 1 つのスイング ファイルまたは GUI でした。ご協力ありがとうございました。ジャミン・A・ビショップ