📄 toolbartab.java
字号:
/** * Method to get the name of the button that is being dragged. * @param tra the Transferable with this information. * @param flavors an array of flavors of the dragged information. * @return the name of the button that is being dragged (null on error). */ private String getDraggedObject(Transferable tra, DataFlavor [] flavors) { if (flavors.length <= 0) return null; // text dragging is name of button if (flavors[0].isFlavorTextType()) { Object obj = null; try { obj = tra.getTransferData(flavors[0]); } catch (Exception ex) {} if (obj instanceof String) { String buttonName = (String)obj; if (buttonName.startsWith("TOOLBARBUTTON ")) return buttonName.substring(14); } } return null; } private void eraseDragImage(DropTarget dt) { if (lastButtonDrawn == null) return; Rectangle pathBounds = new Rectangle(0, 0, lastButtonDrawn.getWidth(), lastButtonDrawn.getHeight()); lastButtonDrawn.paintImmediately(pathBounds); lastButtonDrawn = null; } } } /** * Class to start a drag of the separator icon. */ private class SeparatorDrag implements DragSourceListener, DragGestureListener { /** * Method to start a drag from the separator icon. */ public void dragGestureRecognized(DragGestureEvent e) { Transferable transferable = new StringSelection("TOOLBARBUTTON SEPARATOR"); e.startDrag(null, separatorIcon.getImage(), new Point(0, 0), transferable, this); } public void dragEnter(DragSourceDragEvent e) {} public void dragOver(DragSourceDragEvent e) {} public void dragExit(DragSourceEvent e) {} public void dragDropEnd(DragSourceDropEvent e) {} public void dropActionChanged (DragSourceDragEvent e) {} } /** * Class for catching drags onto the trash. * These drags come from elsewhere in the toolbar (for rearrangement). */ private class TrashDropTarget implements DropTargetListener { private boolean trashHighlighted = false; public void dragEnter(DropTargetDragEvent e) { dragAction(e); } public void dragOver(DropTargetDragEvent e) { if (dragAction(e)) return; DropTarget dt = (DropTarget)e.getSource(); if (dt.getComponent() == trashLabel) { // erase former highlighting eraseDragImage(dt); // highlight what the drag is over Graphics2D g2 = (Graphics2D)trashLabel.getGraphics(); g2.setColor(Color.RED); g2.drawRect(0, 0, trashLabel.getWidth()-1, trashLabel.getHeight()-1); trashHighlighted = true; } } public void dropActionChanged(DropTargetDragEvent e) { dragAction(e); } public void dragExit(DropTargetEvent e) { eraseDragImage((DropTarget)e.getSource()); } public void drop(DropTargetDropEvent dtde) { dtde.acceptDrop(DnDConstants.ACTION_MOVE); // erase former highlighting eraseDragImage((DropTarget)dtde.getSource()); // get the files that were dragged String droppedButton = getDraggedObject(dtde.getTransferable(), dtde.getCurrentDataFlavors()); if (droppedButton != null) { // see what the drop is over DropTarget dt = (DropTarget)dtde.getSource(); Component but = dt.getComponent(); if (but != null) { dropButton(droppedButton); dtde.dropComplete(true); return; } } dtde.dropComplete(false); } /** * Method to analyze the drag. * @param e * @return true if the drag is to be rejected. */ private boolean dragAction(DropTargetDragEvent e) { String droppedButton = getDraggedObject(e.getTransferable(), e.getCurrentDataFlavors()); if (droppedButton != null) { e.acceptDrag(e.getDropAction()); return false; } e.rejectDrag(); return true; } /** * Method to get the name of the button that is being dragged. * @param tra the Transferable with this information. * @param flavors an array of flavors of the dragged information. * @return the name of the button that is being dragged (null on error). */ private String getDraggedObject(Transferable tra, DataFlavor [] flavors) { if (flavors.length <= 0) return null; // text dragging is name of button if (flavors[0].isFlavorTextType()) { Object obj = null; try { obj = tra.getTransferData(flavors[0]); } catch (Exception ex) {} if (obj instanceof String) { String buttonName = (String)obj; if (buttonName.startsWith("TOOLBARBUTTON #")) { String bn = buttonName.substring(14); return bn; } } } return null; } private void eraseDragImage(DropTarget dt) { if (!trashHighlighted) return; Rectangle pathBounds = new Rectangle(0, 0, trashLabel.getWidth(), trashLabel.getHeight()); trashLabel.paintImmediately(pathBounds); trashHighlighted = false; } } /** 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. */ // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; toolbar = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); currentToolbar = new javax.swing.JPanel(); commandsPane = new javax.swing.JScrollPane(); trashLabel = new javax.swing.JLabel(); separatorLabel = new javax.swing.JLabel(); attachImage = new javax.swing.JButton(); getContentPane().setLayout(new java.awt.GridBagLayout()); setTitle("Edit Options"); setName(""); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { closeDialog(evt); } }); toolbar.setLayout(new java.awt.GridBagLayout()); jLabel1.setText("To add commands, drag them from the \"Commands\" to the \"Toolbar\"."); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 1; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(2, 4, 2, 4); toolbar.add(jLabel1, gridBagConstraints); jLabel2.setText("To remove commands, drag them from the \"Toolbar\" to the trash."); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 2; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(2, 4, 2, 4); toolbar.add(jLabel2, gridBagConstraints); jLabel3.setText("To rearrange icons, drag them around the \"Toolbar\"."); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 4; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(2, 4, 2, 4); toolbar.add(jLabel3, gridBagConstraints); jLabel4.setText("To add separators, drag the \"Sep\" icon to the \"Toolbar\"."); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 3; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(2, 4, 2, 4); toolbar.add(jLabel4, gridBagConstraints); currentToolbar.setBorder(javax.swing.BorderFactory.createTitledBorder("Toolbar:")); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 0; gridBagConstraints.gridwidth = 3; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4); toolbar.add(currentToolbar, gridBagConstraints); commandsPane.setBorder(javax.swing.BorderFactory.createTitledBorder("Commands:")); commandsPane.setMinimumSize(new java.awt.Dimension(32, 150)); commandsPane.setPreferredSize(new java.awt.Dimension(32, 150)); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 5; gridBagConstraints.gridwidth = 3; gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; gridBagConstraints.weightx = 1.0; gridBagConstraints.weighty = 1.0; toolbar.add(commandsPane, gridBagConstraints); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 1; gridBagConstraints.gridheight = 4; gridBagConstraints.insets = new java.awt.Insets(4, 20, 4, 10); toolbar.add(trashLabel, gridBagConstraints); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 2; gridBagConstraints.gridy = 1; gridBagConstraints.gridheight = 4; gridBagConstraints.insets = new java.awt.Insets(4, 10, 4, 20); toolbar.add(separatorLabel, gridBagConstraints); attachImage.setText("Attach Image to Command..."); attachImage.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { attachImageActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 6; gridBagConstraints.gridwidth = 3; gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 8); toolbar.add(attachImage, gridBagConstraints); getContentPane().add(toolbar, new java.awt.GridBagConstraints()); pack(); }// </editor-fold>//GEN-END:initComponents private void attachImageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_attachImageActionPerformed attachImageToCommand(); }//GEN-LAST:event_attachImageActionPerformed /** Closes the dialog */ private void closeDialog(java.awt.event.WindowEvent evt)//GEN-FIRST:event_closeDialog { setVisible(false); dispose(); }//GEN-LAST:event_closeDialog // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton attachImage; private javax.swing.JScrollPane commandsPane; private javax.swing.JPanel currentToolbar; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel separatorLabel; private javax.swing.JPanel toolbar; private javax.swing.JLabel trashLabel; // End of variables declaration//GEN-END:variables}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -