Results 1 to 13 of 13
  1. #1
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default whats wrong with my GUI?

    I must of misunderstood something this wont compile... I just want to make 9 buttons using a grid layout

    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]);
    		}
    	}
    }
    I get this compilation error on "addActionListener"

    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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default 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.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: whats wrong with my GUI?

    Quote Originally Posted by EscSequenceAlpha View Post
    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.
    When you feel confident enough with the Java JDK, consider using an IDE instead; it adds those imports for you; e.g. Eclipse can add those imports for you with a couple of a mouse clicks.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default 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 );
    	}
    }
    Java 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]);
    		}
    	}
    }
    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
    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.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: whats wrong with my GUI?

    Quote Originally Posted by EscSequenceAlpha View Post
    I cant get my GUI the way I want it here is what I have so far

    ImageShack® - Online Photo and Video Hosting
    Perhaps you want to post a picture (not a link) to just how you want your GUI to look.

    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'm not understanding this.

    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)
    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.


    Your code snippets:
    Java Code:
    	public GUI()
    	{
    		super("Naughts and Crosses");
    		setLayout(new FlowLayout());
    I usually try to avoid using FlowLayout, especially for the Frame itself as it's very limited in what it can do.

    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 );
    	}
    }
    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:
    
    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]);
    		}
    	}
    }
    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.

    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
    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.

  7. #7
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default 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.

  8. #8
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default 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.

  9. #9
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default 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.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: whats wrong with my GUI?

    Quote Originally Posted by EscSequenceAlpha View Post
    Is there anyway I can make the image completely cover the button? (essentially its a clickable image?
    setXxxPainted(false)

    Go through the API for methods inherited from AbstractButton and you'll find what those Xxx should be. There's another method you'll need too, also one that takes a boolean parameter.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: whats wrong with my GUI?

    Quote Originally Posted by EscSequenceAlpha View Post
    The most embarassing part is I'm using eclipse...
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default 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.

  13. #13
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: whats wrong with my GUI?

    Quote Originally Posted by EscSequenceAlpha View Post
    ok thanks... does ctrl-shift-s combine ctrl-s and ctrl-f11? because thats what I do
    More important is the Ctrl-Shift-O part (it fixes your imports).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. I cant do this.Whats wrong?
    By gaston7eze in forum Java 2D
    Replies: 5
    Last Post: 05-30-2012, 12:34 AM
  2. Whats wrong with this?
    By Alkor in forum AWT / Swing
    Replies: 7
    Last Post: 04-03-2012, 05:49 AM
  3. Whats wrong with the program?
    By fishy8158 in forum New To Java
    Replies: 2
    Last Post: 11-13-2011, 05:30 AM
  4. whats wrong
    By atenv in forum New To Java
    Replies: 6
    Last Post: 06-15-2010, 01:55 PM
  5. whats is wrong with this app??
    By mrajan in forum New To Java
    Replies: 4
    Last Post: 06-09-2010, 10:56 PM

Posting Permissions

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