Results 1 to 5 of 5
- 12-31-2010, 11:01 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 29
- Rep Power
- 0
How to change position of JButtons in a CardLayout?
And when the function is calledJava Code:... JPanel cards; //a panel that uses CardLayout Button BtGotoCard2; //awt button for test BUT DOESNT CHANGES POSITION EITHER Button BtGotoCard1; JButton myButton = new JButton("JB Go to Card2"); public void addComponentToPane(Container pane) { JPanel card1 = new JPanel(); BtGotoCard2 = new Button("Go to Card2"); [COLOR="Red"]BtGotoCard2.setBounds(50,150,80,20);[/COLOR] [COLOR="Red"]myButton.setBounds(50,150,80,20);[/COLOR] card1.add(BtGotoCard2); card1.add(new JButton("J Button 1")); card1.add(myButton); myButton.addActionListener(this); BtGotoCard2.addActionListener(this); BtGotoCard1 = new Button("Go to Card1"); card1.add(BtGotoCard1); BtGotoCard1.addActionListener(this); JPanel card2 = new JPanel(); card2.add(new JTextField("TextField", 20)).setBackground(LightBez); card2.add(BtGotoCard1); //Create the panel that contains the "cards". cards = new JPanel(new CardLayout()); cards.add(card1, "Enter PIN"); cards.add(card2, "Eidos Logariasmou"); pane.add(cards); }
Set bounds works with the frame ( even if not for the size) but not for the JButtons. What can i do?Java Code:private static void createAndShowGUI() { //Create and set up the window. JFrame frame = new JFrame("CardLayoutDemo"); frame.setBounds(450,250,380,300); frame.setPreferredSize(new Dimension(380, 300)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create and set up the content pane. CardLayoutDemo demo = new CardLayoutDemo(); //demo.setBounds(450,250,380,300); demo.addComponentToPane(frame.getContentPane()); //Display the window. frame.pack(); frame.setVisible(true); }Last edited by esgol; 12-31-2010 at 11:04 AM. Reason: styling
- 12-31-2010, 11:06 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
A CardLayout sets each of its components ("cards") to the same size. If those components are, say, JPanels, you can add other components to them using any LayoutManager your want (even none). Those other components can be JButtons or whatevev. b.t.w. don't use Buttons, use JButtons instead.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-31-2010, 11:23 AM #3
Member
- Join Date
- Dec 2010
- Posts
- 29
- Rep Power
- 0
Even None and it will work?
I've imagine tha all other elements inheritate the cardlayout's layout
So i can put other elements by specifing none layout? And i will be freed from the bounds of the cardlayout?
Like doing
myButton.setLayout(null);
myButton.setBounds(50,150,80,20);
card1.add(myButton);
I ve done it inside the constructor but its stil bound to the cardlayout
- 12-31-2010, 11:39 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
If you add your myButton to the JComponent with the CardLayout it will have the size of the entire component. It doesn't matter whether or not you've set the LayoutManager of the JButton to null. Add some JPanels (without a LayoutManager) to the JComponent with the CardLayout (also a JPanel?) and add components to the other JPanels. Use the CardLayout to display one of the JPanels. So:
kind regards,Java Code:CardLayout cards= new CardLayout(); JPanel mainPanel= new JPanel(cards); // create the main panel JPanel panel1= new JPanel(null); // create two cards JPanel panel2= new JPanel(null); mainPanel.add(panel1, "panel1"); // add them to the main panel mainPanel.add(panel2, "panel2"); // add other components to panel1 and panel2 here // note that the panels don't have a LayoutManager ... // use cards.show(mainPanel, "panel1") to show panel1
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-31-2010, 12:15 PM #5
Member
- Join Date
- Dec 2010
- Posts
- 29
- Rep Power
- 0
IT WORKS! :D IT WORKS!!! :D :D :D
And into the Action Listenet AfterwardsJava Code://CONSTRUCTOR TYPES DECLARATION CardLayout cards= new CardLayout(); //a panel that uses CardLayout JPanel MainPanel= new JPanel(cards); // create the main panel Button BtGotoCard2; Button BtGotoCard1; JTextField TextField1 = new JTextField("TextField1", 20); public void addComponentToPane(Container pane) { ... JPanel card1 = new JPanel(null); MainPanel.add(card1, "Enter PIN"); BtGotoCard2 = new Button("Go to Card2"); BtGotoCard2.addActionListener(this); BtGotoCard2.setBounds(50,50,80,20); myButton.setBounds(150,100,80,20); card1.add(BtGotoCard2); card1.add(myButton); //------------------------------------ JPanel card2 = new JPanel(null); MainPanel.add(card2, "Eidos Logariasmou"); BtGotoCard1 = new Button("Go to Card1"); BtGotoCard1.setBounds(50,50,80,20); TextField1.setBounds(100,100,80,20); BtGotoCard1.addActionListener(this); card2.add(BtGotoCard1); card2.add(TextField1); ... pane.add(MainPanel); }
NOW I'M GONNA HAVE A SUPER ATM INTERFACE :D :D :D :DJava Code:public void actionPerformed(ActionEvent e) { if (e.getSource()==BtGotoCard2) { System.out.println("pathitike to Card2"); //CardLayout cl = (MainPanel)(cards.getLayout()); cards.show(MainPanel, "Eidos Logariasmou"); //cl.show(cards,"Eidos Logariasmou" ); } if (e.getSource()==myButton) { System.out.println("Anagnoristike pathma myButton apo to if getsource"); //CardLayout cl = (CardLayout)(cards.getLayout()); cards.show(MainPanel, "Eidos Logariasmou"); } if (e.getSource()==BtGotoCard1) { System.out.println("pathitike to GotoCard1"); //CardLayout cl = (CardLayout)(cards.getLayout()); cards.show(MainPanel, "Enter PIN"); } }
THANK YOU ALL OF YOU :D
Similar Threads
-
How to I change the position in arrays
By LennyKosmos in forum New To JavaReplies: 3Last Post: 09-27-2010, 10:04 PM -
Change the Font Size and Text Position
By stayfierce in forum New To JavaReplies: 1Last Post: 12-03-2009, 05:17 AM -
get position in string from caret position
By helloworld111 in forum AWT / SwingReplies: 5Last Post: 02-19-2009, 01:36 AM -
Regarding CardLayout
By adeeb in forum AWT / SwingReplies: 1Last Post: 06-07-2008, 07:52 PM -
how to change the appearance of jbuttons
By katie in forum AWT / SwingReplies: 1Last Post: 08-06-2007, 10:26 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks