Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: GUI issue

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default GUI issue

    So I'm making a really simple counter for use while playing Magic the Gathering, my goal is to have 2 buttons (looking like arrows) one to count up and one to count down, however when I try and execute it looks like this:



    I've tried with down.setIgnoreRepaint(true);, but it doesn't help.

    GUI code:

    package magic.counter;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class GUI extends JPanel {
        Image op1 = Toolkit.getDefaultToolkit().getImage("op.png");
        Image ned = Toolkit.getDefaultToolkit().getImage("ned.png");
     
        ImageIcon op = new ImageIcon(op1);
        ImageIcon ned1 = new ImageIcon(ned);
     
        JButton up = new JButton();
        JButton down = new JButton();
     
        int tæller = 20;
     
    public void paintComponent(Graphics g){      
        down.setIgnoreRepaint(true);
        up.setIgnoreRepaint(true);
     
        add(up);
        up.setLocation(75,10);
        up.setBorder(BorderFactory.createEmptyBorder());
        up.setContentAreaFilled(false);
        up.setIcon(op);
     
     
     
        up.addActionListener(new ActionListener() {
     
             public void actionPerformed(ActionEvent e)
             {
                 //Execute when button is pressed
                 tæller++;
                 repaint();            
             }
        });
     
        add (down);
        down.setLocation(75, 150);
        down.setBorder(BorderFactory.createEmptyBorder());
        down.setContentAreaFilled(false);
        down.setIcon(ned1);
     
     
        up.addActionListener(new ActionListener() {
     
             public void actionPerformed(ActionEvent e)
             {
                 //Execute when button is pressed
                 tæller--;
                 repaint();            
             }
        });     
     
        g.drawString(""+tæller+"", 125, 125);          
    }
     
    }


    Main code:

    package magic.counter;
     
    import javax.swing.*;
     
    public class MagicCounter {
     
        public static void main(String[] args) {
            JFrame Vindue = new JFrame("MTG counter");
            Vindue.add( new GUI());
            Vindue.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            Vindue.setSize(625,300);
            Vindue.setVisible(true);
        }
    }


    Please help a young and confused programmer, and don't laugh too hard
    Attached Images Attached Images


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: GUI issue

    Don't add components or do any program logic inside of a paint or paintComponent method. These methods are for painting and painting only. Instead add your components, add your ActionListeners and do your logic in your class's constructor or initComponents or init method (if an applet) or other non-painting methods.
    Last edited by curmudgeon; October 10th, 2012 at 07:06 PM.

  3. The Following User Says Thank You to curmudgeon For This Useful Post:

    Pusillus (October 11th, 2012)

  4. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: GUI issue

    Tried moving it out of paint like so

    public void init(){
     
        add(up);
        add (down);
        up.setLocation(75,10);
        up.setBorder(BorderFactory.createEmptyBorder());
        up.setContentAreaFilled(false);
        up.setIcon(op);
     
     
     
        up.addActionListener(new ActionListener() {
     
             public void actionPerformed(ActionEvent e)
             {
                 //Execute when button is pressed
                 tæller++;
                 repaint();            
             }
        });
     
     
        down.setLocation(75, 150);
        down.setBorder(BorderFactory.createEmptyBorder());
        down.setContentAreaFilled(false);
        down.setIcon(ned1);
     
     
        up.addActionListener(new ActionListener() {
     
             public void actionPerformed(ActionEvent e)
             {
                 //Execute when button is pressed
                 tæller--;
                 repaint();            
             }
        });     
     
        }

    but then the images doesn't show?
    Last edited by Pusillus; October 11th, 2012 at 03:40 AM.

  5. #4
    Member
    Join Date
    Sep 2012
    Location
    The Netherlands
    Posts
    84
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: GUI issue

    I also had problems with the layout from the GUI when I made one a months ago.
    I also had a hard time by placing them in the way I wanted them on the GUI, so I solved it by using a GridLayout.
    On that you can also place a button and attach an image to that.

  6. #5
    Junior Member
    Join Date
    Jun 2012
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: GUI issue

    The problem is still not fixed, any ideas?

  7. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: GUI issue

    The problem is still not fixed, any ideas?
    Please provide a description of what the code should do, what it does do, and the current version of the code.
    I see 6 or 8 possible arrows, which 2 of the group did you want?

  8. #7
    Junior Member
    Join Date
    Jun 2012
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: GUI issue

    The code should be a counter, so basically it needs to be 2 arrows, 1 up and 1 down, and a number, when you press the up arrow it's supposed to count 1 up, and vice versa, however my problem is as displayed on the screenshot that it paints so many arrows.

  9. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: GUI issue

    where is the current version of the code?

  10. #9
    Junior Member
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: GUI issue

    One apparent problem that I see is that you are trying to do absolute positioning for your gui components (buttons) without setting the layout manager to null. You must make a call to setLayout(null) in order for absolute positioning to work as expected. Besides, the code seems dirty to me and you need to do a lot of reading regarding how to build a gui.

Similar Threads

  1. Java Issue / Cache issue
    By VisualPK in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2012, 08:43 PM
  2. A GUI issue.
    By LasOz in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 9th, 2012, 12:46 AM
  3. I am having an issue with my code
    By gmamagoodwin in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 9th, 2012, 11:36 PM
  4. not sure what the issue is here
    By Tfence in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 28th, 2011, 02:18 AM
  5. Tic Tac Toe ActionPerformed Issue
    By Jakesta42 in forum What's Wrong With My Code?
    Replies: 19
    Last Post: August 14th, 2011, 11:11 PM