Results 1 to 8 of 8
- 04-02-2011, 02:03 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 13
- Rep Power
- 0
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
Here titleScreen is a boolean that is true when the buttons should be shown.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); }
**
Now here is paintComponent and actionPerformed.
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.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(); }
Thanks in advance!
- 04-02-2011, 02:28 AM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,606
- Rep Power
- 5
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
- 04-02-2011, 02:31 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 13
- Rep Power
- 0
So then I should do this?
MyPanel is a class, not a JPanel. Here's the code snippet for it.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); }
Java Code:class MyPanel extends JPanel implements ActionListener{Last edited by corrax; 04-02-2011 at 02:34 AM.
- 04-02-2011, 02:33 AM #4
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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.
- 04-02-2011, 02:50 AM #5
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,606
- Rep Power
- 5
Did you try it? What happenedSo then I should do this?
And posting an SSCCE would have immediately resolved this...and I wholeheartedly second all of camickr's advice.MyPanel is a class, not a JPanel. Here's the code snippet for it.
- 04-02-2011, 03:01 AM #6
Member
- Join Date
- Apr 2011
- Posts
- 13
- Rep Power
- 0
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.
- 04-02-2011, 03:18 AM #7
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Did you read my comment?
Well, your class does extend JPanel so you don't need a Container or a JPanel. All you do is:If your class already extends JPanel then I don't even know why you are creating a separate Container.
Java Code:setLayout( null ); ... add( ins ); ... add( newGame );
- 04-02-2011, 03:27 AM #8
Member
- Join Date
- Apr 2011
- Posts
- 13
- Rep Power
- 0
Similar Threads
-
jbutton
By Patea2000 in forum NetBeansReplies: 1Last Post: 03-16-2011, 08:56 AM -
JButton help
By ThrashingBoy in forum New To JavaReplies: 8Last Post: 03-01-2011, 01:48 AM -
[SWING]Adding JButton(class1) to JPanel(class2)
By equal in forum New To JavaReplies: 8Last Post: 02-20-2011, 01:09 AM -
Java Swing Gui Slow init on Solaris user
By markonthego in forum AWT / SwingReplies: 0Last Post: 09-13-2010, 09:55 PM -
Help with JButton
By geoffreybarwise in forum New To JavaReplies: 4Last Post: 05-21-2008, 10:48 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks