Results 1 to 16 of 16
- 03-14-2009, 08:47 AM #1
Member
- Join Date
- Mar 2009
- Posts
- 8
- Rep Power
- 0
Problem on adding JButton on JPanel NEED HELP
I added Jbuttons but I cant see them I tried many things, but then no luck.
Any help would be really appreciated!
Here is the code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class GameBoard extends JFrame implements Runnable
{
private Image imgBG, imgPlayers, imgDice1, imgDice2;
private ImageIcon imgButtonGreen, imgButtonRed, imgButtonRollDice;
private Image img0, img1, img2, img3, img4, img5, img6, img7, img8, img9;
private JButton btnSkill1, btnSkill2, btnSkill3, btnSkill4, btnRollDice;
private Random objRandom = new Random();
private boolean bThrowedDice, bUseSkill;
private int nCurrentPlayer, nMoves, nTemp;
private JPanel panelMain = new JPanel();
private Character[] objCharacter = new Character[3];
public GameBoard(int nNumberOfPlayers)
{
super("Sample Game Board");
//JFrame attributes
setBounds(120,80,0,0);
pack();
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//JPanel attributes
panelMain.setPreferredSize(new Dimension(800,600));
panelMain.setLayout(null);
add(panelMain);
//panelMain.add(this);
//Sets GameBoard Image
imgBG = (new ImageIcon("images/GameBoard.png").getImage());
Dimension objDimension = new Dimension(imgBG.getWidth(null),imgBG.getHeight(nul l));
setPreferredSize(objDimension);
setMinimumSize(objDimension);
setMaximumSize(objDimension);
setSize(objDimension);
setLayout(null);
setVisible(true);
//Starting variables
nCurrentPlayer = 1;
nMoves = 0;
bThrowedDice = false;
bUseSkill = true;
//bUseSkill:
//true = Skill can be used this turn
//false = Skill already been used this turn
//JButtons define
imgButtonRed = new ImageIcon("images/RedButton.png");
btnSkill1 = new JButton(imgButtonRed);
btnSkill2 = new JButton(imgButtonRed);
btnSkill3 = new JButton(imgButtonRed);
btnSkill4 = new JButton(imgButtonRed);
btnSkill1.setBounds(650, 400, btnSkill1.getHeight(), btnSkill1.getWidth());
btnSkill2.setBounds(650, 430, btnSkill2.getHeight(), btnSkill2.getWidth());
btnSkill3.setBounds(650, 460, btnSkill3.getHeight(), btnSkill3.getWidth());
btnSkill4.setBounds(650, 490, btnSkill4.getHeight(), btnSkill4.getWidth());
imgButtonGreen = new ImageIcon("images/GreenButton.png");
imgDice1 = (new ImageIcon("images/Die1.png").getImage());
imgDice2 = (new ImageIcon("images/Die2.png").getImage());
imgButtonRollDice = new ImageIcon("images/RollDice.png");
btnRollDice = new JButton("SAMPLE");
btnRollDice.setBounds(380, 515, btnRollDice.getHeight(), btnRollDice.getWidth());
panelMain.add(btnSkill1);
panelMain.add(btnSkill2);
panelMain.add(btnSkill3);
panelMain.add(btnSkill4);
panelMain.add(btnRollDice);
//Creates Character objects depending how many players
for (int i = 0; i < nNumberOfPlayers; i++)
objCharacter[i] = new Character((i+1));
}
public void run()
{
if (bThrowedDice == true)
{
repaint();
}
if (bUseSkill = true)
{
}
}
public void paint(Graphics g)
{
g.drawImage(imgBG, 0, 0, null);
//g.drawImage(imgDice1, 230, 515, null);
//g.drawImage(imgDice2, 290, 515, null);
//Roll the dice
if (bThrowedDice == true)
{
g.drawImage(imgDice1, 50, 450, null);
g.drawImage(imgDice2, 85, 450, null);
nMoves = 0;
bThrowedDice = false;
}
}
}
I think it has something to do with the paint function? or not? However, I am sure that the locations of the image is correct because I tried using g.DrawImage inside method paint to show that the pictures can be loaded. Are there other ways to show the buttons? I tried setVisible(true) and setEnabled(true) but then no luck. Any help, suggestion, ideas, or any thing else is welcomed thanksLast edited by boisk; 03-14-2009 at 09:24 AM. Reason: Removed some methods that are not needed
- 03-14-2009, 11:06 AM #2
As Your code is quite large,i cant understand what you want.
i think if you remove the setbounds() from your code,it may work.Mak
(Living @ Virtual World)
-
I see several issues here:
1) Don't draw directly onto a JFrame (override its paint method). Instead draw on a JPanel that either is your contentPane or is added to your contentPane BorderLayout.CENTER.
2) When you draw on a JPanel, you will need to override paintComponent, not paint.
3) Whenever overriding paint or paintComponent, usually the first line of the override will contain the statement super.paint(g) or super.paintComponent(g) respectively. This will tell graphics to draw all the background stuff that needs to be drawn.
4) Call pack and setVisible(true) after you've added all of your widgets to your JPanels and JFrame. Otherwise if this isn't done, graphics will be drawing your application but not the stuff added after these methods have been called.
5) Avoid using null layout. Your GUI life will be made much easier if you study and learn to use the layout managers available for your use.
6) In particular, don't set the layout of the JFrame or its contentPane to null. That's asking for trouble!Last edited by Fubarable; 03-14-2009 at 02:26 PM.
- 03-14-2009, 04:58 PM #4
Member
- Join Date
- Mar 2009
- Posts
- 8
- Rep Power
- 0
makpandian,
I tried, but unforunately it did not work.
Fubarable,
Thanks for trying to realize my code! Anyway,
I tried what you said by instead of drawing inside a JFrame I placed a JPanel instead. And I also used paintComponent instead of paint. regarding the contentpane() im not sure if I did it correctly, it is in bold letters.
I have a question about calling super.paintComponent(g). Do i need to use that or can i just use repaint();?
And regarding about calling pack() and setVisible(true) at the end, my buttons still does not show up. Am i still doing something wrong? Do I need to update also my buttons to show up everytime?
And for the layout, yes i will try, but for now i think i will stay with null first until I get this right
just some xtra info: Im using NetBeans if there is some connection with this problem. The BG Shows Up, the only problems are on the buttons showing up with their image.
btw here is the updated code, and really Thanks for your time checking and reading my code!
public class GameBoard extends JPanel implements Runnable
{
private Image imgBG, imgPlayers, imgDice1, imgDice2;
private ImageIcon imgButtonGreen, imgButtonRed, imgButtonRollDice;
private Image img0, img1, img2, img3, img4, img5, img6, img7, img8, img9;
private JButton btnSkill1, btnSkill2, btnSkill3, btnSkill4, btnRollDice;
private Random objRandom = new Random();
private boolean bThrowedDice, bUseSkill;
private int nCurrentPlayer, nMoves, nTemp;
private JPanel panelMain = new JPanel();
private Character[] objCharacter = new Character[3];
public GameBoard(int nNumberOfPlayers)
{
JFrame frameMain = new JFrame("Snakes and Ladders: Casino");
JPanel panelMain = (JPanel)frameMain.getContentPane();
//JPanel attributes
panelMain.setPreferredSize(new Dimension(800,600));
panelMain.setLayout(null);
panelMain.add(this);
//JFrame attributes
frameMain.setBounds(120,80,0,0);
frameMain.setResizable(false);
frameMain.setDefaultCloseOperation(JFrame.EXIT_ON_ CLOSE);
//Sets GameBoard Image
imgBG = (new ImageIcon("images/GameBoard.png").getImage());
Dimension objDimension = new Dimension(imgBG.getWidth(null),imgBG.getHeight(nul l));
setPreferredSize(objDimension);
setMinimumSize(objDimension);
setMaximumSize(objDimension);
setSize(objDimension);
setLayout(null);
//Starting variables
nCurrentPlayer = 1;
nMoves = 0;
bThrowedDice = false;
bUseSkill = true;
//bUseSkill:
//true = Skill can be used this turn
//false = Skill already been used this turn
//JButtons define
imgButtonRed = new ImageIcon("images/RedButton.png");
btnSkill1 = new JButton(imgButtonRed);
btnSkill2 = new JButton(imgButtonRed);
btnSkill3 = new JButton(imgButtonRed);
btnSkill4 = new JButton(imgButtonRed);
btnSkill1.setBounds(650, 400, btnSkill1.getHeight(), btnSkill1.getWidth());
btnSkill2.setBounds(650, 430, btnSkill2.getHeight(), btnSkill2.getWidth());
btnSkill3.setBounds(650, 460, btnSkill3.getHeight(), btnSkill3.getWidth());
btnSkill4.setBounds(650, 490, btnSkill4.getHeight(), btnSkill4.getWidth());
imgButtonGreen = new ImageIcon("images/GreenButton.png");
imgDice1 = (new ImageIcon("images/Die1.png").getImage());
imgDice2 = (new ImageIcon("images/Die2.png").getImage());
//imgButtonRollDice = new ImageIcon("images/RollDice.png");
imgButtonRollDice = new ImageIcon("images/RollDice.png");
btnRollDice = new JButton("SAMPLE");
btnRollDice.setBounds(380, 515, btnRollDice.getHeight(), btnRollDice.getWidth());
add(btnSkill1);
add(btnSkill2);
add(btnSkill3);
add(btnSkill4);
add(btnRollDice);
//Creates Character objects depending how many players
for (int i = 0; i < nNumberOfPlayers; i++)
objCharacter[i] = new Character((i+1));
frameMain.pack();
frameMain.setVisible(true);
}
public void run()
{
if (bThrowedDice == true)
{
UpdateVar();
Rules();
repaint();
}
if (bUseSkill = true)
{
}
}
public void paintComponent(Graphics g)
{
g.drawImage(imgBG, 0, 0, null);
//g.drawImage(imgDice1, 230, 515, null);
//g.drawImage(imgDice2, 290, 515, null);
//Roll the dice
if (bThrowedDice == true)
{
g.drawImage(imgDice1, 50, 450, null);
g.drawImage(imgDice2, 85, 450, null);
nMoves = 0;
bThrowedDice = false;
}
}
}Last edited by boisk; 03-14-2009 at 05:01 PM.
-
1) Let's simplify things as much as possible. I recommend that you get rid of all your program logic and just post compilable code that does nothing but lay out components in your application.
2) when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?
To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.
Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:
Java Code:[code] // your code goes here // notice how the top and bottom tags are different [/code]
-
As an example of the power of layouts, say you wanted to create a GUI that had 4 buttons in the lower right corner, like so:
Now what if later you wanted to change the number of buttons on the right to seven? Since the above code uses layouts and doesn't use absolute positioning, all I have to do is change one constant from 4 to 7. in other words, this:Java Code:import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class FooBoard extends JPanel { private static final Dimension MAIN_SIZE = new Dimension(750, 600); private static final int FOO_MAX = 4; private JButton[] fooButtons = new JButton[FOO_MAX]; private JPanel board = new JPanel(); public FooBoard() { JPanel fooBtnPanel = new JPanel(new GridLayout(0, 1, 10, 10)); FooButtonListener fooListener = new FooButtonListener(); for (int i = 0; i < fooButtons.length; i++) { JButton fooBtn = new JButton("Foo Button " + String.valueOf(i + 1)); fooBtn.addActionListener(fooListener); fooBtnPanel.add(fooBtn); } JPanel eastPanel = new JPanel(new BorderLayout()); eastPanel.add(fooBtnPanel, BorderLayout.SOUTH); JButton otherButton = new JButton("Other Button"); otherButton.addActionListener(new OtherButtonActionListener()); JPanel southPanel = new JPanel(); southPanel.add(otherButton); board.setBorder(BorderFactory.createLineBorder(Color.black)); setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12)); setPreferredSize(MAIN_SIZE); setLayout(new BorderLayout(20, 20)); add(eastPanel, BorderLayout.EAST); add(southPanel, BorderLayout.SOUTH); add(board, BorderLayout.CENTER); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); // do painting here } private class FooButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println(e.getActionCommand() + " pressed"); // TODO get rid of // TODO finish } } private class OtherButtonActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println("Other Button pressed"); // TODO get rid of // TODO finish } } private static void createAndShowUI() { JFrame frame = new JFrame("FooBoard"); frame.getContentPane().add(new FooBoard()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } }
becomes this:Java Code:private static final int FOO_MAX = 4;
and the layout managers will do the dirty work for me.Java Code:private static final int FOO_MAX = 7;
- 03-15-2009, 01:53 AM #7
Member
- Join Date
- Mar 2009
- Posts
- 8
- Rep Power
- 0
Sorry for the long code, but I made this one shorter thanks to Fubarable
Fubarable,
I tried your example, but still no luck. I don't get why I can't my JButtons. Is it because of the image that I am placing on my Jbuttons? Because I am using ImageIcon instead of Image, while I am using Image for my BG. And from your example, what if the void static main is on another class, will implements Runnable be placed on your FooBoard? When I try to put the things to add on my public void run(), they dont run at all. Any Ideas? Thanks. And the shortened code is posted below
Java Code:import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class GameBoard extends JPanel implements Runnable { private Image imgBG; private ImageIcon imgButtonRollDice; private JButton btnRollDice; private JFrame frameMain = new JFrame("Sample Board Game"); public GameBoard(int nNumberOfPlayers) { //JFrame attributes frameMain.setBounds(120,80,0,0); frameMain.setResizable(false); frameMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Sets GameBoard Image imgBG = (new ImageIcon("images/GameBoard.png").getImage()); Dimension objDimension = new Dimension(imgBG.getWidth(null),imgBG.getHeight(null)); setPreferredSize(objDimension); setMinimumSize(objDimension); setMaximumSize(objDimension); setSize(objDimension); setLayout(null); } @Override public void run() { frameMain.getContentPane().add(this); //JButtons define imgButtonRollDice = new ImageIcon("images/RollDice.png"); btnRollDice = new JButton(imgButtonRollDice); btnRollDice.setBounds(380, 515, btnRollDice.getHeight(), btnRollDice.getWidth()); add(btnRollDice); frameMain.pack(); frameMain.setVisible(true); } public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(imgBG, 0, 0, null); } }Java Code:public class Driver { public static void main(String[] args) { GameBoard objGameBoard = new GameBoard(3); } }Last edited by boisk; 03-15-2009 at 01:55 AM. Reason: added Driver class which has main
-
I don't like your trying to set height with btnRollDice.getHeight(), especially before rendering the button. For one width comes before height here. For another, if you have to do it this way, you're far better off using btnRollDice.getPreferredSize().height rather than getHeight()
what if you change your code like so:
Java Code:btnRollDice = new JButton(imgButtonRollDice); System.out.println("btnRollDice.getHeight(): " + btnRollDice.getHeight()); System.out.println("btnRollDice.getPreferredSize().height: " + btnRollDice.getPreferredSize().height); Dimension d = btnRollDice.getPreferredSize(); btnRollDice.setBounds(380, 515, d.width, d.height);Last edited by Fubarable; 03-15-2009 at 02:33 AM.
- 03-15-2009, 05:29 AM #9
Member
- Join Date
- Mar 2009
- Posts
- 8
- Rep Power
- 0
Fubarable,
THANKS SO MUCH! That was my problem I just have to fix the Dimension of the button! At last I can further fix my program thanks a lot!!
-
cool, glad it helped. Now be a good boy and read up on layout managers!
- 03-15-2009, 07:02 AM #11
Member
- Join Date
- Mar 2009
- Posts
- 8
- Rep Power
- 0
Yeah I will try learning it after printing out these buttons, but I have another problem. public void run() doesn't run at all. I need to put all those add buttons and calling the images inside the constructor. I can not make it run inside the run() method any ideas? Its as if the run() method is not being called.
Java Code:public class GameBoard extends JPanel implements Runnable { private Image imgBG, imgPlayers, imgDice1, imgDice2; private ImageIcon imgButtonRollDice; private JButton btnRollDice; private JFrame frameMain = new JFrame("Sample Board Game"); private Dimension objDimension = new Dimension(); public GameBoard(int nNumberOfPlayers) { //JFrame attributes frameMain.setBounds(120,80,0,0); frameMain.setResizable(false); frameMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Sets GameBoard Image imgBG = (new ImageIcon("images/GameBoard.png").getImage()); Dimension objDimension = new Dimension(imgBG.getWidth(null),imgBG.getHeight(null)); setPreferredSize(objDimension); setMinimumSize(objDimension); setMaximumSize(objDimension); setSize(objDimension); setLayout(null); frameMain.getContentPane().add(this); frameMain.setVisible(true); } public void run() { UpdateUI(); } public void UpdateUI() { imgButtonRollDice = new ImageIcon("images/RollDice.png"); btnRollDice = new JButton(imgButtonRollDice); objDimension = btnRollDice.getPreferredSize(); btnRollDice.setBounds(375, 515, objDimension.width, objDimension.height); add(btnRollDice); frameMain.pack(); } }Java Code:public class Driver { public static void main(String[] args) { GameBoard objGameBoard = new GameBoard(3); } }Last edited by boisk; 03-15-2009 at 07:15 AM. Reason: added codes
- 03-15-2009, 07:19 AM #12
Member
- Join Date
- Mar 2009
- Posts
- 8
- Rep Power
- 0
I changed the codes and I am not sure if this is ok, but it works
then I called run(); in the constructor. Is it ok to do this?Java Code:public void run() { UpdateUI(); run(); }
- 03-15-2009, 08:15 AM #13
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
A method named "run" is not magic. Yes, you can call it in your constructor.
However, I'm assuming that since it *looks* like you are trying to start a new thread, that that is what your intent is. Well, is it?
- 03-15-2009, 09:00 AM #14
Member
- Join Date
- Mar 2009
- Posts
- 8
- Rep Power
- 0
Yes Im trying to start a new thread, but I have no idea how. I thought by overwriting run, it will run by itself like paint. so how can I call it in a formal way?
-
- 03-15-2009, 02:27 PM #16
Member
- Join Date
- Mar 2009
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
[SOLVED] Little help on adding a jfreechart graph to Jpanel
By Manfizy in forum New To JavaReplies: 5Last Post: 02-25-2009, 08:01 AM -
JButton Problem
By wassim in forum AWT / SwingReplies: 6Last Post: 02-18-2009, 10:29 PM -
adding a jpanel in the middle of the script
By 2o2 in forum AWT / SwingReplies: 11Last Post: 10-12-2008, 05:50 PM -
Problem with JPanel
By ibanez270dx in forum New To JavaReplies: 2Last Post: 11-09-2007, 05:04 PM -
Problem with JButton
By Marcus in forum AWT / SwingReplies: 1Last Post: 07-05-2007, 05:56 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks