Results 1 to 13 of 13
Thread: whats wrong with my GUI?
- 05-30-2012, 07:00 PM #1
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
whats wrong with my GUI?
I must of misunderstood something this wont compile... I just want to make 9 buttons using a grid layout
I get this compilation error on "addActionListener"Java Code:package naughtsAndCrosses; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.Container; import javax.swing.ButtonGroup; import javax.swing.JFrame; import javax.swing.JButton; public class GUI extends JFrame { private JButton[] buttons; private ButtonGroup buttonGroup; private static final String[] buttonNames = { "1", "2", "3", "4", "5", "6", "7", "8", "9" }; private GridLayout gridLayout; private Container container; public GUI() { super("Naughts and Crosses"); gridLayout = new GridLayout( 3, 3, 10, 10 ); container = getContentPane(); setLayout( gridLayout ); buttons = new JButton[ buttonNames.length ]; buttonGroup = new ButtonGroup(); for ( int i = 0 ; i < buttonNames.length ; i++ ) { buttons[i] = new JButton ( buttonNames[i] ); buttons[i].addActionListener ( new ActionListener() { public void actionPerformed(ActionEvent event) { System.out.println("you presses a button well done"); } } ); add(buttons[i]); buttonGroup.add(buttons[i]); } } }
The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (new ActionListener(){})
and this runtime error
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (new ActionListener(){})
ActionListener cannot be resolved to a type
I dont know what I'm doing wrong here, I'm trying to create an annon inner class that is a action listener and attach it to the JButtons...
also a related question, how do I seperate my code from the GUI? I dont want to take out segments of code and put them into actionlisteners then all my classes will be destroyed.Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.
-
Re: whats wrong with my GUI?
You haven't imported the ActionListener class yet, it appears.
- 05-30-2012, 08:01 PM #3
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
Re: whats wrong with my GUI?
are you serious? this is ridiculous... I keep forgetting when something goes wrong maybe I havent done anything weird but just overlooked something trivial, in this case importing the damn thing.
Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.
- 05-30-2012, 08:31 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Re: whats wrong with my GUI?
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-30-2012, 09:58 PM #5
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
Re: whats wrong with my GUI?
The most embarassing part is I'm using eclipse...
I cant get my GUI the way I want it here is what I have so far
ImageShack® - Online Photo and Video Hosting
Now, my design idea was simple. I couldnt get flow layout to put the grid above the text feild, all the buttons would rearranged and stuff.
so I thought ok, lets reduce the grid to a single componant of its own panel class (so it can have its own grid layout for that panel only) and then in the Jframe class I can say put the panel north and the textfield south!
unexplained behaviour. depending on some size arguments the buttons replicate and theres loads of duplicates but thats ok because I made it not resizable.
now, how do I make the buttons bigger? I tried resizing the button panel but that messed it up (text feild looped round the window somehow)
Java Code:package naughtsAndCrosses; import java.awt.BorderLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.Font; import java.awt.FlowLayout; import java.awt.Container; import javax.swing.JFrame; import javax.swing.JTextField; public class GUI extends JFrame { private ButtonPanel myPanel; private Container container; public GUI() { super("Naughts and Crosses"); setLayout(new FlowLayout()); myPanel = new ButtonPanel(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(500, 500); this.setVisible(true); this.setResizable(false); add(myPanel, BorderLayout.NORTH); JTextField textField = new JTextField("Welcome player!", 100); textField.setFont( new Font( "Serif", Font.PLAIN, 14 )); add( textField, BorderLayout.SOUTH ); } }also I mentioned this before but I cant get eclipse or java to recognise pictures, even when they are in the same file as the java files and are of ping formatJava Code:package naughtsAndCrosses; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.ButtonGroup; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JPanel; public class ButtonPanel extends JPanel { private JButton[] buttons; private ButtonGroup buttonGroup; private static final String[] buttonNames = { "1", "2", "3", "4", "5", "6", "7", "8", "9" }; private GridLayout gridLayout; public void paintComponent(Graphics g) { super.paintComponent(g); gridLayout = new GridLayout( 3, 3, 10, 10 ); setLayout( gridLayout ); //Icon x = new ImageIcon( getClass().getResource("x.png")); //Icon o = new ImageIcon( getClass().getResource("o.png")); //JButton imageButton = new JButton("fancy", x); //add(imageButton); IMAGES ARE NEVER FOUND buttons = new JButton[ buttonNames.length ]; buttonGroup = new ButtonGroup(); for ( int i = 0 ; i < buttonNames.length ; i++ ) { buttons[i] = new JButton ( buttonNames[i] ); buttons[i].addActionListener ( new ActionListener() { public void actionPerformed(ActionEvent event) { System.out.println("you presses a button well done"); } } ); add(buttons[i]); buttonGroup.add(buttons[i]); } } }Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.
-
Re: whats wrong with my GUI?
Perhaps you want to post a picture (not a link) to just how you want your GUI to look.
I'm not understanding this.unexplained behaviour. depending on some size arguments the buttons replicate and theres loads of duplicates but thats ok because I made it not resizable.
I've often increased the button's font when doing this. Another way is to give the button an ImageIcon that is the size you want it. You could use a blank ImageIcon for this. Or you could do the same with a JLabel.now, how do I make the buttons bigger? I tried resizing the button panel but that messed it up (text feild looped round the window somehow)
Your code snippets:
I usually try to avoid using FlowLayout, especially for the Frame itself as it's very limited in what it can do.Java Code:public GUI() { super("Naughts and Crosses"); setLayout(new FlowLayout());
Don't call setSize(...). If you have to do something like that, you're better off calling setPreferredSize(...), and not forgetting to call pack(). Even preferredSize should be avoided unless absolutely necessary. Better to let the components set their own size. Also, don't set the window visible until you've added all its components, else they may not render or render correctly.Java Code:myPanel = new ButtonPanel(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(500, 500); this.setVisible(true); this.setResizable(false); add(myPanel, BorderLayout.NORTH); JTextField textField = new JTextField("Welcome player!", 100); textField.setFont( new Font( "Serif", Font.PLAIN, 14 )); add( textField, BorderLayout.SOUTH ); } }
OK, I now think I know why you're seeing some crazy stuff. The paint/paintComponent methods are for painting *only* and not for program logic and that includes adding or removing components. This is important, so let me repeat this: You never want to have program logic inside of paint or paintComponent methods. You need this method to be blindingly fast, and so it must have only essential code, and you do not have full control over when or even if it gets called. Please read the Swing tutorials as they will show you how to add components to a GUI correctly.Java Code:public class ButtonPanel extends JPanel { private JButton[] buttons; private ButtonGroup buttonGroup; private static final String[] buttonNames = { "1", "2", "3", "4", "5", "6", "7", "8", "9" }; private GridLayout gridLayout; public void paintComponent(Graphics g) { super.paintComponent(g); gridLayout = new GridLayout( 3, 3, 10, 10 ); setLayout( gridLayout ); //Icon x = new ImageIcon( getClass().getResource("x.png")); //Icon o = new ImageIcon( getClass().getResource("o.png")); //JButton imageButton = new JButton("fancy", x); //add(imageButton); IMAGES ARE NEVER FOUND buttons = new JButton[ buttonNames.length ]; buttonGroup = new ButtonGroup(); for ( int i = 0 ; i < buttonNames.length ; i++ ) { buttons[i] = new JButton ( buttonNames[i] ); buttons[i].addActionListener ( new ActionListener() { public void actionPerformed(ActionEvent event) { System.out.println("you presses a button well done"); } } ); add(buttons[i]); buttonGroup.add(buttons[i]); } } }
Are you dragging the images into Eclipse? You should be since this will place them in the project relative to the class files. What I've done is create an images package and drag my images there. Then I usually can get them via my class resourses.also I mentioned this before but I cant get eclipse or java to recognise pictures, even when they are in the same file as the java files and are of ping format
- 05-31-2012, 02:29 AM #7
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
Re: whats wrong with my GUI?
jackpot! fubarable response
guess I'm off to the gui tutorials... I hate gui's I really do. but it must be done.
do you know of an easy way to resize an image by pixel dimentions without locking aspect ratio?
...If I'm using images to size the buttons they should be of the same dimentions... strangely I cant find a tool for such an easy task, can you suggest anything?Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.
- 05-31-2012, 03:27 AM #8
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
Re: whats wrong with my GUI?
Is there anyway I can make the image completely cover the button? (essentially its a clickable image?
Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.
- 05-31-2012, 03:46 AM #9
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
Re: whats wrong with my GUI?
also what is the best gui builder plug in for eclipse?
Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.
- 05-31-2012, 04:12 AM #10
Re: whats wrong with my GUI?
Why do they call it rush hour when nothing moves? - Robin Williams
- 05-31-2012, 07:49 AM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Re: whats wrong with my GUI?
There's no excuse for the incorrect imports then; here's a tip: always press Ctrl-Shift-O Ctrl-Shift-S after you're done typing; the first keys organize your imports while the second key combination saves and compiles everything that needs saving and (re)compilation. No need to worry because it works amazingly fast.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-31-2012, 06:09 PM #12
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
Re: whats wrong with my GUI?
ok thanks... does ctrl-shift-s combine ctrl-s and ctrl-f11? because thats what I do
Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.
- 05-31-2012, 08:20 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
I cant do this.Whats wrong?
By gaston7eze in forum Java 2DReplies: 5Last Post: 05-30-2012, 12:34 AM -
Whats wrong with this?
By Alkor in forum AWT / SwingReplies: 7Last Post: 04-03-2012, 05:49 AM -
Whats wrong with the program?
By fishy8158 in forum New To JavaReplies: 2Last Post: 11-13-2011, 05:30 AM -
whats wrong
By atenv in forum New To JavaReplies: 6Last Post: 06-15-2010, 01:55 PM -
whats is wrong with this app??
By mrajan in forum New To JavaReplies: 4Last Post: 06-09-2010, 10:56 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks