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