Results 1 to 11 of 11
- 07-02-2011, 07:29 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 5
- Rep Power
- 0
problems with program - cards show up only when I minimize/resize window
I'm making 1 program with poker cards. I got 2 problems:
1. When I click JButton "generate" nothing happens but when I click minimize of the program window or just click on the edge of the window cards show up.
2. When I click "generate" again program doesn't generate new cards.
I've been using same code for ActionListener like I used with another program when I was making simple calculator and there was everything fine. Also around words of "generate" button there is rectangle and on calculator buttons there was no rectangle.
Here is my main class ("generate" button in code is "Generiraj random karte"):
Java Code:package glavni; import java.awt.Component; import java.awt.Dimension; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.Collections; import java.util.List; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.Spring; import javax.swing.SpringLayout; public class Main extends JFrame { private static final long serialVersionUID = 1L; public static void main(String[] args) { final JFrame okvir = new JFrame("Evaluacija poker ruku"); okvir.setLayout(new SpringLayout()); okvir.setPreferredSize(new Dimension(780, 652)); final JButton generiraj = new JButton(); generiraj.setText("Generiraj random karte"); ImageIcon back = new ImageIcon ("Karte/back.gif"); final JButton prva = new JButton(); prva.setIcon(back); final JButton druga = new JButton(); druga.setIcon(back); final JButton treca = new JButton(); treca.setIcon(back); final JButton cetvrta = new JButton(); cetvrta.setIcon(back); final JButton peta = new JButton(); peta.setIcon(back); okvir.add (new JLabel()); okvir.add (generiraj, new SpringLayout.Constraints(Spring.constant(20), Spring.constant(20), Spring.constant(200), Spring.constant(40))); okvir.add (prva, new SpringLayout.Constraints(Spring.constant(10), Spring.constant(80), Spring.constant(140), Spring.constant(196))); okvir.add (druga, new SpringLayout.Constraints(Spring.constant(160), Spring.constant(80), Spring.constant(140), Spring.constant(196))); okvir.add (treca, new SpringLayout.Constraints(Spring.constant(310), Spring.constant(80), Spring.constant(140), Spring.constant(196))); okvir.add (cetvrta, new SpringLayout.Constraints(Spring.constant(460), Spring.constant(80), Spring.constant(140), Spring.constant(196))); okvir.add (peta, new SpringLayout.Constraints(Spring.constant(610), Spring.constant(80), Spring.constant(140), Spring.constant(196))); okvir.pack(); okvir.setVisible(true); class GenerirajListener implements ActionListener { public void actionPerformed (ActionEvent e){ final List<Karta> spil = new ArrayList<Karta>(); String snagaTemp = null; String bojaTemp = null; String k = null; for (int i = 1; i<5; i++){ for (int j = 2; j<14; j++){ Karta karta = new Karta(j, i, null); switch( i ) { case 1: bojaTemp = "s"; break; case 2: bojaTemp = "d"; break; case 3: bojaTemp = "h"; break; case 4: bojaTemp = "c"; break; } switch( j ) { case 2: snagaTemp = "2"; break; case 3: snagaTemp = "3"; break; case 4: snagaTemp = "4"; break; case 5: snagaTemp = "5"; break; case 6: snagaTemp = "6"; break; case 7: snagaTemp = "7"; break; case 8: snagaTemp = "8"; break; case 9: snagaTemp = "9"; break; case 10: snagaTemp = "T"; break; case 11: snagaTemp = "J"; break; case 12: snagaTemp = "Q"; break; case 13: snagaTemp = "K"; break; case 14: snagaTemp = "A"; break; } k = "Karte/"+snagaTemp+bojaTemp+".gif"; karta.setImg(k); spil.add(karta); } } Collections.shuffle(spil); ImageIcon generirana1Icon = new ImageIcon (spil.get(0).getImg()); ImageIcon generirana2Icon = new ImageIcon (spil.get(1).getImg()); ImageIcon generirana3Icon = new ImageIcon (spil.get(2).getImg()); ImageIcon generirana4Icon = new ImageIcon (spil.get(3).getImg()); ImageIcon generirana5Icon = new ImageIcon (spil.get(4).getImg()); final JButton prvaGenerirana = new JButton(); prvaGenerirana.setIcon(generirana1Icon); final JButton drugaGenerirana = new JButton(); drugaGenerirana.setIcon(generirana2Icon); final JButton trecaGenerirana = new JButton(); trecaGenerirana.setIcon(generirana3Icon); final JButton cetvrtaGenerirana = new JButton(); cetvrtaGenerirana.setIcon(generirana4Icon); final JButton petaGenerirana = new JButton(); petaGenerirana.setIcon(generirana5Icon); okvir.remove(prva); okvir.remove(druga); okvir.remove(treca); okvir.remove(cetvrta); okvir.remove(peta); okvir.add (prvaGenerirana, new SpringLayout.Constraints(Spring.constant(10), Spring.constant(80), Spring.constant(140), Spring.constant(196))); okvir.add (drugaGenerirana, new SpringLayout.Constraints(Spring.constant(160), Spring.constant(80), Spring.constant(140), Spring.constant(196))); okvir.add (trecaGenerirana, new SpringLayout.Constraints(Spring.constant(310), Spring.constant(80), Spring.constant(140), Spring.constant(196))); okvir.add (cetvrtaGenerirana, new SpringLayout.Constraints(Spring.constant(460), Spring.constant(80), Spring.constant(140), Spring.constant(196))); okvir.add (petaGenerirana, new SpringLayout.Constraints(Spring.constant(610), Spring.constant(80), Spring.constant(140), Spring.constant(196))); } } GenerirajListener gl = new GenerirajListener(); generiraj.addActionListener(gl); } }Last edited by Fubarable; 07-02-2011 at 07:31 PM. Reason: Moderator Edit: code tags added
-
Any time you add/remove components from a container you should call revalidate() on the container (if it derives from JComponent) and then repaint(). The first call tells the layout managers to layout the components present and the second tells the container to repaint itself and its children (and borders). Since you're adding to the JFrame, you're actually adding to its contentPane which is a Container variable but is almost always a JPanel. So to fix this, call
Also, I added code tags to your post to help the code retain its formatting. To see how to do this yourself for your next posts, please see the link in my signature below.Java Code:// after adding new components onto okvir... ((JPanel)okvir.getContentPane()).revalidate(); // cast it to a JPanel so we can call revalidate okvir.repaint();
Last edited by Fubarable; 07-02-2011 at 08:21 PM.
- 07-02-2011, 08:19 PM #3
Um,okvir is a JFrame.
@Revalidate the JFrame's contentPane.
db
- 07-02-2011, 08:21 PM #4
Cross posted
Problem u javi - gumb se prika
db
-
- 07-02-2011, 09:48 PM #6
Member
- Join Date
- Jul 2011
- Posts
- 5
- Rep Power
- 0
...
Tried
but Xbulb saysJava Code:((JFrame)okvir.getContentPane()).revalidate();
"The method revalidate is undefined for the type JFrame."
I got option to validate and invalidate. I tried to validate when I made first components and then revalidate after second but got same message.
Also when saving/running I'm getting message:
"Save All Failed
java.lang.NullPointerException"Last edited by AlphaScorpii; 07-02-2011 at 09:53 PM.
-
I don't see anywhere in our replies where we suggest that you cast the contentPane as a JFrame. Rather we said JPanel. Please check if your code is correct first.
So you've got another unrelated error. Best find out which line is throwing this exception and then checking to see which variable is null and why. You've got some debugging to do.Also when saving/running I'm getting message:
"Save All Failed
java.lang.NullPointerException"
- 07-02-2011, 10:25 PM #8
Member
- Join Date
- Jul 2011
- Posts
- 5
- Rep Power
- 0
Sry, from your messaging I wrongly understood that you wrote JPanel wrong and that it should go JFrame... but anyway I tried both but for JPanel I get message: "JPanel cannot be resolved to a type".
Problem is that I don't get information which line makes problem. Althought I get this message work is saved...
-
Have you imported the JPanel class? You should probably read up on importing classes and other Java basics before trying to tackle Swing as you may be trying to run before you can walk. You can find out more on it here: Creating and Using Packages
Are you sure about this? That would be a very unusual NullPointerException message if it didn't give you the line numbers that are involved. If still having a problem, post the actual exception message lines here.Problem is that I don't get information which line makes problem. Althought I get this message work is saved...
- 07-03-2011, 12:44 PM #10
Member
- Join Date
- Jul 2011
- Posts
- 5
- Rep Power
- 0
I didn't import JPanel... I don't have much time for learning, I have to be done soon and practicly everything is working except this 2 problems.
Fixed this. Problem was in class where I had comparator and made wrong compare...
btw. I tried with repaint only but only change when I click "generate" is that now back of the cards is removed but I still need to click on the edge of the window for cards to show up.
- 07-03-2011, 01:07 PM #11
Member
- Join Date
- Jul 2011
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
FrameView doesn't show new JPanel until I resize
By metazone in forum SWT / JFaceReplies: 3Last Post: 01-05-2012, 03:52 PM -
how to disable minimize and maximize button in jframe window
By santhosh_el in forum AWT / SwingReplies: 6Last Post: 11-22-2010, 11:55 AM -
Resize shell on show/hide controls.
By spacetoha in forum SWT / JFaceReplies: 1Last Post: 03-24-2009, 12:20 PM -
Window resize icon...
By pele in forum SWT / JFaceReplies: 3Last Post: 06-09-2008, 08:31 AM -
Problems with gridBaglayout when I resize the window
By Iyengar in forum AWT / SwingReplies: 1Last Post: 02-16-2008, 11:43 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks