[Java]小作品-訂單管理系統
- 使用MVC來做出一個簡單作品
- M(Model)
1. DataBase(表)
2. Class->uml圖
- V(View)
->UI介面
->新增畫面
- Controller
- [database]
public class order {
private String name;
private int lcd, ram, mouse;public order(String name, int lcd, int ram, int mouse) {
this.name = name;
if (lcd > 0) {
this.lcd = lcd;
}
if (ram > 0) {
this.ram = ram;
}
if (mouse > 0) {
this.mouse = mouse;
}
}public void setName(String name) {
this.name = name;
}public void setLcd(int lcd) {
this.lcd = lcd;
}public void setRam(int ram) {
this.ram = ram;
}public void setMouse(int mouse) {
this.mouse = mouse;
}public String getName() {
return name;
}public int getLcd() {
return lcd;
}public int getRam() {
return ram;
}public int getMouse() {
return mouse;
}}
- [UI]
public class orderUI extends javax.swing.JFrame {
/**
* Creates new form orderUI
*/
public orderUI() {
initComponents();
}/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings(“unchecked”)
// <editor-fold defaultstate=”collapsed” desc=”Generated Code”>
private void initComponents() {jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
name = new javax.swing.JTextField();
lcd = new javax.swing.JTextField();
ram = new javax.swing.JTextField();
mouse = new javax.swing.JTextField();
enter = new javax.swing.JButton();
jPanel3 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
ans = new javax.swing.JTextArea();setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(255, 153, 0));
jLabel1.setFont(new java.awt.Font(“王漢宗綜藝體繁”, 0, 18)); // NOI18N
jLabel1.setText(“南科訂單系統”);javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(145, 145, 145)
.addComponent(jLabel1)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);jPanel2.setBackground(new java.awt.Color(204, 204, 204));
jLabel2.setFont(new java.awt.Font(“王漢宗綜藝體繁”, 0, 15)); // NOI18N
jLabel2.setText(“姓名:”);jLabel3.setFont(new java.awt.Font(“王漢宗綜藝體繁”, 0, 15)); // NOI18N
jLabel3.setText(“LCD:”);jLabel4.setFont(new java.awt.Font(“王漢宗綜藝體繁”, 0, 15)); // NOI18N
jLabel4.setText(“RAM:”);jLabel5.setFont(new java.awt.Font(“王漢宗綜藝體繁”, 0, 15)); // NOI18N
jLabel5.setText(“MOUSE:”);name.setFont(new java.awt.Font(“王漢宗綜藝體繁”, 0, 15)); // NOI18N
lcd.setFont(new java.awt.Font(“王漢宗綜藝體繁”, 0, 15)); // NOI18N
ram.setFont(new java.awt.Font(“王漢宗綜藝體繁”, 0, 15)); // NOI18N
mouse.setFont(new java.awt.Font(“王漢宗綜藝體繁”, 0, 15)); // NOI18N
enter.setBackground(new java.awt.Color(255, 153, 0));
enter.setFont(new java.awt.Font(“王漢宗綜藝體繁”, 0, 15)); // NOI18N
enter.setText(“送出”);
enter.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
enterMouseClicked(evt);
}
});javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(name))
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jLabel3)
.addGap(14, 14, 14)
.addComponent(lcd))
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jLabel4)
.addGap(12, 12, 12)
.addComponent(ram)))
.addGap(14, 14, 14))
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jLabel5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(mouse, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE)
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(enter)
.addContainerGap())
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(24, 24, 24)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(lcd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4)
.addComponent(ram, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel5)
.addComponent(mouse, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(enter)
.addContainerGap(61, Short.MAX_VALUE))
);jPanel3.setBackground(new java.awt.Color(255, 255, 204));
ans.setColumns(20);
ans.setRows(5);
jScrollPane1.setViewportView(ans);javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE)
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1)
);javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
);pack();
}// </editor-fold>private void enterMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
String Name = name.getText();
int Lcd = Integer.parseInt(lcd.getText());
int Ram = Integer.parseInt(ram.getText());
int Mouse = Integer.parseInt(mouse.getText());order o = new order(Name, Lcd, Ram, Mouse);
ans.setText(“Congratulations,You are sent a new order!”
+ “\nname:” + o.getName()
+ “\nLCD:” + o.getLcd()
+ “\nRAM:” + o.getRam()
+ “\nMOUSE:” + o.getMouse());}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate=”collapsed” desc=” Look and feel setting code (optional) “>
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if (“Nimbus”.equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(orderUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(orderUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(orderUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(orderUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new orderUI().setVisible(true);
}
});
}// Variables declaration — do not modify
private javax.swing.JTextArea ans;
private javax.swing.JButton enter;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField lcd;
private javax.swing.JTextField mouse;
private javax.swing.JTextField name;
private javax.swing.JTextField ram;
// End of variables declaration
}
- 當按下送出之後,姓名、成績會直接顯示在底下的黃色框框中
(在UI打的粗體字程式碼)