Results 1 to 8 of 8
  1. #1
    corrax is offline Member
    Join Date
    Apr 2011
    Posts
    13
    Rep Power
    0

    Default New AWT/SWING user, help in JButton

    Hi.
    I am working on a game project for school, and I am new at swing/awt.
    In my program, my code should print out 2 JButtons. But no Buttons show up. Here is class MyPanel which contains the methods paintComponent, and setting up both buttons as well as ActionListener.
    ***
    First, here is the constructor for setting up the JButtons

    Java Code:
    public MyPanel(){
    			cont = new Container();
    			cont.setLayout(null);
    			
    			ins = new JButton("Instructions");
    			ins.setSize(130,110);
    			ins.setLocation(240,300);
    			cont.add(ins);
    			ins.addActionListener(this);
    			
    			newGame = new JButton("New Game");
    			newGame.setSize(130,110);
    			newGame.setLocation(360,500);
    			cont.add(newGame);
    			newGame.addActionListener(this);
    			if(titleScreen)
    				cont.setVisible(true);
    			else if(!titleScreen)
    				cont.setVisible(false);
    		}
    Here titleScreen is a boolean that is true when the buttons should be shown.

    **
    Now here is paintComponent and actionPerformed.

    Java Code:
     public void paintComponent(Graphics g){
    			super.paintComponent(g);
    			if(copyright)
    				g.drawImage(title,50,30,this);
    			else if(titleScreen){
    				g.drawImage(javam,70,25,this);
    				g.drawImage(catc,90,140,this);
    			}
    			else if(insScreen)
    			{
    				g.setColor(Color.green);
    				g.fillRect(0,0,600,600);
    				g.drawImage(oak,25,100,this);
    			}
    			else if(gameStart)
    			{
    				g.setColor(Color.blue);
    				g.fillRect(0,0,600,600);
    			}
    			canvas.repaint();
    		}
    		public void actionPerformed(ActionEvent e)
    		{
    			String command = e.getActionCommand();
    			if(titleScreen && command.equals("Instructions"))
    			{
    				insScreen = true;
    				titleScreen = false;
    			}
    			else if(titleScreen && command.equals("New Game"))
    			{
    				gameStart = true;
    				titleScreen = false;
    			}
    			canvas.repaint();
    		}
    Here gameStart is accessed when the newGame button is clicked(if it shows up), and insScreen is ins button clicked. titleScreen is the screen with both buttons. And boolean copyright(from paintComponent) is the copyright screen which I added for fun.

    Thanks in advance!

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    There is a JPanel in which the buttons are created (top snippet of code), and the buttons are added to a Container, but based upon the code posted the container is never added to the actual JPanel

  3. #3
    corrax is offline Member
    Join Date
    Apr 2011
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by doWhile View Post
    There is a JPanel in which the buttons are created (top snippet of code), and the buttons are added to a Container, but based upon the code posted the container is never added to the actual JPanel
    So then I should do this?


    Java Code:
    public MyPanel(){
                           [B] JPanel panel = new JPanel();[/B]
    			cont = new Container();
    			cont.setLayout(null);
                           [B] panel.add(cont);[/B]
    			
    			ins = new JButton("Instructions");
    			ins.setSize(130,110);
    			ins.setLocation(240,300);
    			cont.add(ins);
    			ins.addActionListener(this);
    			
    			newGame = new JButton("New Game");
    			newGame.setSize(130,110);
    			newGame.setLocation(360,500);
    			cont.add(newGame);
    			newGame.addActionListener(this);
    			if(titleScreen)
    				cont.setVisible(true);
    			else if(!titleScreen)
    				cont.setVisible(false);
    		}
    MyPanel is a class, not a JPanel. Here's the code snippet for it.

    Java Code:
    class MyPanel extends JPanel implements ActionListener{
    Last edited by corrax; 04-02-2011 at 02:34 AM.

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default

    Don't use a "null layout". Learn how to create a GUI properly and use layout managers. Generally in Swing we add components to a JPanel, not a Container. If you class already extends JPanel then I don't even know why you are creating a separate Container.

    Post your Short, Self Contained, Correct Example that demonstrates the problem if you need more help.

  5. #5
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    So then I should do this?
    Did you try it? What happened

    MyPanel is a class, not a JPanel. Here's the code snippet for it.
    And posting an SSCCE would have immediately resolved this...and I wholeheartedly second all of camickr's advice.

  6. #6
    corrax is offline Member
    Join Date
    Apr 2011
    Posts
    13
    Rep Power
    0

    Default

    Well that was basically all of my code (I haven't gone far). But here's the total code or SSCCE. Also, the JPanel thing did not work. I'm researching layouts to see how it would work if I added a gridLayout in that area. Because I know how to make a normal buttonPanel with a borderLayout, but I wanted to make the buttons at different x and y coordinates, not in a flowLayout. You have already seen the main entirety of the code below.

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.util.Timer;
    import java.util.TimerTask;
    
    public class Javamon extends JApplet {
    	private MyPanel canvas;
    	private Image title,javam, catc,oak;
    	private boolean copyright = true,titleScreen = false, gameStart = false, insScreen = false;
    	private Timer timer;
    	private JButton ins, newGame;
    	private Container cont;
    	public void init() {
    		timer = new Timer("Printer");
    		MyTask t = new MyTask();
    		timer.schedule(t,0,	1000);
    		canvas = new MyPanel();
    		setContentPane(canvas);
    		canvas.setBackground(Color.black);
    		title = getImage(getCodeBase(),"Title.png");
    		catc = getImage(getCodeBase(),"Catch.png");
    		javam = getImage(getCodeBase(),"Javamon.png");
    		oak = getImage(getCodeBase(),"oak.png");
    	}
    	class MyTask extends TimerTask{
    		int times = 0;
    		public void run() {
    			times++;
    			if(times > 7){
    				titleScreen = true;
    				copyright = false;
    			}
    		}
    	}
    	class MyPanel extends JPanel implements ActionListener{
    	
    		public MyPanel(){
    			JPanel panel = new JPanel();
    			cont = new Container();
    			cont.setLayout(null);
    			panel.add(cont);
    			
    			ins = new JButton("Instructions");
    			ins.setSize(130,110);
    			ins.setLocation(240,300);
    			cont.add(ins);
    			ins.addActionListener(this);
    			
    			newGame = new JButton("New Game");
    			newGame.setSize(130,110);
    			newGame.setLocation(360,500);
    			cont.add(newGame);
    			newGame.addActionListener(this);
    			if(titleScreen)
    			       cont.setVisible(true);
    			else if(!titleScreen)
    				cont.setVisible(false);
    		}
    		public void paintComponent(Graphics g){
    			super.paintComponent(g);
    			if(copyright)
    				g.drawImage(title,50,30,this);
    			else if(titleScreen){
    				g.drawImage(javam,70,25,this);
    				g.drawImage(catc,90,140,this);
    			}
    			else if(insScreen)
    			{
    				g.setColor(Color.green);
    				g.fillRect(0,0,600,600);
    				g.drawImage(oak,25,100,this);
    			}
    			else if(gameStart)
    			{
    				g.setColor(Color.blue);
    				g.fillRect(0,0,600,600);
    			}
    			canvas.repaint();
    		}
    		public void actionPerformed(ActionEvent e)
    		{
    			String command = e.getActionCommand();
    			if(titleScreen && command.equals("Instructions"))
    			{
    				insScreen = true;
    				titleScreen = false;
    			}
    			else if(titleScreen && command.equals("New Game"))
    			{
    				gameStart = true;
    				titleScreen = false;
    			}
    			canvas.repaint();
    		}
    	}
    }
    Last edited by corrax; 04-02-2011 at 03:04 AM.

  7. #7
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default

    Did you read my comment?

    If your class already extends JPanel then I don't even know why you are creating a separate Container.
    Well, your class does extend JPanel so you don't need a Container or a JPanel. All you do is:

    Java Code:
    setLayout( null );
    ...
    add( ins );
    ...
    add( newGame );

  8. #8
    corrax is offline Member
    Join Date
    Apr 2011
    Posts
    13
    Rep Power
    0

    Default

    Thank you! It worked. Only thing is, it still prints out the buttons at all times because I had to delete the setVisible. But I can figure that out myself. Thanks to both of you again!

Similar Threads

  1. jbutton
    By Patea2000 in forum NetBeans
    Replies: 1
    Last Post: 03-16-2011, 08:56 AM
  2. JButton help
    By ThrashingBoy in forum New To Java
    Replies: 8
    Last Post: 03-01-2011, 01:48 AM
  3. [SWING]Adding JButton(class1) to JPanel(class2)
    By equal in forum New To Java
    Replies: 8
    Last Post: 02-20-2011, 01:09 AM
  4. Java Swing Gui Slow init on Solaris user
    By markonthego in forum AWT / Swing
    Replies: 0
    Last Post: 09-13-2010, 09:55 PM
  5. Help with JButton
    By geoffreybarwise in forum New To Java
    Replies: 4
    Last Post: 05-21-2008, 10:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •