//Titel: 1. Labor, WiSe 2017/18 // MDI-Rahmen import java.awt.*; import java.awt.event.*; import javax.swing.*; // Packages fuer Opendialog import java.io.File; import javax.swing.JFileChooser; import javax.swing.filechooser.*; public class Labor1 extends JFrame implements ActionListener { // Container fuer die Clientfenster private JDesktopPane _desktopPane = new JDesktopPane(); private static final String PREFERRED_LOOK_AND_FEEL = "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"; private static void installLnF() { try { String lnfClassname = PREFERRED_LOOK_AND_FEEL; if (lnfClassname == null) lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName(); UIManager.setLookAndFeel(lnfClassname); } catch (Exception e) { System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL + " on this platform:" + e.getMessage()); } } // Konstruktor public Labor1() { this.setSize( 900, 700); setTitle( "1. Labor, MDI Rahmen"); setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); setGUI(); } public void setGUI() { // BorderLayout setzen this.getContentPane().setLayout( new BorderLayout() ); // Variable DesktopPane einfuegen // in ihr werden alle Clientfenster eingefuegt this.getContentPane().add( _desktopPane, BorderLayout.CENTER); // _desktopPane.setBackground(Color.RED); setMenus(); setToolBars(); installLnF(); } // setzt die Menues private void setMenus() { // Variablen fuer die Menues JMenuBar menuBar1 = new JMenuBar(); JMenu mainFile= new JMenu("Datei"); JMenuItem mnOpen = new JMenuItem("Oeffnen"); JMenuItem mnClose = new JMenuItem("Schließen"); // Event definieren // Event definieren mnOpen.addActionListener(this); mnOpen.setActionCommand("OpenFile"); // Short Key definieren STRG+O mnOpen.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK)); // Menueeintraege zum MainFile hinzufuegen mainFile.add(mnOpen); mainFile.addSeparator(); // -------------------------- // Event definieren mnClose.addActionListener(this); mnClose.setActionCommand("Close"); mainFile.add(mnClose); // Hauptmenue FILE in den MenueBar einfuegen menuBar1.add(mainFile); // ---------------- // Menü Windows JMenu mainWindows = new JMenu("Fenster"); JMenuItem mnPrevWindows = new JMenuItem("Vorheriges Fenster"); JMenuItem mnNextWindows = new JMenuItem("Nächstes Fenster"); JMenuItem mnTile = new JMenuItem("Aufteilen"); JMenuItem mnTileVertical = new JMenuItem("Aufteilen Vertikal"); JMenuItem mnTileHorizontal = new JMenuItem("Aufteilen Horizontal"); JMenuItem mnCascade = new JMenuItem("Kaskade"); mnCascade.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_F6, 0)); mnTile.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_F7, 0)); mnTileVertical.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_F8, 0)); mnTileHorizontal.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_F9, 0)); // MenüEinträge definieren mainWindows.add(mnPrevWindows); mainWindows.add(mnNextWindows); mainWindows.addSeparator(); mainWindows.add(mnCascade); mainWindows.add(mnTile); mainWindows.add(mnTileVertical); mainWindows.add(mnTileHorizontal); menuBar1.add(mainWindows); // Event definieren mnPrevWindows.addActionListener(this); mnPrevWindows.setActionCommand("WinPrev"); mnNextWindows.addActionListener(this); mnNextWindows.setActionCommand("WinNext"); mnCascade.addActionListener(this); mnCascade.setActionCommand("Cascade"); mnTile.addActionListener(this); mnTile.setActionCommand("Tile"); mnTileVertical.addActionListener(this); mnTileVertical.setActionCommand("TileVertical"); mnTileHorizontal.addActionListener(this); mnTileHorizontal.setActionCommand("TileHorizontal"); menuBar1.add(mainWindows); this.setJMenuBar(menuBar1); } // setMenus private void setToolBars() { JToolBar toolBar = new JToolBar(); JButton bnOpen = new JButton("Öffnen"); toolBar.add(bnOpen); bnOpen.addActionListener(this); bnOpen.setActionCommand("OpenFile"); this.getContentPane().add(toolBar, BorderLayout.NORTH); } // setToolBars private void addClientFrame(JInternalFrame frame) { final int w = 350; final int h = 200; int pos = _desktopPane.getAllFrames().length+1; frame.setBounds( (pos*50 % (_desktopPane.getWidth()-w) ), (pos*50 % (_desktopPane.getHeight()-h) ), w, h); _desktopPane.add(frame); _desktopPane.moveToFront(frame); } public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); if (cmd.equals("OpenFile") ) { MnOpen_Click(); } if (cmd.equals("WinPrev") ) { WindowLayout.mnPrevWindows(_desktopPane); } if (cmd.equals("WinNext") ) { WindowLayout.mnNextWindows(_desktopPane); } if (cmd.equals("Cascade") ) { WindowLayout.mnCascade(_desktopPane); } if (cmd.equals("Tile") ) { WindowLayout.mnTile(_desktopPane); } if (cmd.equals("TileVertical") ) { WindowLayout.mnTileVertical(_desktopPane); } if (cmd.equals("TileHorizontal") ) { WindowLayout.mnTileHorizontal(_desktopPane); } } // actionPerformed // Beispiel zum Oeffnen EINER Datei private void MnOpen_Click() { JFileChooser jFileChooser1 = new JFileChooser(); String filenname; jFileChooser1.setMultiSelectionEnabled(true); jFileChooser1.setCurrentDirectory( new File( (String) System.getProperties().get("user.dir") ) ); // Aufruf des Dialogfensters if (JFileChooser.APPROVE_OPTION == jFileChooser1.showOpenDialog(this)) { for (File file : jFileChooser1.getSelectedFiles()) { filenname = file.getPath(); MyJInternalFrame frame = new MyJInternalFrame(filenname); addClientFrame(frame); // hier fehlt Code } } } // MnOpen // wird aufgerufen beim Schliessen des Hauptfensters private void MnClose_Click() { this.dispose(); // System.exit(0); } // Hauptprogramm static public void main(String[] args) { Labor1 frame = new Labor1(); frame.setVisible(true); } } // class labor1 class MyJInternalFrame extends JInternalFrame { protected String _filename; private MyCanvas _canvas = new MyCanvas (); private static final String PREFERRED_LOOK_AND_FEEL = "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"; private static void installLnF() { try { String lnfClassname = PREFERRED_LOOK_AND_FEEL; if (lnfClassname == null) lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName(); UIManager.setLookAndFeel(lnfClassname); } catch (Exception e) { System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL + " on this platform:" + e.getMessage()); } } public MyJInternalFrame(String filename) { _filename = filename; if (_filename.equals("")) setTitle("noname"); else setTitle(_filename); setGUI(); setVisible(true); installLnF(); } // wird aufgerufen wird Clientfenster, abgeleitet private void setGUI() { setClosable(true); setIconifiable(true); setMaximizable(true); setResizable(true); setLayout( new BorderLayout() ); add(_canvas, BorderLayout.CENTER); } public void setFilename(String filename) { _filename = filename; setTitle( filename ); } // wird aufgerufen vom MainFenster public String getFilename() { return _filename; } public void save() { } } // MyInternalFrame class MyCanvas extends Canvas { int color; public MyCanvas() { int red = (int) (Math.random()*255); int green = (int) (Math.random()*255); int blue = (int) (Math.random()*255); color = (red<<16) | (green<<8) | blue; System.out.println(color+""+" red: "+red+" green: "+green+" blue: "+blue); } public void paint (Graphics g) { // Ausgabe der normalen Grafik (Menues, Schalterleisten, etc. ). // this.paintComponents(g); // super.paint(g); // weißer Hintergrund g.setColor( new Color(color)); g.fillRect(0,0, this.getWidth(), this.getHeight()); g.setColor( new Color(~color) ); Graphics2D g2 = (Graphics2D) g; // g2.setStroke( new BasicStroke(4.0f, 2.0f) ); float[] dash = { 20, 10 }; // new float[]{ 20,10 } // 1 = miterlimit // 0 =dash_phase, Verschiebung beim Start BasicStroke stroke = new BasicStroke( 12, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1, dash, 10 ); g2.setStroke( stroke ); g.drawLine(10,10, getWidth()-10,getHeight()-10) ; } // paint } class WindowLayout { public static void mnPrevWindows(JDesktopPane desktopPane) { JInternalFrame[] frames = desktopPane.getAllFrames(); if(frames.length > 1) { desktopPane.selectFrame(false); } } public static void mnNextWindows(JDesktopPane desktopPane) { JInternalFrame[] frames = desktopPane.getAllFrames(); if(frames.length > 1) { desktopPane.selectFrame(true); } } private static void setCascadeFrame(JDesktopPane desktopPane) { final int w = 250; final int h = 150; JInternalFrame frame; // the actual Clientwindow JInternalFrame[] ff; // Array of all internal Clientwindow ff = desktopPane.getAllFrames(); for (int pos = 0; pos 0) { if (n<=3) Abstand = 80; else { if (n>6) Abstand = 25; else Abstand = 40; } // calc the width and height of each frame w = desktopPane.getWidth() - (n-1)*Abstand; h = desktopPane.getHeight() - (n-1)*Abstand; if ( (w<80) || (h<80) || (n>20) ) { setCascadeFrame(desktopPane); } else { if (w<=0) w = 100; if (h<=0) h = 50; // the top frame has the index 0 ! // index i is for the position // index j ist for the frames, the last is the top-frame ! for (i = 0, j=n-1; i 0 ? if (ff.length > 0) { // Quadratische Aufteilung /* 5: - - - - - 11: - - - - - - - - - - - */ n = ff.length; if (n < 1) { return; } if (n == 1) { f = (JInternalFrame)ff[0]; try { f.setMaximum(true); } catch (Exception e1) { e1.printStackTrace(); } return; } anzCol = (int)Math.round(Math.sqrt(n)); // Wurzel aus Anzahl // bestimme Anzahl der unteren (vollen) Zeilen anzRowUnten = n / anzCol; // trunc // Abfrage nach Rest if (n > (anzRowUnten * anzCol)) { anzRow = anzRowUnten + 1; bZweiBreiten = true; } else { anzRow = anzRowUnten; bZweiBreiten = false; } // AnzRow zeigt die Anzahl aller Zeilen // nun werden zwei Breiten und eine Höhe ausgerechnet // 1. Breite gilt für die vollen Reihen // 2. Breite gilt für oberste Reihe, falls sie nicht voll ist hoehe = desktopPane.getHeight(); h = desktopPane.getHeight() / anzRow; b1 = desktopPane.getWidth() / anzCol; // volle Breite if (bZweiBreiten) b2 = desktopPane.getWidth() / (n - anzCol * anzRowUnten); // untere Breite else b2 = b1; // Rückwärts anz = 0; left = 0; top = hoehe - h; indexCol = 0; for (i = ff.length - 1; i >= 0; i--) { f = (JInternalFrame)ff[i]; //f.setSize(100,100); try { f.setMaximum(false); } catch (Exception e2) { e2.printStackTrace(); } anz++; indexCol++; if (indexCol > anzCol) { // neue Reihe anfangen left = 0; top -= h; indexCol = 1; } // Position im Fenster setzen f.setLocation(left, top); // Breite Höhe setzen // Check, ob man in der obersten Zeile sich befindet ? if (anz > (anzRowUnten * anzCol)) { f.setSize(b2, h); // width height left += b2; } else { f.setSize(b1, h); // width height left += b1; } } } } // MnTile public static void mnTileHorizontal(JDesktopPane desktopPane) { JInternalFrame[] ff; // Array for all internal frames JInternalFrame f; // int hoehe, breite, top; int i, n; ff = desktopPane.getAllFrames(); // Count > 0 ? if (ff.length > 0) { n = ff.length; breite = desktopPane.getWidth() ; hoehe = desktopPane.getHeight()/ n; if (ff.length==1 ) { f = (JInternalFrame)ff[0]; try { f.setMaximum(false); f.setSize(desktopPane.getWidth(), hoehe); f.setLocation(0,0); return; } catch (Exception e2) { e2.printStackTrace(); } } top=0; for (i = ff.length - 1; i >= 0; i--) { f = (JInternalFrame)ff[i]; //f.setSize(100,100); try { f.setMaximum(false); f.setSize(breite, hoehe); f.setLocation(0,top); // x/y top+=hoehe; } catch (Exception e2) { e2.printStackTrace(); } } } } public static void mnTileVertical(JDesktopPane desktopPane) { JInternalFrame[] ff; // Array for all internal frames JInternalFrame f; // int hoehe, breite, left, top; int i, n; ff = desktopPane.getAllFrames(); // Count > 0 ? if (ff.length > 0) { n = ff.length; breite = desktopPane.getWidth() / n; hoehe = desktopPane.getHeight(); if (ff.length==1 ) { f = (JInternalFrame)ff[0]; try { f.setMaximum(false); f.setSize(desktopPane.getWidth(), hoehe); f.setLocation(0,0); return; } catch (Exception e2) { e2.printStackTrace(); } } left=0; for (i = ff.length - 1; i >= 0; i--) { f = (JInternalFrame)ff[i]; //f.setSize(100,100); try { f.setMaximum(false); f.setSize(breite, hoehe); f.setLocation(left,0); // x/y left+=breite; } catch (Exception e2) { e2.printStackTrace(); } } } } }