|
|
|
Java GUI : พอดีทำโปรแกรมร้านกาแฟแต่อาจารย์ให้เปลี่ยนโครงสร้างข้อมูลใหม่เป็นลิงค์ลิสต์รบกวนแก้ไขให้ด้วยนะคะพอดีเป็นมือใหม่ค่ะ |
|
|
|
|
|
|
|
Code (Java)
package nam.coffeeshop;
import java.text.DecimalFormat;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
public class Form extends javax.swing.JFrame {
//ราคาของรายกาแฟ
String price[]={"25","30","25","25","30","30"};
//สร้างโมเดลตาราง
DefaultTableModel model;
//สร้างตัว Comma
DecimalFormat df=new DecimalFormat ("#,###.00");
//หาราคารวม
double total=0.0;
//เก็บแถวที่ผู้ใช้เลือก
int selected;
//เก็บตำแหน่งที่ซ้ำ
int n;
//เก็บราคารวม
double money;
public Form() {
initComponents();
t1.setText(price[0]);
DefaultTableModel DefaultTableModel;
model=(DefaultTableModel)tb.getModel();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
cbcoffee = new javax.swing.JComboBox();
jLabel2 = new javax.swing.JLabel();
t1 = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
t2 = new javax.swing.JTextField();
bn1 = new javax.swing.JButton();
bn2 = new javax.swing.JButton();
bn3 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
tb = new javax.swing.JTable();
lbtotal = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("โปรแกรมร้านกาแฟ");
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jLabel1.setText("ราคา/ชุด");
cbcoffee.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
cbcoffee.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "กาแฟนมสด", "คาปูชิโน่", "ชาเย็นนมสด", "โอวันติน", "โกโก้", "ลาเต้นมสด" }));
cbcoffee.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
cbcoffeeItemStateChanged(evt);
}
});
jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jLabel2.setText("รายการกาแฟ");
t1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
t1.setEnabled(false);
jLabel3.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jLabel3.setText("จำนวน");
t2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
bn1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
bn1.setText("เพิ่ม");
bn1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bn1ActionPerformed(evt);
}
});
bn2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
bn2.setText("ลบ");
bn2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bn2ActionPerformed(evt);
}
});
bn3.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
bn3.setText("แก้ไข");
bn3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bn3ActionPerformed(evt);
}
});
tb.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
tb.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"รายการกาแฟ", "ราคา/ชุด", "จำนวน", "จำนวนเงิน"
}
) {
boolean[] canEdit = new boolean [] {
true, false, false, false
};
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
tb.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
tbMouseClicked(evt);
}
});
jScrollPane1.setViewportView(tb);
if (tb.getColumnModel().getColumnCount() > 0) {
tb.getColumnModel().getColumn(0).setResizable(false);
tb.getColumnModel().getColumn(1).setResizable(false);
tb.getColumnModel().getColumn(2).setResizable(false);
tb.getColumnModel().getColumn(3).setResizable(false);
}
lbtotal.setFont(new java.awt.Font("Tahoma", 1, 16)); // NOI18N
lbtotal.setText(" ราคารวม");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(30, 30, 30)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(95, 95, 95)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(bn1, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(17, 17, 17)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(t2, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGap(13, 13, 13)
.addComponent(bn2, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(29, 29, 29)
.addComponent(bn3))))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(cbcoffee, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(t1, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
.addContainerGap(31, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(lbtotal, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(116, 116, 116))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(26, 26, 26)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(cbcoffee)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(t1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(t2, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(bn1)
.addComponent(bn3)
.addComponent(bn2))
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 314, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(lbtotal, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(23, 23, 23))
);
pack();
}// </editor-fold>
private void cbcoffeeItemStateChanged(java.awt.event.ItemEvent evt) {
t1.setText(price[cbcoffee.getSelectedIndex()]);
}
private void bn1ActionPerformed(java.awt.event.ActionEvent evt) {
Vector v=new Vector();
if(t2.getText().isEmpty()){
JOptionPane.showMessageDialog(this,"กรุณาป้อนจำนวนกาแฟ","คำเตือน",
JOptionPane.ERROR_MESSAGE);
}if (CheckMenu()){
//a-จำนวนเติมที่เพิ่มเข้าไปก่อนหน้า
int a=Integer.parseInt(model.getValueAt(n,2).toString())
+Integer.parseInt(t2.getText());
System.out.println("a="+a);
model.setValueAt(a, n, 2);
//หาจำนวนเงินของแต่ละรายการ
double price=a*Double.valueOf(tb.getValueAt(n, 1).toString());
System.out.println("จำนวนเงินใหม่="+price);
model.setValueAt(df.format(price),n,3);
//หาราคารวม
money=Double.valueOf(t1.getText())*Double.valueOf(t2.getText());
total+=money;
lbtotal.setText("ราคารวม : "+df.format(total)+" บาท ");
}else{
v.add(cbcoffee.getSelectedItem().toString());
v.add(t1.getText());
v.add(t2.getText());
money=Double.valueOf(t1.getText())*Double.valueOf(t2.getText());
v.add(df.format(money));
total+=money;
model.addRow(v);
lbtotal.setText("ราคารวม : "+df.format(total)+" บาท ");
}
}
private void bn2ActionPerformed(java.awt.event.ActionEvent evt) {
if(tb.isRowSelected(selected)){
double moneySelected=Double.valueOf(tb.getValueAt
(selected,3).toString().replace(",",""));
System.out.println("แสดงจำนวนเงิน="+moneySelected);
total-=moneySelected;
lbtotal.setText("ราคารวม : "+df.format(total)+" บาท ");
if(total==0){
lbtotal.setText("ราคารวม : 0.00 บาท ");
}
model.removeRow(selected);
}else{
JOptionPane.showMessageDialog(this,"กรุณาเลือกแถวในตาราง","คำเตือน",
JOptionPane.ERROR_MESSAGE);
}
}
private void tbMouseClicked(java.awt.event.MouseEvent evt) {
selected=tb.getSelectedRow();
String name=tb.getValueAt(selected,0).toString();
System.out.println("Name="+name);
System.out.println("แถวที่เลือก="+selected);
t2.setText(tb.getValueAt(selected,2).toString());
//การเปลี่ยนแปลง Item ใน Combobox
for (int i = 0; i < cbcoffee.getItemCount(); i++){
if(name.equals(cbcoffee.getItemAt(i))){
cbcoffee.setSelectedIndex(i);
}
}
}
private void bn3ActionPerformed(java.awt.event.ActionEvent evt) {
//เก็บราคาก่อนแก้ไข
double before=Double.valueOf(tb.getValueAt(selected,1).toString())
*Double.valueOf(tb.getValueAt(selected,2).toString());
System.out.println("before="+before);
//นำจำนวนลงตาราง
tb.setValueAt(t2.getText(),selected,2);
int amount=Integer.valueOf(t2.getText());
//คำนวณราคาใหม่
double bnew=amount*Double.valueOf(tb.getValueAt(selected, 1).toString());
tb.setValueAt(df.format(bnew),selected,3);
total=total-before+bnew;
lbtotal.setText("ราคารวม : "+df.format(total)+" บาท ");
}
public boolean CheckMenu(){
for (int i = 0; i < tb.getRowCount(); i++){
if(cbcoffee.getSelectedItem().toString().equals(tb.getValueAt(i,0))){
n = i;
System.out.println("รายการกาแฟซ้ำกัน"+"ที่แถว="+n);
return true;
}
}
return false;
}
Tag : Java, JAVA
|
|
|
|
|
|
Date :
2015-10-03 14:01:01 |
By :
namza98 |
View :
3229 |
Reply :
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
Code (Java)
public class Form extends javax.swing.JFrame {
//ราคาของรายกาแฟ
String price[]={"25","30","25","25","30","30"};
//สร้างโมเดลตาราง
DefaultTableModel model;
//สร้างตัว Comma
DecimalFormat df=new DecimalFormat ("#,###.00");
//หาราคารวม
double total=0.0;
//เก็บแถวที่ผู้ใช้เลือก
int selected;
//เก็บตำแหน่งที่ซ้ำ
int n;
//เก็บราคารวม
double money;
// add value
List<String> list = new LinkedList<String>();
list.add("กาแฟนมสด");
list.add("คาปูชิโน่");
list.add("ชาเย็นนมสด");
list.add("โอวันติน");
list.add("โกโก้");
list.add("ลาเต้นมสด");
public Form() {
initComponents();
t1.setText(price[0]);
DefaultTableModel DefaultTableModel;
model=(DefaultTableModel)tb.getModel();
//add list
for(String data : list){
cbcoffee.addItem(data);
}
}
|
ประวัติการแก้ไข 2015-10-03 22:06:46 2015-10-03 22:34:59
|
|
|
|
Date :
2015-10-03 22:03:59 |
By :
ipstarone |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import class java เข้ามาใช้ก่อนจ้า
เอาไว้บนข้างบน ใกล้ๆกับชื่อ package ของเรา
หรือไม่ก็กด ctrl+shift+i มันจะ import เข้ามาให้
Code (Java)
import java.util.LinkedList;
import java.util.List;
|
|
|
|
|
Date :
2015-10-04 13:51:54 |
By :
ipstarone |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 03
|