標題:
園遊會小幫手 (一)
[打印本頁]
作者:
周政輝
時間:
2018-7-7 10:10
標題:
園遊會小幫手 (一)
本帖最後由 周政輝 於 2018-7-7 12:05 編輯
試完成如下之視窗程式實作,商品的種類與價格、字體樣式與大小、按鈕大小、按鈕圖案皆自由發揮,但需做到下列需求:
1. 顯示商品名稱與價格
2. 指定購買數量的機制 (用按鈕點擊或鍵盤輸入皆可)
3. 顯示「即時的」各商品購買數量、小計以及總金額
4. 點擊結帳鈕後顯示應找金額
5. 若總金額欄或收款欄為零,則結帳按鈕無效
6. 若應找金額為負數,找零欄字體設為紅色
7. 點擊重置鈕後清除交易細節
8. 利用 JButton 下的 setToolTipText() 方法為結帳鈕與重置紐設定提示訊息
[attach]4413[/attach]
[attach]4414[/attach]
[attach]4415[/attach]
[attach]4416[/attach]
本帖隱藏的內容需要回復才可以瀏覽
作者:
蔡季樺
時間:
2018-7-7 12:00
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Main extends JFrame {
private JPanel contentPane;
private JTextField num;
private JTextField num2;
private JTextField num3;
private JTextField num4;
private JTextField total;
private JTextField total2;
private JTextField total3;
private JTextField total4;
private JTextField textField_8;
private JTextField textField_9;
private JTextField textField_10;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
int price[] = new int[4];
price[0] = 1000;
price[1] = 15;
price[2] = 1487;
price[3] = 199;
JLabel lblNewLabel = new JLabel("\u54C1\u540D");
lblNewLabel.setBounds(37, 22, 46, 15);
contentPane.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("\u55AE\u50F9");
lblNewLabel_1.setBounds(110, 22, 46, 15);
contentPane.add(lblNewLabel_1);
JLabel lblNewLabel_2 = new JLabel("\u6578\u91CF");
lblNewLabel_2.setBounds(183, 22, 46, 15);
contentPane.add(lblNewLabel_2);
JLabel lblNewLabel_3 = new JLabel("\u589E/\u6E1B");
lblNewLabel_3.setBounds(270, 22, 46, 15);
contentPane.add(lblNewLabel_3);
JLabel lblNewLabel_4 = new JLabel("\u5C0F\u8A08");
lblNewLabel_4.setBounds(365, 22, 46, 15);
contentPane.add(lblNewLabel_4);
JLabel label = new JLabel("\u5149\u982D\u845B\u683C");
label.setBounds(10, 49, 55, 15);
contentPane.add(label);
JLabel lbla = new JLabel("\u7D05\u5FC3A");
lbla.setBounds(10, 74, 46, 15);
contentPane.add(lbla);
JLabel lblNewLabel_5 = new JLabel("\u6BCB\u901A\u54E5");
lblNewLabel_5.setBounds(10, 99, 46, 15);
contentPane.add(lblNewLabel_5);
JLabel lblNewLabel_6 = new JLabel("\u7D05\u8272\u5C0F\u8932\u8932");
lblNewLabel_6.setBounds(10, 124, 73, 15);
contentPane.add(lblNewLabel_6);
JLabel price_1 = new JLabel(String.valueOf(price[0]));
price_1.setBounds(110, 49, 46, 15);
contentPane.add(price_1);
JLabel price_2 = new JLabel(String.valueOf(price[1]));
price_2.setBounds(110, 74, 46, 15);
contentPane.add(price_2);
JLabel price_3 = new JLabel(String.valueOf(price[2]));
price_3.setBounds(110, 99, 46, 15);
contentPane.add(price_3);
JLabel price_4 = new JLabel(String.valueOf(price[3]));
price_4.setBounds(110, 124, 46, 15);
contentPane.add(price_4);
num = new JTextField();
num.setText("0");
num.setEnabled(false);
num.setBounds(175, 47, 64, 21);
contentPane.add(num);
num.setColumns(10);
num2 = new JTextField();
num2.setText("0");
num2.setEnabled(false);
num2.setBounds(175, 71, 64, 21);
contentPane.add(num2);
num2.setColumns(10);
num3 = new JTextField();
num3.setText("0");
num3.setEnabled(false);
num3.setBounds(175, 96, 64, 21);
contentPane.add(num3);
num3.setColumns(10);
num4 = new JTextField();
num4.setText("0");
num4.setEnabled(false);
num4.setBounds(174, 121, 64, 21);
contentPane.add(num4);
num4.setColumns(10);
total = new JTextField();
total.setText("0");
total.setEnabled(false);
total.setBounds(338, 47, 86, 21);
contentPane.add(total);
total.setColumns(10);
total2 = new JTextField();
total2.setText("0");
total2.setEnabled(false);
total2.setBounds(338, 71, 86, 21);
contentPane.add(total2);
total2.setColumns(10);
total3 = new JTextField();
total3.setText("0");
total3.setEnabled(false);
total3.setBounds(338, 96, 86, 21);
contentPane.add(total3);
total3.setColumns(10);
total4 = new JTextField();
total4.setText("0");
total4.setEnabled(false);
total4.setBounds(338, 121, 86, 21);
contentPane.add(total4);
total4.setColumns(10);
JButton b1p = new JButton("+");
b1p.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent arg0) {
int temp = Integer.parseInt(num.getText())+1;
num.setText(String.valueOf(temp));
total.setText(String.valueOf(temp*Integer.parseInt(price_1.getText())));
}
});
b1p.setBounds(244, 45, 46, 23);
contentPane.add(b1p);
JButton b1m = new JButton("-");
b1m.setBounds(291, 45, 46, 23);
contentPane.add(b1m);
b1m.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent arg0) {
int temp = Integer.parseInt(num.getText())-1;
num.setText(String.valueOf(temp));
total.setText(String.valueOf(temp*Integer.parseInt(price_1.getText())));
}
});
JButton b2p = new JButton("+");
b2p.setBounds(244, 70, 46, 23);
contentPane.add(b2p);
b2p.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent arg0) {
int temp = Integer.parseInt(num2.getText())+1;
num2.setText(String.valueOf(temp));
total2.setText(String.valueOf(temp*Integer.parseInt(price_2.getText())));
}
});
JButton b2m = new JButton("-");
b2m.setBounds(291, 70, 46, 23);
contentPane.add(b2m);
b2m.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent arg0) {
int temp = Integer.parseInt(num2.getText())-1;
num2.setText(String.valueOf(temp));
total2.setText(String.valueOf(temp*Integer.parseInt(price_2.getText())));
}
});
JButton b3p = new JButton("+");
b3p.setBounds(244, 95, 46, 23);
contentPane.add(b3p);
b3p.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent arg0) {
int temp = Integer.parseInt(num3.getText())+1;
num3.setText(String.valueOf(temp));
total3.setText(String.valueOf(temp*Integer.parseInt(price_3.getText())));
}
});
JButton b3m = new JButton("-");
b3m.setBounds(291, 95, 46, 23);
contentPane.add(b3m);
b3m.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent arg0) {
int temp = Integer.parseInt(num3.getText())-1;
num3.setText(String.valueOf(temp));
total3.setText(String.valueOf(temp*Integer.parseInt(price_3.getText())));
}
});
JButton b4p = new JButton("+");
b4p.setBounds(244, 120, 46, 23);
contentPane.add(b4p);
b4p.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent arg0) {
int temp = Integer.parseInt(num4.getText())+1;
num4.setText(String.valueOf(temp));
total4.setText(String.valueOf(temp*Integer.parseInt(price_4.getText())));
}
});
JButton b4m = new JButton("-");
b4m.setBounds(291, 120, 46, 23);
contentPane.add(b4m);
b4m.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent arg0) {
int temp = Integer.parseInt(num4.getText())-1;
num4.setText(String.valueOf(temp));
total4.setText(String.valueOf(temp*Integer.parseInt(price_4.getText())));
}
});
JLabel lblNewLabel_7 = new JLabel("\u7E3D\u91D1\u984D");
lblNewLabel_7.setBounds(10, 149, 55, 26);
contentPane.add(lblNewLabel_7);
JLabel lblNewLabel_8 = new JLabel("\u6536");
lblNewLabel_8.setBounds(10, 185, 55, 28);
contentPane.add(lblNewLabel_8);
JLabel lblNewLabel_9 = new JLabel("\u627E");
lblNewLabel_9.setBounds(10, 223, 55, 29);
contentPane.add(lblNewLabel_9);
textField_8 = new JTextField();
textField_8.setEnabled(false);
textField_8.setBounds(79, 149, 96, 21);
contentPane.add(textField_8);
textField_8.setColumns(10);
textField_9 = new JTextField();
textField_9.setBounds(79, 189, 96, 21);
contentPane.add(textField_9);
textField_9.setColumns(10);
textField_10 = new JTextField();
textField_10.setEnabled(false);
textField_10.setBounds(79, 227, 96, 21);
contentPane.add(textField_10);
textField_10.setColumns(10);
JButton btnNewButton_2 = new JButton("");
btnNewButton_2.setIcon(new ImageIcon("C:\\eclipse\\workspace\\D20180707\\src\\checkout.png"));
btnNewButton_2.setBounds(220, 152, 96, 84);
contentPane.add(btnNewButton_2);
JButton button_6 = new JButton("");
button_6.setIcon(new ImageIcon("C:\\eclipse\\workspace\\D20180707\\src\\clear.png"));
button_6.setBounds(328, 152, 96, 84);
contentPane.add(button_6);
}
}
複製代碼
作者:
蔡庭豪
時間:
2018-7-7 12:01
本帖最後由 蔡庭豪 於 2018-7-14 11:18 編輯
package bbs.istak.org.tw;
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO 自動產生的方法 Stub
JButton btn = (JButton)arg0.getSource();
if(btn.getText().equals("+")){
if(btn == button_m_qq){
int num = Integer.valueOf(textField_num_qq.getText())+1;
textField_num_qq.setText(String.valueOf(num)) ;
textField_tt_qq.setText(String.valueOf(num*22));
}
else if(btn == button_m_basand){
int num = Integer.valueOf(textField_num_basand.getText())+1;
textField_num_basand.setText(String.valueOf(num)) ;
textField_tt_basand.setText(String.valueOf(num*49));
}
else if(btn == button_m_bubmt){
int num = Integer.valueOf(textField_num_bubmt.getText())+1;
textField_num_bubmt.setText(String.valueOf(num)) ;
textField_tt_bubmt.setText(String.valueOf(num*29));
}
else if(btn == button_m_sprite){
int num = Integer.valueOf(textField_num_sprite.getText())+1;
textField_num_sprite.setText(String.valueOf(num)) ;
textField_tt_sprite.setText(String.valueOf(num*12));
}
}
else{
if(btn == button_l_qq){
int num = Integer.valueOf(textField_num_qq.getText())-1;
textField_num_qq.setText(String.valueOf(num)) ;
textField_tt_qq.setText(String.valueOf(num*22));
}
else if(btn == button_l_basand){
int num = Integer.valueOf(textField_num_basand.getText())-1;
textField_num_basand.setText(String.valueOf(num)) ;
textField_tt_basand.setText(String.valueOf(num*49));
}
else if(btn == button_l_bubmt){
int num = Integer.valueOf(textField_num_bubmt.getText())-1;
textField_num_bubmt.setText(String.valueOf(num)) ;
textField_tt_bubmt.setText(String.valueOf(num*29));
}
else if(btn == button_l_sprite){
int num = Integer.valueOf(textField_num_sprite.getText())-1;
textField_num_sprite.setText(String.valueOf(num)) ;
textField_tt_sprite.setText(String.valueOf(num*12));
}
}
int total = Integer.valueOf(textField_tt_qq.getText())+
Integer.valueOf(textField_tt_basand.getText())+
Integer.valueOf(textField_tt_bubmt.getText())+
Integer.valueOf(textField_tt_sprite.getText());
textField_total.setText(String.valueOf(total));
}
}
複製代碼
作者:
黃茂勛
時間:
2018-7-7 12:09
本帖最後由 黃茂勛 於 2018-7-14 18:04 編輯
[attach]4483[/attach]
作者:
張健勳
時間:
2018-7-14 10:04
package bbs.istak.org.tw;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Main extends JFrame {
private JPanel contentPane;
private JTextField Food1Num;
private JTextField Food2Num;
private JTextField Food3Num;
private JTextField Food4Num;
private JTextField food1lc;
private JTextField food2lc;
private JTextField food3lc;
private JTextField food4lc;
private JTextField AllMoney;
private JTextField Pay;
private JTextField Change;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Main() {
setTitle("\u5712\u904A\u6703\u5C0F\u5E6B\u624B");
setIconImage(Toolkit.getDefaultToolkit().getImage(Main.class.getResource("/bbs/istak/org/tw/icon.png")));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 567, 343);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel NameLb = new JLabel("\u54C1\u540D");
NameLb.setBounds(46, 10, 46, 15);
contentPane.add(NameLb);
JLabel MoneyLb = new JLabel("\u55AE\u50F9");
MoneyLb.setBounds(126, 10, 46, 15);
contentPane.add(MoneyLb);
JLabel NumLb = new JLabel("\u6578\u91CF");
NumLb.setBounds(201, 10, 57, 15);
contentPane.add(NumLb);
JLabel UDLb = new JLabel("\u589E / \u6E1B");
UDLb.setBounds(312, 10, 67, 15);
contentPane.add(UDLb);
JLabel LcLb = new JLabel("\u5C0F\u8A08");
LcLb.setBounds(443, 10, 46, 15);
contentPane.add(LcLb);
AllMoney = new JTextField();
AllMoney.setEditable(false);
AllMoney.setText("0");
AllMoney.setHorizontalAlignment(SwingConstants.RIGHT);
AllMoney.setColumns(10);
AllMoney.setBounds(83, 169, 149, 29);
contentPane.add(AllMoney);
int All = Integer.valueOf(AllMoney.getText());
JLabel AllMoney = new JLabel("\u7E3D\u91D1\u984D");
AllMoney.setBounds(23, 176, 46, 15);
contentPane.add(AllMoney);
JLabel PayLb = new JLabel("\u6536");
PayLb.setBounds(23, 218, 46, 15);
contentPane.add(PayLb);
Pay = new JTextField();
Pay.setText("0");
Pay.setHorizontalAlignment(SwingConstants.RIGHT);
Pay.setColumns(10);
Pay.setBounds(83, 211, 149, 29);
contentPane.add(Pay);
Change = new JTextField();
Change.setText("0");
Change.setHorizontalAlignment(SwingConstants.RIGHT);
Change.setEditable(false);
Change.setColumns(10);
Change.setBounds(80, 261, 152, 29);
contentPane.add(Change);
JLabel ChangeLb = new JLabel("\u627E");
ChangeLb.setBounds(23, 268, 46, 15);
contentPane.add(ChangeLb);
JButton Count = new JButton("");
Count.setIcon(new ImageIcon(Main.class.getResource("/bbs/istak/org/tw/checkout.png")));
Count.setBounds(276, 176, 114, 107);
contentPane.add(Count);
JButton Clear = new JButton("");
Clear.setIcon(new ImageIcon(Main.class.getResource("/bbs/istak/org/tw/clear.png")));
Clear.setBounds(404, 176, 114, 107);
contentPane.add(Clear);
JPanel FoodListPanel = new JPanel();
FoodListPanel.setBounds(23, 32, 495, 124);
contentPane.add(FoodListPanel);
FoodListPanel.setLayout(null);
JLabel food1Lb = new JLabel("QQ\u86CB");
food1Lb.setBounds(14, 8, 89, 15);
FoodListPanel.add(food1Lb);
JLabel food2Lb = new JLabel("\u78B3\u70E4\u4E09\u660E\u6CBB");
food2Lb.setBounds(14, 36, 89, 15);
FoodListPanel.add(food2Lb);
JLabel food3Lb = new JLabel("\u73CD\u73E0\u5976\u8336");
food3Lb.setBounds(14, 66, 89, 15);
FoodListPanel.add(food3Lb);
JLabel food4Lb = new JLabel("\u5F48\u73E0\u6C7D\u6C34");
food4Lb.setBounds(14, 94, 89, 15);
FoodListPanel.add(food4Lb);
JLabel Food1MoneyLb = new JLabel("22\u5143");
Food1MoneyLb.setBounds(103, 8, 46, 15);
FoodListPanel.add(Food1MoneyLb);
Food1MoneyLb.setHorizontalAlignment(SwingConstants.LEFT);
JLabel Food2MoneyLb = new JLabel("49\u5143");
Food2MoneyLb.setBounds(103, 36, 46, 15);
FoodListPanel.add(Food2MoneyLb);
JLabel Food3MoneyLb = new JLabel("29\u5143");
Food3MoneyLb.setBounds(103, 66, 46, 15);
FoodListPanel.add(Food3MoneyLb);
JLabel Food4MoneyLb = new JLabel("12\u5143");
Food4MoneyLb.setBounds(103, 94, 46, 15);
FoodListPanel.add(Food4MoneyLb);
Food1Num = new JTextField();
Food1Num.setBounds(163, 6, 73, 19);
FoodListPanel.add(Food1Num);
Food1Num.setEditable(false);
Food1Num.setHorizontalAlignment(SwingConstants.RIGHT);
Food1Num.setText("0");
Food1Num.setColumns(10);
Food2Num = new JTextField();
Food2Num.setBounds(163, 34, 73, 19);
FoodListPanel.add(Food2Num);
Food2Num.setEditable(false);
Food2Num.setText("0");
Food2Num.setHorizontalAlignment(SwingConstants.RIGHT);
Food2Num.setColumns(10);
Food3Num = new JTextField();
Food3Num.setBounds(163, 64, 73, 19);
FoodListPanel.add(Food3Num);
Food3Num.setEditable(false);
Food3Num.setText("0");
Food3Num.setHorizontalAlignment(SwingConstants.RIGHT);
Food3Num.setColumns(10);
Food4Num = new JTextField();
Food4Num.setBounds(163, 92, 73, 19);
FoodListPanel.add(Food4Num);
Food4Num.setEditable(false);
Food4Num.setText("0");
Food4Num.setHorizontalAlignment(SwingConstants.RIGHT);
Food4Num.setColumns(10);
JButton food1up = new JButton("+");
food1up.setBounds(250, 4, 56, 23);
FoodListPanel.add(food1up);
JButton food1down = new JButton("-");
food1down.setBounds(320, 4, 58, 23);
FoodListPanel.add(food1down);
JButton food2up = new JButton("+");
food2up.setBounds(250, 32, 56, 23);
FoodListPanel.add(food2up);
JButton food2down = new JButton("-");
food2down.setBounds(320, 32, 58, 23);
FoodListPanel.add(food2down);
JButton food3up = new JButton("+");
food3up.setBounds(250, 62, 56, 23);
FoodListPanel.add(food3up);
JButton food3down = new JButton("-");
food3down.setBounds(320, 62, 58, 23);
FoodListPanel.add(food3down);
JButton food4up = new JButton("+");
food4up.setBounds(250, 90, 56, 23);
FoodListPanel.add(food4up);
JButton food4down = new JButton("-");
food4down.setBounds(320, 90, 58, 23);
FoodListPanel.add(food4down);
food1lc = new JTextField();
food1lc.setBounds(392, 6, 73, 19);
FoodListPanel.add(food1lc);
food1lc.setEditable(false);
food1lc.setText("0");
food1lc.setHorizontalAlignment(SwingConstants.RIGHT);
food1lc.setColumns(10);
food2lc = new JTextField();
food2lc.setBounds(392, 34, 73, 19);
FoodListPanel.add(food2lc);
food2lc.setEditable(false);
food2lc.setText("0");
food2lc.setHorizontalAlignment(SwingConstants.RIGHT);
food2lc.setColumns(10);
food3lc = new JTextField();
food3lc.setBounds(392, 64, 73, 19);
FoodListPanel.add(food3lc);
food3lc.setEditable(false);
food3lc.setText("0");
food3lc.setHorizontalAlignment(SwingConstants.RIGHT);
food3lc.setColumns(10);
food4lc = new JTextField();
food4lc.setBounds(392, 92, 73, 19);
FoodListPanel.add(food4lc);
food4lc.setEditable(false);
food4lc.setText("0");
food4lc.setHorizontalAlignment(SwingConstants.RIGHT);
food4lc.setColumns(10);
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2