下面列出了javax.swing.JComponent#setOpaque ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void installUI(JComponent c) {
super.installUI(c);
c.setForeground(COLOR_DEFAULT_FOREGROUND);
c.setBackground(COLOR_DEFAULT_BACKGROUND);
c.setOpaque(false);
progressBar.addChangeListener(pulseChangeListener);
progressBar.addChangeListener(sparkChangeListener);
progressBar.addPropertyChangeListener(PROPERTY_STROKE_MULTIPLIER,
repaintListener);
progressBar.addPropertyChangeListener(PROPERTY_SPARK_ANGLE,
repaintListener);
progressBar.addPropertyChangeListener(PROPERTY_ACCELERATE,
repaintListener);
progressBar.addPropertyChangeListener(PROPERTY_STROKE_WIDTH,
repaintListener);
progressBar.setBorder(null);
}
/**
* Create the GUI and show it. For thread safety, this method should be
* invoked from the event-dispatching thread.
*/
public static JFrame createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("DropDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create and set up the content pane.
JComponent newContentPane = new DropDemo();
newContentPane.setOpaque(true); // content panes must be opaque
frame.setContentPane(newContentPane);
// Display the window.
frame.pack();
frame.setVisible(true);
return frame;
}
public void addItem(JComponent c) {
c.setOpaque(false);
if (c instanceof JButton)
((JButton)c).setDefaultCapable(false);
if (toolbar != null) {
toolbar.add(c);
} else {
add(c);
if (c instanceof AbstractButton) {
AbstractButton b = (AbstractButton) c;
b.addMouseListener(listener);
b.addChangeListener(listener);
b.addFocusListener(listener);
b.setRolloverEnabled(true);
}
}
}
@Override
public final void dragGestureRecognized(final DragGestureEvent dge) {
// final TreePath path = tree.getSelectionPath();
final TreePath path = tree.getPathForLocation(dge.getDragOrigin().x, dge.getDragOrigin().y);
if (path != null) {
draggedNode = (DefaultMutableTreeNode) path.getLastPathComponent();
draggedNodeParent = (DefaultMutableTreeNode) draggedNode.getParent();
if (drawImage) {
final Rectangle pathBounds = tree.getPathBounds(path); // getpathbounds of selectionpath
final JComponent lbl =
(JComponent) tree.getCellRenderer().getTreeCellRendererComponent(tree, draggedNode,
false, tree.isExpanded(path),
((DefaultTreeModel) tree.getModel()).isLeaf(path.getLastPathComponent()), 0, false);// returning
// the
// label
lbl.setBounds(pathBounds);// setting bounds to lbl
image =
new BufferedImage(lbl.getWidth(), lbl.getHeight(),
java.awt.image.BufferedImage.TYPE_INT_ARGB_PRE);// buffered image reference passing
// the label's ht and width
final Graphics2D graphics = image.createGraphics();// creating the graphics for buffered
// image
graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f)); // Sets
// the
// Composite
// for the
// Graphics2D
// context
lbl.setOpaque(false);
lbl.paint(graphics); // painting the graphics to label
graphics.dispose();
}
dragSource.startDrag(dge, DragSource.DefaultMoveNoDrop, image, new Point(0, 0),
new TransferableNode(draggedNode), this);
}
}
@Override
public void installUI(JComponent c) {
initializeUIManager();
super.installUI(c);
c.setOpaque(false);
c.setBorder(new BasicBorders.MarginBorder());
}
/**
* Should be called shortly after <code>prep()</code>.
*/
private static void restore(Component c) {
if (c instanceof JComponent) {
JComponent jc = (JComponent) c;
Boolean b = (Boolean) jc.getClientProperty(USED_TO_BE_OPAQUE);
if (b != null && b.booleanValue()) {
jc.setOpaque(true);
}
jc.putClientProperty(USED_TO_BE_OPAQUE, null);
Dimension d = (Dimension) jc.getClientProperty(SIZE);
if (d != null) {
jc.setSize(d);
jc.putClientProperty(SIZE, null);
}
}
if (c instanceof JSlider) {
JSlider s = (JSlider) c;
ChangeListener[] listeners = (ChangeListener[]) s
.getClientProperty(CHANGE_LISTENERS);
Integer i = (Integer) s.getClientProperty(SLIDER_VALUE);
if (i != null)
s.setValue(i.intValue());
if (listeners != null) {
for (int a = 0; a < listeners.length; a++) {
s.addChangeListener(listeners[a]);
}
}
s.putClientProperty(SLIDER_VALUE, null);
s.putClientProperty(CHANGE_LISTENERS, null);
}
if (c instanceof Container) {
Container c2 = (Container) c;
for (int a = 0; a < c2.getComponentCount(); a++) {
restore(c2.getComponent(a));
}
}
}
private static void createAndShowGUI(JPanel jtop) {
JFrame frame = new JFrame("GfxdTop");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent contentPane = (JComponent) frame.getContentPane();
contentPane.add(jtop, BorderLayout.CENTER);
contentPane.setOpaque(true);
contentPane.setBorder(new EmptyBorder(12, 12, 12, 12));
frame.setContentPane(contentPane);
frame.pack();
frame.setVisible(true);
}
protected JComponent createEditor()
{
JComponent e = super.createEditor();
e.setOpaque(false);
//参见JSpinner.NumberEditor,super.createEditor()返回值就是它的父类
//(是一个JPanel实例),它是由一个FormatttedTextField及其父JPanel组成
//的,所以设置完 e.setOpaque(false),还要把它的子FormatttedTextField
//设置成透明,其实它的子只有1个,它里为了适用未来的扩展假设它有很多子,
Component[] childs = e.getComponents();
BEUtils.componentsOpaque(childs, false);
return e;
}
private static void setOpaque(JComponent jc, boolean opaque) {
if (jc instanceof JTextField)
return;
jc.setOpaque(false);
if (jc instanceof JSpinner)
return;
for (int a = 0; a < jc.getComponentCount(); a++) {
JComponent child = (JComponent) jc.getComponent(a);
setOpaque(child, opaque);
}
}
/**
* Create the GUI and show it. For thread safety, this method should be
* invoked from the event-dispatching thread.
*/
private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("TableToolTipsDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create and set up the content pane.
JComponent newContentPane = new TableToolTipsDemo();
newContentPane.setOpaque(true); // content panes must be opaque
frame.setContentPane(newContentPane);
// Display the window.
frame.pack();
frame.setVisible(true);
}
private static void createAndShowGUI(JPanel jtop) {
JFrame frame = new JFrame("GfxdTop");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent contentPane = (JComponent) frame.getContentPane();
contentPane.add(jtop, BorderLayout.CENTER);
contentPane.setOpaque(true);
contentPane.setBorder(new EmptyBorder(12, 12, 12, 12));
frame.setContentPane(contentPane);
frame.pack();
frame.setVisible(true);
}
/**
* Set up component.
*/
protected void initialize() {
JComponent component = getComponent();
component.setToolTipText(label.getToolTipText());
component.setEnabled(editable);
component.setOpaque(false);
}
protected void paintComponent(Graphics g) {
JTableHeader header = getHeader();
setupHeader(header);
TableCellRenderer renderer = header.getDefaultRenderer();
JComponent component = (JComponent)renderer.getTableCellRendererComponent(
getTable(), "", isSelected && isPressed, isFocusOwner(), -1, 0); // NOI18N
int height = header.getPreferredSize().height;
component.setBounds(0, 0, getWidth(), height);
component.setOpaque(false);
getPainter().paintComponent(g, component, null, 0, 0, getWidth(), height, false);
}
protected void paintComponent(Graphics g) {
JTableHeader header = getHeader();
setupHeader(header);
TableCellRenderer renderer = header.getDefaultRenderer();
JComponent component = (JComponent)renderer.getTableCellRendererComponent(
getTable(), "", isSelected && isPressed, isFocusOwner(), -1, 0); // NOI18N
int height = header.getPreferredSize().height;
component.setBounds(0, 0, getWidth(), height);
component.setOpaque(false);
getPainter().paintComponent(g, component, null, 0, 0, getWidth(), height, false);
}
public DiagramScene(Action[] actions, DiagramViewModel model) {
this.actions = actions;
selectedWidgets = new ArrayList<FigureWidget>();
content = new InstanceContent();
lookup = new AbstractLookup(content);
this.setCheckClipping(true);
this.getInputBindings().setZoomActionModifiers(0);
JComponent comp = this.createView();
comp.setDoubleBuffered(true);
comp.setBackground(Color.WHITE);
comp.setOpaque(true);
this.setBackground(Color.WHITE);
this.setOpaque(true);
scrollPane = new JScrollPane(comp);
scrollPane.setBackground(Color.WHITE);
scrollPane.getVerticalScrollBar().setUnitIncrement(SCROLL_UNIT_INCREMENT);
scrollPane.getVerticalScrollBar().setBlockIncrement(SCROLL_BLOCK_INCREMENT);
scrollPane.getHorizontalScrollBar().setUnitIncrement(SCROLL_UNIT_INCREMENT);
scrollPane.getHorizontalScrollBar().setBlockIncrement(SCROLL_BLOCK_INCREMENT);
scrollPane.getViewport().addChangeListener(scrollChangeListener);
hoverAction = this.createWidgetHoverAction();
blockLayer = new LayerWidget(this);
this.addChild(blockLayer);
startLayer = new LayerWidget(this);
this.addChild(startLayer);
// TODO: String startLabelString = "Loading graph with " + originalDiagram.getFigures().size() + " figures and " + originalDiagram.getConnections().size() + " connections...";
String startLabelString = "";
LabelWidget w = new LabelWidget(this, startLabelString);
scrollChangeListener.register(w, new Point(10, 10));
w.setAlignment(LabelWidget.Alignment.CENTER);
startLabel = w;
startLayer.addChild(w);
mainLayer = new LayerWidget(this);
this.addChild(mainLayer);
topLeft = new Widget(this);
topLeft.setPreferredLocation(new Point(-BORDER_SIZE, -BORDER_SIZE));
this.addChild(topLeft);
bottomRight = new Widget(this);
bottomRight.setPreferredLocation(new Point(-BORDER_SIZE, -BORDER_SIZE));
this.addChild(bottomRight);
slotLayer = new LayerWidget(this);
this.addChild(slotLayer);
connectionLayer = new LayerWidget(this);
this.addChild(connectionLayer);
LayerWidget selectionLayer = new LayerWidget(this);
this.addChild(selectionLayer);
this.setLayout(LayoutFactory.createAbsoluteLayout());
this.getActions().addAction(hoverAction);
zoomAction = new BoundedZoomAction(1.1, false);
zoomAction.setMaxFactor(ZOOM_MAX_FACTOR);
zoomAction.setMinFactor(ZOOM_MIN_FACTOR);
this.getActions().addAction(ActionFactory.createMouseCenteredZoomAction(1.1));
panAction = new ExtendedPanAction();
this.getActions().addAction(panAction);
this.getActions().addAction(ActionFactory.createPopupMenuAction(popupMenuProvider));
LayerWidget selectLayer = new LayerWidget(this);
this.addChild(selectLayer);
this.getActions().addAction(ActionFactory.createRectangularSelectAction(rectangularSelectDecorator, selectLayer, rectangularSelectProvider));
blockWidgets = new HashMap<InputBlock, BlockWidget>();
boolean b = this.getUndoRedoEnabled();
this.setUndoRedoEnabled(false);
this.setNewModel(model);
this.setUndoRedoEnabled(b);
}
/** Set the background on a component. */
private JComponent setBackground(JComponent comp, Color c) {
comp.setOpaque(true);
comp.setBackground(c);
return comp;
}
@Override
public void installUI(JComponent c) {
super.installUI(c);
c.setOpaque(false);
}
@Override
public void installUI(JComponent c) {
super.installUI(c);
c.setOpaque(false);
}
/**
* Adds a component on this Canvas inside a frame.
*
* @param comp The component to add to the canvas.
* @param toolBox Should be set to true if the resulting frame is
* used as a toolbox (that is: it should not be counted as a
* frame).
* @param popupPosition A preferred {@code PopupPosition}.
* @param resizable Whether this component can be resized.
* @return The {@code JInternalFrame} that was created and added.
*/
private JInternalFrame addAsFrame(JComponent comp, boolean toolBox,
PopupPosition popupPosition,
boolean resizable) {
final int FRAME_EMPTY_SPACE = 60;
final JInternalFrame f = (toolBox) ? new ToolBoxFrame()
: new JInternalFrame();
Container con = f.getContentPane();
if (con instanceof JComponent) {
JComponent c = (JComponent)con;
c.setOpaque(false);
c.setBorder(null);
}
if (comp.getBorder() != null) {
if (comp.getBorder() instanceof EmptyBorder) {
f.setBorder(Utility.blankBorder(10, 10, 10, 10));
} else {
f.setBorder(comp.getBorder());
comp.setBorder(Utility.blankBorder(5, 5, 5, 5));
}
} else {
f.setBorder(null);
}
final FrameMotionListener fml = new FrameMotionListener(f);
comp.addMouseMotionListener(fml);
comp.addMouseListener(fml);
if (f.getUI() instanceof BasicInternalFrameUI) {
BasicInternalFrameUI biu = (BasicInternalFrameUI) f.getUI();
biu.setNorthPane(null);
biu.setSouthPane(null);
biu.setWestPane(null);
biu.setEastPane(null);
}
f.getContentPane().add(comp);
f.setOpaque(false);
f.pack();
int width = f.getWidth();
int height = f.getHeight();
if (width > getWidth() - FRAME_EMPTY_SPACE) {
width = Math.min(width, getWidth());
}
if (height > getHeight() - FRAME_EMPTY_SPACE) {
height = Math.min(height, getHeight());
}
f.setSize(width, height);
Point p = chooseLocation(comp, width, height, popupPosition);
f.setLocation(p);
this.addToCanvas(f, MODAL_LAYER);
f.setName(comp.getClass().getSimpleName());
f.setFrameIcon(null);
f.setVisible(true);
f.setResizable(resizable);
try {
f.setSelected(true);
} catch (java.beans.PropertyVetoException e) {}
return f;
}
public DiagramScene(Action[] actions, DiagramViewModel model) {
this.actions = actions;
selectedWidgets = new ArrayList<FigureWidget>();
content = new InstanceContent();
lookup = new AbstractLookup(content);
this.setCheckClipping(true);
this.getInputBindings().setZoomActionModifiers(0);
JComponent comp = this.createView();
comp.setDoubleBuffered(true);
comp.setBackground(Color.WHITE);
comp.setOpaque(true);
this.setBackground(Color.WHITE);
this.setOpaque(true);
scrollPane = new JScrollPane(comp);
scrollPane.setBackground(Color.WHITE);
scrollPane.getVerticalScrollBar().setUnitIncrement(SCROLL_UNIT_INCREMENT);
scrollPane.getVerticalScrollBar().setBlockIncrement(SCROLL_BLOCK_INCREMENT);
scrollPane.getHorizontalScrollBar().setUnitIncrement(SCROLL_UNIT_INCREMENT);
scrollPane.getHorizontalScrollBar().setBlockIncrement(SCROLL_BLOCK_INCREMENT);
scrollPane.getViewport().addChangeListener(scrollChangeListener);
hoverAction = this.createWidgetHoverAction();
blockLayer = new LayerWidget(this);
this.addChild(blockLayer);
startLayer = new LayerWidget(this);
this.addChild(startLayer);
// TODO: String startLabelString = "Loading graph with " + originalDiagram.getFigures().size() + " figures and " + originalDiagram.getConnections().size() + " connections...";
String startLabelString = "";
LabelWidget w = new LabelWidget(this, startLabelString);
scrollChangeListener.register(w, new Point(10, 10));
w.setAlignment(LabelWidget.Alignment.CENTER);
startLabel = w;
startLayer.addChild(w);
mainLayer = new LayerWidget(this);
this.addChild(mainLayer);
topLeft = new Widget(this);
topLeft.setPreferredLocation(new Point(-BORDER_SIZE, -BORDER_SIZE));
this.addChild(topLeft);
bottomRight = new Widget(this);
bottomRight.setPreferredLocation(new Point(-BORDER_SIZE, -BORDER_SIZE));
this.addChild(bottomRight);
slotLayer = new LayerWidget(this);
this.addChild(slotLayer);
connectionLayer = new LayerWidget(this);
this.addChild(connectionLayer);
LayerWidget selectionLayer = new LayerWidget(this);
this.addChild(selectionLayer);
this.setLayout(LayoutFactory.createAbsoluteLayout());
this.getActions().addAction(hoverAction);
zoomAction = new BoundedZoomAction(1.1, false);
zoomAction.setMaxFactor(ZOOM_MAX_FACTOR);
zoomAction.setMinFactor(ZOOM_MIN_FACTOR);
this.getActions().addAction(ActionFactory.createMouseCenteredZoomAction(1.1));
panAction = new ExtendedPanAction();
this.getActions().addAction(panAction);
this.getActions().addAction(ActionFactory.createPopupMenuAction(popupMenuProvider));
LayerWidget selectLayer = new LayerWidget(this);
this.addChild(selectLayer);
this.getActions().addAction(ActionFactory.createRectangularSelectAction(rectangularSelectDecorator, selectLayer, rectangularSelectProvider));
blockWidgets = new HashMap<InputBlock, BlockWidget>();
boolean b = this.getUndoRedoEnabled();
this.setUndoRedoEnabled(false);
this.setNewModel(model);
this.setUndoRedoEnabled(b);
}