1Danke
ERLEDIGT
NEIN
NEIN
ANTWORTEN
4
4
ZUGRIFFE
574
574
EMPFEHLEN
-
20.01.12 16:59 #1
- Registriert seit
- Aug 2011
- Beiträge
- 62
Hallo liebes Forum:
Wieder mal ne Frage:
Hab mir von "The Code Project" http://www.codeproject.com/KB/tabs/JTabbedPane.aspx
den code geholt und habe dazu eine Frage:
Hier folgt mal der code für die Tabbearbeitung:
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
package test5; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Component; import java.awt.Cursor; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import javax.swing.*; public class TabControl extends JTabbedPane{ private TabCloseUI closeUI = new TabCloseUI(this); public void paint(Graphics g){ super.paint(g); closeUI.paint(g); } public void addTab(String title, Component component) { super.addTab(title+" ", component); } public String getTabTitleAt(int index) { return super.getTitleAt(index).trim(); } private class TabCloseUI implements MouseListener, MouseMotionListener { private TabControl tabbedPane; private int closeX = 0 ,closeY = 0, meX = 0, meY = 0; private int selectedTab; private final int width = 8, height = 8; private Rectangle rectangle = new Rectangle(0,0,width, height); private TabCloseUI(){} public TabCloseUI(TabControl pane) { tabbedPane = pane; tabbedPane.addMouseMotionListener(this); tabbedPane.addMouseListener(this); } public void mouseEntered(MouseEvent me) {} public void mouseExited(MouseEvent me) {} public void mousePressed(MouseEvent me) {} public void mouseClicked(MouseEvent me) {} public void mouseDragged(MouseEvent me) {} public void mouseReleased(MouseEvent me) { if(closeUnderMouse(me.getX(), me.getY())){ boolean isToCloseTab = tabAboutToClose(selectedTab); if (isToCloseTab && selectedTab > -1){ tabbedPane.removeTabAt(selectedTab); } selectedTab = tabbedPane.getSelectedIndex(); } } public void mouseMoved(MouseEvent me) { meX = me.getX(); meY = me.getY(); if(mouseOverTab(meX, meY)){ controlCursor(); tabbedPane.repaint(); } } private void controlCursor() { if(tabbedPane.getTabCount()>0) if(closeUnderMouse(meX, meY)){ tabbedPane.setCursor(new Cursor(Cursor.HAND_CURSOR)); if(selectedTab > -1) tabbedPane.setToolTipTextAt(selectedTab, "Close " +tabbedPane.getTitleAt(selectedTab)); } else{ tabbedPane.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); if(selectedTab > -1) tabbedPane.setToolTipTextAt(selectedTab,""); } } private boolean closeUnderMouse(int x, int y) { rectangle.x = closeX; rectangle.y = closeY; return rectangle.contains(x,y); } public void paint(Graphics g) { int tabCount = tabbedPane.getTabCount(); for(int j = 0; j < tabCount; j++) if(tabbedPane.getComponent(j).isShowing()){ int x = tabbedPane.getBoundsAt(j).x + tabbedPane.getBoundsAt(j).width -width-5; int y = tabbedPane.getBoundsAt(j).y +5; drawClose(g,x,y); break; } if(mouseOverTab(meX, meY)){ drawClose(g,closeX,closeY); } } private void drawClose(Graphics g, int x, int y) { if(tabbedPane != null && tabbedPane.getTabCount() > 0){ Graphics2D g2 = (Graphics2D)g; drawColored(g2, isUnderMouse(x,y)? Color.RED : Color.WHITE, x, y); } } private void drawColored(Graphics2D g2, Color color, int x, int y) { g2.setStroke(new BasicStroke(5,BasicStroke.JOIN_ROUND,BasicStroke.CAP_ROUND)); g2.setColor(Color.BLACK); g2.drawLine(x, y, x + width, y + height); g2.drawLine(x + width, y, x, y + height); g2.setColor(color); g2.setStroke(new BasicStroke(3, BasicStroke.JOIN_ROUND, BasicStroke.CAP_ROUND)); g2.drawLine(x, y, x + width, y + height); g2.drawLine(x + width, y, x, y + height); } private boolean isUnderMouse(int x, int y) { if(Math.abs(x-meX)<width && Math.abs(y-meY)<height ) return true; return false; } private boolean mouseOverTab(int x, int y) { int tabCount = tabbedPane.getTabCount(); for(int j = 0; j < tabCount; j++) if(tabbedPane.getBoundsAt(j).contains(meX, meY)){ selectedTab = j; closeX = tabbedPane.getBoundsAt(j).x + tabbedPane.getBoundsAt(j).width -width-5; closeY = tabbedPane.getBoundsAt(j).y +5; return true; } return false; } } public boolean tabAboutToClose(int tabIndex) { return true; } }
Und hier der Aufruf:
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
package test5; import java.awt.BorderLayout; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import test5.TabControl; public class Test5 extends JFrame { private Save tabbedPane; public Test5() { addTabbedPane(); addMenu(); setSize(500, 400); setVisible(true); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private void addMenu() { // Create menu for adding tabs JMenuBar menuBar = new JMenuBar(); JMenu menu = new JMenu("File"); JMenuItem menuItem = new JMenuItem("Add Tab"); menuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { tabbedPane.addTab("TAB " + (tabbedPane.getTabCount() + 1), new JPanel(new BorderLayout()) //********************frage************************************************ //Hier sollen diverse object eingepflanzt werden. ); JLabel l = new JLabel("This is NORTH", JLabel.CENTER); add(l, BorderLayout.NORTH); } }); menu.add(menuItem); menuBar.add(menu); setJMenuBar(menuBar); } private void addTabbedPane() { // Create ClosableTabbedPane and override the tabAboutToClose // to be notified when certain tab is going to close. tabbedPane = new TabControl() { public boolean tabAboutToClose(int tabIndex) { String tab = tabbedPane.getTabTitleAt(tabIndex); int choice = JOptionPane.showConfirmDialog(null, "You are about to close '" + tab + "'\nDo you want to proceed ?", "Confirmation Dialog", JOptionPane.INFORMATION_MESSAGE); return choice == 0; // if returned false tab closing will be // canceled } }; getContentPane().add(tabbedPane); } public static void main(String[] args) { new Test5(); } }
So meine Frage: Wie kann ich nun auf das Jpanel mehrere Objekte hinlegen. Ich habe ja keinen Namen des Jpanels, und wenn ich nur die add() Mehtode aufrufe wirds ja nur auf den frame angezeigt! ... Ich hoffe ich drüce mich halbigs verstndlich aus!
Danke schon mal wieder an euch!
lg
-
Warum legst du dir das JPanel nicht vorher an z.b. als Klassenvariable, wenn du es noch an anderer Stelle benutzen willst, ansonsten vor dem Einbinden. Dann hast du auch nen Namen.
Alternativ das JPanel als Rückgabe Wert einer Methode, in der du das erstellst ( würde man auch eher machen, wegen der Übersicht)
Code java:1 2 3 4 5
private JPanel makeJPanel(){ JPanel panel = new JPanel(new BorderLayout()); // add was du willst return panel; }
und in deinem Code machste
Code java:1
tabbedPane.addTab("TAB " + (tabbedPane.getTabCount() + 1),makeJPanel());
oder so. wenn ich dein Anliegen richtig verstanden hab.
-
21.01.12 15:21 #3
- Registriert seit
- Aug 2011
- Beiträge
- 62
genau nach dem hab ich gesucht! Danke vielmals! lg
-
21.01.12 21:57 #4
- Registriert seit
- Aug 2011
- Beiträge
- 62
OKay, hab das jetzt mal alles so halbigs hingebracht wie ich das wollte!
Allerdings das neu erzeugt panel erstreckt sich über den ganzen Frame!
Wie kann ich dem Panel eine Größe geben?
mit
Code :1 2
setLayout(null); tabbedPane.setBounds(10,10,60,60);
komm ich irgendwie nicht hin! den sobald ich das Layout auf null setzte, wird gar nichts mehr angezeigt!
ich habe es mal anderes probier
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
package probe; import java.awt.Button; import java.awt.Color; import java.awt.Container; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.*; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTabbedPane; public class Probe extends JFrame{ JButton btClose; public Probe(){ Container c = this.getContentPane(); JTabbedPane tpane = new JTabbedPane(); tpane.addTab("Tab1", new JPanel()); tpane.addTab("Tab2", new JPanel()); tpane.setBounds(0, 0, 200, 200); this.setLayout(null); this.add(tpane); this.setSize(500, 500); this.setVisible(true); } public static void main(String[] args){ Probe demo = new Probe(); demo.setVisible(true); } }
In diesem Beispiel funtkionierts! Warum in dem obrigen von mir geposteten Beispiel nicht?
Danke!
Lg
-
23.01.12 08:07 #5
Hi,
du solltest dir vielleicht zu dem Thema mal die verschiedenen Layouts unter Swing ansehen. Dann kommst du schon von ganz alleine auf die Lösung.
Gruß
FabioBitte die Code-Tags verwenden. Bei Java-Code: [java]...[/java]
Tutorials:
Automatisches erzeugen eines Inhaltsverzeichnisses (Javascript)
JAnimationPanel - Animationen für Swing/AWT
SWTRatingBar (Bewertungs-Composite) selbst programmieren
____________________________________________________________________________
Über eine Bewertung (Stern links unter dem Beitrag) oder ein Danke freue ich mich sehr.
Ähnliche Themen
-
JTabbedPane
Von Edgar2010 im Forum Swing, Java2D/3D, SWT, JFaceAntworten: 0Letzter Beitrag: 20.09.11, 10:27 -
JTabbedPane
Von Floffy im Forum Swing, Java2D/3D, SWT, JFaceAntworten: 5Letzter Beitrag: 26.08.08, 12:12 -
JTabbedPane: Event auf Tab...?
Von Kestrel im Forum Swing, Java2D/3D, SWT, JFaceAntworten: 2Letzter Beitrag: 23.11.07, 14:14 -
DefaultTableModel und JTabbedPane
Von marcelh im Forum Swing, Java2D/3D, SWT, JFaceAntworten: 0Letzter Beitrag: 26.10.07, 10:31 -
JTabbedPane
Von Knödel im Forum Swing, Java2D/3D, SWT, JFaceAntworten: 4Letzter Beitrag: 28.04.04, 17:58





Zitieren
Login





