Results 1 to 9 of 9
Thread: Card Layout button glitch
- 10-19-2012, 06:52 PM #1
Member
- Join Date
- Sep 2012
- Posts
- 28
- Rep Power
- 0
Card Layout button glitch
Hi,
I reacently tried using card layout, and i got it to work ,but when i try to add buttons i get these errors;
Whats wrong with the buttons?Java Code:public class CardLayoutExample { JFrame guiFrame; CardLayout cards; JPanel cardPanel; public static void main(String[] args) { // Use the event dispatch thread for Swing components EventQueue.invokeLater(new Runnable() { @Override public void run() { try { new CardLayoutExample(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } public CardLayoutExample() throws IOException { guiFrame = new JFrame(); // make sure the program exits when the frame closes guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); guiFrame.setTitle("CardLayout Example"); guiFrame.setSize(800, 600); // This will center the JFrame in the middle of the screen guiFrame.setLocationRelativeTo(null); guiFrame.setLayout(new BorderLayout()); // creating a border to highlight the JPanel areas Border outline = BorderFactory.createLineBorder(Color.black); JPanel tabsPanel = new JPanel(); tabsPanel.setBorder(outline); JButton switchCards = new JButton("Switch Card"); switchCards.setActionCommand("Switch Card"); switchCards.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { cards.next(cardPanel); } }); tabsPanel.add(switchCards); guiFrame.add(tabsPanel, BorderLayout.NORTH);// add tabs panel at top cards = new CardLayout(); cardPanel = new JPanel(); cardPanel.setLayout(cards); cards.show(cardPanel, "Fruits"); final BufferedImage img = ImageIO.read(new File( "C:/dq5-orc-king.jpg")); final BufferedImage img2 = ImageIO.read(new File( "C:/drak slime.png")); final BufferedImage img3 = ImageIO.read(new File( "C:/king slime.jpg")); JPanel firstCard = null; JButton button = new JButton(); //firstCard.add(button); //ImageIcon slimeIcon = new ImageIcon("C:/Users/Denys/workspace/HelloWorld/Dragon quest game/slime.png"); //button.setIcon(); firstCard = new JPanel() { @Override public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; int x = (this.getWidth() - img2.getWidth(null)) / 2;// get width of image and then utalize window frame int y = (this.getHeight() - img2.getHeight(null)) / 2;// get height g.drawImage(img2, x, y, null); repaint(); } }; JPanel thirdCard = null; thirdCard = new JPanel() { @Override public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; int x = (this.getWidth() - img3.getWidth(null)) / 2; int y = (this.getHeight() - img3.getHeight(null)) / 2; g.drawImage(img3, x, y, null); repaint(); } }; JPanel secondCard = null; secondCard = new JPanel() { @Override public void paint(Graphics g) { g.drawImage(img, 0, 0, null); Graphics2D g2d = (Graphics2D) g; int x = (this.getWidth() - img.getWidth(null)) / 2; int y = (this.getHeight() - img.getHeight(null)) / 2; repaint(); } }; cardPanel.add(firstCard); cardPanel.add(secondCard); cardPanel.add(thirdCard); guiFrame.add(tabsPanel, BorderLayout.NORTH);//at top guiFrame.add(cardPanel, BorderLayout.CENTER);// add to frame in center guiFrame.setVisible(true); } // All the buttons are following the same pattern // so create them all in one place. }
-
-
Re: Card Layout button glitch
Also, your JPanel's should draw in a paintComponent method override, not a paint method override, and more importantly never ever call repaint() from within a paint or paintComponent method.
- 10-19-2012, 09:12 PM #4
- 10-19-2012, 10:18 PM #5
Member
- Join Date
- Sep 2012
- Posts
- 28
- Rep Power
- 0
Re: Card Layout button glitch
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at CardLayoutExample.<init>(CardLayoutExample.java:87 )
at CardLayoutExample$1.run(CardLayoutExample.java:34)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
- 10-19-2012, 10:19 PM #6
Member
- Join Date
- Sep 2012
- Posts
- 28
- Rep Power
- 0
-
Re: Card Layout button glitch
-
Re: Card Layout button glitch
Let me guess, one of the lines is this one:
You seem to be calling this before you've added any components to the cardPanel. Not only that, you don't appear to be using String literals much less one refering to fruits" when you do in fact get around to adding components to the cardPanel.Java Code:cards.show(cardPanel, "Fruits");
- 10-22-2012, 12:51 AM #9
Member
- Join Date
- Sep 2012
- Posts
- 28
- Rep Power
- 0
Similar Threads
-
Card Layout with Buttons
By jazob in forum New To JavaReplies: 1Last Post: 01-12-2012, 11:12 PM -
Image in Card Layout
By poorbrain in forum Java AppletsReplies: 4Last Post: 03-08-2011, 06:15 PM -
Question Card Layout, Card Management
By lrichil in forum AWT / SwingReplies: 1Last Post: 04-22-2010, 10:11 AM -
Button Glitch
By dunafrothint in forum AWT / SwingReplies: 4Last Post: 02-16-2010, 04:06 PM -
Card Layout
By Gilbee in forum NetBeansReplies: 3Last Post: 03-03-2009, 09:37 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks