?? editdialog.java~37~
字號:
//EditDialog.java//提供新增,編輯,刪除記錄的界面import java.awt.*;import javax.swing.*;import javax.swing.border.*;import java.awt.event.*;public class EditDialog extends JDialog { JPanel panel1 = new JPanel(); BorderLayout borderLayout1 = new BorderLayout(); JPanel jPanel1 = new JPanel(); JPanel jPanel2 = new JPanel(); JPanel jPanel3 = new JPanel(); GridLayout gridLayout1 = new GridLayout(0,1); JLabel jLabel1 = new JLabel(); JLabel jLabel2 = new JLabel(); JLabel jLabel3 = new JLabel(); GridLayout gridLayout2 = new GridLayout(0,1); JButton btCancel = new JButton(); JButton btOK = new JButton(); JTextField tfDescription = new JTextField(); JTextField tfArea = new JTextField(); JTextField tfFormula = new JTextField(); Border border1; Border border2; Floor floor; //最后返回的時候,不該修改傳入的對象,應該新生成對象傳回,這個是結果對象 Floor floorInfo = new Floor(); boolean isOK = false; //為了在不同的情況下顯示不同的對話框標題,所以生成標題數組 final static String[] TITLES = new String[3]; //定義對話框可能進行的操作 public final static int NEW_OPERATION =0; public final static int EDIT_OPERATION=1; public final static int DELETE_OPERATION=2; //當前的對話框的操作狀態 int currentOperation = NEW_OPERATION; static{ TITLES[NEW_OPERATION] = "New record"; TITLES[EDIT_OPERATION] = "Edit record"; TITLES[DELETE_OPERATION] = "Delete record ?"; } public EditDialog(Frame frame, String title, boolean modal) { super(frame, title, modal); try { jbInit(); pack(); } catch(Exception ex) { ex.printStackTrace(); } } public EditDialog() { this(null, "", false); } void jbInit() throws Exception { border1 = BorderFactory.createEmptyBorder(20,20,20,20); border2 = BorderFactory.createEmptyBorder(10,0,0,0); panel1.setLayout(borderLayout1); jPanel1.setLayout(gridLayout1); gridLayout1.setRows(0); gridLayout1.setColumns(1); jLabel1.setMinimumSize(new Dimension(20, 18)); jLabel1.setPreferredSize(new Dimension(20, 18)); jLabel1.setText("Description:"); jLabel2.setMinimumSize(new Dimension(20, 18)); jLabel2.setPreferredSize(new Dimension(20, 18)); jLabel2.setText("Area:"); jLabel3.setMinimumSize(new Dimension(20, 18)); jLabel3.setPreferredSize(new Dimension(20, 18)); jLabel3.setText("Formula:"); jPanel2.setLayout(gridLayout2); btCancel.setText("Cancel"); btCancel.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { btCancel_actionPerformed(e); } }); btOK.setText("OK"); btOK.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { btOK_actionPerformed(e); } }); gridLayout2.setRows(0); gridLayout2.setColumns(1); tfDescription.setMinimumSize(new Dimension(120, 22)); tfDescription.setPreferredSize(new Dimension(120, 22)); tfFormula.setMinimumSize(new Dimension(120, 22)); tfFormula.setPreferredSize(new Dimension(120, 22)); tfArea.setMinimumSize(new Dimension(120, 22)); tfArea.setPreferredSize(new Dimension(120, 22)); panel1.setBorder(border1); panel1.setMinimumSize(new Dimension(250, 170)); panel1.setPreferredSize(new Dimension(250, 170)); jPanel3.setBorder(border2); getContentPane().add(panel1); panel1.add(jPanel1, BorderLayout.CENTER); jPanel1.add(jLabel1, null); jPanel1.add(jLabel3, null); jPanel1.add(jLabel2, null); panel1.add(jPanel2, BorderLayout.EAST); jPanel2.add(tfDescription, null); jPanel2.add(tfFormula, null); jPanel2.add(tfArea, null); panel1.add(jPanel3, BorderLayout.SOUTH); jPanel3.add(btOK, null); jPanel3.add(btCancel, null); } /** * 初始化對話框狀態,在顯示前必須被調用。 * @param operation - 當前的操作,可以是刪除新增或編輯 * @param floor - 用來進行操作的Floor對象 */ void initDialog(int operation,Floor floor){ currentOperation = operation; this.floor = floor; if (currentOperation == NEW_OPERATION){ this.floor = new Floor(); } updateGUI(); } /** * 將界面使用Floor對象進行更新 */ void updateGUI(){ this.setTitle(TITLES[currentOperation]); tfDescription.setText(floor.getDescription()); tfFormula.setText(floor.getFormula()); tfArea.setText(floor.getArea()+""); if (currentOperation == DELETE_OPERATION){ tfDescription.setEnabled(false); tfFormula.setEnabled(false); tfArea.setEnabled(false); } } /** * 獲得當前界面上的信息,并將信息回寫到結果對象里面 */ void getGUIInfo(){ floorInfo.setDescription(tfDescription.getText()); floorInfo.setFormula(tfFormula.getText()); double area =0; try { String s = tfArea.getText(); area = Double.parseDouble(s); } catch (Exception ex) { System.out.println("Area is not a double"); } floorInfo.setArea(area); } void btOK_actionPerformed(ActionEvent e) { doOK(); } void btCancel_actionPerformed(ActionEvent e) { dispose(); } //用戶按了OK按鈕后進行的操作 void doOK(){ double area; String s = tfArea.getText(); try { area = Double.parseDouble(s); } catch (Exception ex) { JOptionPane.showMessageDialog(this,"Invalid input !","Warning",JOptionPane.WARNING_MESSAGE); return; } getGUIInfo(); isOK = true; dispose(); } //獲得用戶操作過后的結果對象,在刪除操作中無用 Floor getUpdatedFloor(){ return floorInfo; }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -