ComponentsContainer
Aus Byte-Welt Wiki
Version vom 16. Dezember 2010, 15:23 Uhr von AndreUhres (Diskussion | Beiträge) (ComponentsContainer hat Null-Layout und ist dazu gedacht um JComponentBounds aufzunehmen (beweglich und Größe veränderbar).)
ComponentsContainer hat Null-Layout und ist dazu gedacht um JComponentBounds aufzunehmen (beweglich und Größe veränderbar). Er kann versteckte Komponenten durch Mausklick nach oben bringen (setComponentZOrder). Die aktuelle JComponentBounds kann gelöscht werden wenn sie fokusiert ist (über Löschtaste oder Kontextmenü). Der ComponentsContainer kann seinen Inhalt skalieren zu dem Zweck ihn zu drucken. Er kann auch ein Raster zeichnen.
Eine Demo ist in der main Methode enthalten. <code=java> /*
* ComponentsContainer.java * * Uses the class JComponentBounds: * http://wiki.byte-welt.net/wiki/JComponentBounds * *Has Null-Layout and is supposed to contain JComponentBounds (moveable and resizable). *Takes hidden components to the top with mouse click (setComponentZOrder). *The current JComponentBounds can be deleted if focused (using delete key or popup menu). *Can scale its contents for printing. *Can paint a grid. * * A demo is included within main method. * * author: Andre Uhres * last update September 17, 2008 at 17:30 */
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*;
public class ComponentsContainer extends JPanel implements ActionListener {
private JComponentBounds current; private JMenuItem itemDelete; private JPopupMenu popup; private double scale = 1.0d; private boolean zOrder;
public ComponentsContainer() { super(null);
setBackground(Color.WHITE); //the following MouseListener only serves to take hidden Components //to the top (setComponentZOrder): addMouseListener(new MouseInputAdapter() {
@Override public void mousePressed(final MouseEvent e) { zOrder = true; }
@Override public void mouseReleased(final MouseEvent e) { if (e.isPopupTrigger() && current != null && current.isFocused()) { popup.show(ComponentsContainer.this, current.getX(), current.getY()); return; } adaptZorder(e.getSource(), e.getPoint()); } }); //the following KeyListener only serves to delete the component last clicked //and being on top: addKeyListener(new KeyAdapter() {
@Override public void keyPressed(final KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_DELETE) { delete(); } } }); popup = new JPopupMenu(); itemDelete = new JMenuItem("Delete component"); popup.add(itemDelete); itemDelete.addActionListener(this); }
public void adaptZorder(Object source, Point pos) { Component[] comps = getComponents(); for (int i = 0; i < comps.length; i++) { ((JComponentBounds) comps[i]).setFocused(false); } if (source instanceof ComponentsContainer) { return; } JComponentBounds f = (JComponentBounds) source; pos.translate(f.getX(), f.getY()); Component component = null; for (int i = 0; i < comps.length; i++) { Rectangle rect = comps[i].getBounds(); if (rect.contains(pos)) { component = comps[i]; } } if (component != null && zOrder) { current = (JComponentBounds) component; setComponentZOrder(current, 0); } if (current != null) { current.setFocused(true); } repaint(); }
@Override public void print(final Graphics g) { ((Graphics2D) g).scale(scale, scale); super.print(g); }
@Override public void actionPerformed(final ActionEvent e) { Object source = e.getSource(); if (source == itemDelete) { delete(); } }
private void delete() { if (current != null && current.isFocused()) { remove(current); revalidate(); repaint(); current = null; } }
public void setScale(final double scale) { this.scale = scale; }
public void setCurrent(final JComponentBounds current) { this.current = current; }
public void setZorderAllowed(final boolean b) { zOrder = b; }
//main method for demo only: public static void main(String[] args) { JFrame f = new JFrame("ComponentsContainer Demo"); f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); f.setSize(700, 600); f.setLocationRelativeTo(null); JButton button = new JButton("Hello"); JScrollPane scrollpane = new JScrollPane(new JTable(4, 4)); scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); JLabel label = new JLabel("Test Label"); label.setPreferredSize(new Dimension(100, 50)); label.setOpaque(true); label.setBackground(Color.YELLOW); final JComponentBounds labelBounds = new JComponentBounds(label); labelBounds.setMinimumSize(new Dimension(50, 20)); ComponentsContainer container = new ComponentsContainer(); container.add(labelBounds); container.add(new JComponentBounds(button, 150, 50, true)); container.add(new JComponentBounds(scrollpane, 100, 100, true)); f.add(container); f.setVisible(true); }
}
</code=java>