Broken code

what am i doing wrong?

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

publicclass MoveToRectangleimplements ActionListener

{

private JFrame frame;

private JPanel panel;

private JTextField xTextField;

private JTextField yTextField;

privateint x=0;

privateint y=0;

privatefinalint WIDE=40;

privatefinalint HIGH=60;

publicvoid go()

{

frame =new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setTitle("MoveToRectangle Frame");

JButton locationbutton =new JButton("Move To");

locationbutton.addActionListener(this);

JLabel xLabel =new JLabel("X");

JLabel yLabel =new JLabel("Y");

xTextField =new JTextField(6);

yTextField =new JTextField(6);

xTextField.setText("0");

yTextField.setText("0");

JPanel controlPanel =new JPanel();

controlPanel.add(xLabel);

controlPanel.add(xTextField);

controlPanel.add(yLabel);

controlPanel.add(yTextField);

controlPanel.add(locationbutton);

panel =new RectanglePanel();

frame.getContentPane().add(BorderLayout.SOUTH, locationbutton);

frame.getContentPane().add(BorderLayout.CENTER, controlPanel);

frame.setSize(300, 300);

frame.setVisible(true);

}

class LocationListenerimplements ActionListener

{

publicvoid actionPerformed(ActionEvent e)

{

String xStr, yStr;

xStr = xTextField.getText();

x = Integer.parseInt(xStr);

yStr = yTextField.getText();

y = Integer.parseInt(yStr);

}

}

class RectanglePanelextends JPanel

{

publicvoid paintComponent(Graphics g)

{

Graphics2D g2d = (Graphics2D) g;

Rectangle box =new Rectangle(x, y, WIDE, HIGH);

g2d.draw(box);

}

}

}

[3743 byte] By [monkeysenseia] at [2007-10-2 7:11:48]
# 1
> what am i doing wrong? For one, you're not asking a specific question. Do you expect someone here to run your code and figure out what your problem is, and then tell you how to fix that problem? I'd bet everyone here will pass on that. The ball is back in your court.
warnerjaa at 2007-7-16 20:45:13 > top of Java-index,Java Essentials,Java Programming...
# 2
Perhaps in future you should implement exception handling within your code. Then simply display the output of printStackTrace to the forum. Perhaps then we could actually help.
Valehrua at 2007-7-16 20:45:13 > top of Java-index,Java Essentials,Java Programming...
# 3

Well, that wasn't very specific... :)

If you're trying to access the actionPerformed() method within LocationListener, I'm not sure that'll work... You must make MoveToRectangle override actionPerformed instead, or point to an instance of LocationListener... Correct me, people!! I'm not sure about this...

Not sure if JFrame has a BorderLayout by default either, otherwise, you'll have to set the layout manually...

Call repaint() after you change the rectangle's coords, as well...

vagrantcharlya at 2007-7-16 20:45:13 > top of Java-index,Java Essentials,Java Programming...
# 4

Try:

public void actionPerformed(ActionEvent e)

{

String xStr, yStr;

xStr = xTextField.getText();

x = Integer.parseInt(xStr);

yStr = yTextField.getText();

y = Integer.parseInt(yStr);

panel.repaint(); // your panel needs to repaint after event

}

gaffer_uka at 2007-7-16 20:45:13 > top of Java-index,Java Essentials,Java Programming...
# 5
I would admit though, having a at least a brief description of the problem would of helped.
gaffer_uka at 2007-7-16 20:45:13 > top of Java-index,Java Essentials,Java Programming...
# 6
oy oy... gommenasai.... erm sorry for the lack of info but thanks for the info
monkeysenseia at 2007-7-16 20:45:13 > top of Java-index,Java Essentials,Java Programming...
# 7
Also what are you adding panel to, you haven't done this.
gaffer_uka at 2007-7-16 20:45:13 > top of Java-index,Java Essentials,Java Programming...
# 8
every time i try to compile, it gives me some kind of abstract error.something about this code not being abstract and does not override the actionpreformed method
monkeysenseia at 2007-7-16 20:45:13 > top of Java-index,Java Essentials,Java Programming...
# 9
That's what I stated earlier: remove "implements ActionListener" from your main class, since it's not overriding actionPerformed, you don't need it...
vagrantcharlya at 2007-7-16 20:45:13 > top of Java-index,Java Essentials,Java Programming...
# 10

Change your code from this:

class LocationListener implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

String xStr, yStr;

xStr = xTextField.getText();

x = Integer.parseInt(xStr);

yStr = yTextField.getText();

y = Integer.parseInt(yStr);

}

}

to this :

public void actionPerformed(ActionEvent e)

{

String xStr, yStr;

xStr = xTextField.getText();

x = Integer.parseInt(xStr);

yStr = yTextField.getText();

y = Integer.parseInt(yStr);

}

gaffer_uka at 2007-7-16 20:45:13 > top of Java-index,Java Essentials,Java Programming...
# 11

Also add the repaint so its:

public void actionPerformed(ActionEvent e)

{

String xStr, yStr;

xStr = xTextField.getText();

x = Integer.parseInt(xStr);

yStr = yTextField.getText();

y = Integer.parseInt(yStr);

panel.repaint();

}

gaffer_uka at 2007-7-16 20:45:13 > top of Java-index,Java Essentials,Java Programming...
# 12

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class MoveToRectangle implements ActionListener

{

private JFrame frame;

private JPanel panel;

private JTextField xTextField;

private JTextField yTextField;

private int x=0;

private int y=0;

private final int WIDE=40;

private final int HIGH=60;

public void go()

{

frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setTitle("MoveToRectangle Frame");

JButton locationbutton = new JButton("Move To");

locationbutton.addActionListener(this);

JLabel xLabel = new JLabel("X");

JLabel yLabel = new JLabel("Y");

xTextField = new JTextField(6);

yTextField = new JTextField(6);

xTextField.setText("0");

yTextField.setText("0");

JPanel controlPanel = new JPanel();

controlPanel.add(xLabel);

controlPanel.add(xTextField);

controlPanel.add(yLabel);

controlPanel.add(yTextField);

controlPanel.add(locationbutton);

panel = new RectanglePanel();

frame.getContentPane().add(BorderLayout.SOUTH, locationbutton);

frame.getContentPane().add(BorderLayout.CENTER, controlPanel);

frame.getContentPane().add(BorderLayout.NORTH, panel);

frame.setSize(300, 300);

frame.setVisible(true);

}

public void actionPerformed(ActionEvent e)

{

String xStr, yStr;

xStr = xTextField.getText();

x = Integer.parseInt(xStr);

yStr = yTextField.getText();

y = Integer.parseInt(yStr);

panel.repaint();

}

class RectanglePanel extends JPanel

{

public void paintComponent(Graphics g)

{

Graphics2D g2d = (Graphics2D) g;

Rectangle box = new Rectangle(x, y, WIDE, HIGH);

g2d.draw(box);

}

}

}

gaffer_uka at 2007-7-16 20:45:13 > top of Java-index,Java Essentials,Java Programming...
# 13
Yay!! Thanks for typing that up, I didn't have the patience... :P
vagrantcharlya at 2007-7-16 20:45:13 > top of Java-index,Java Essentials,Java Programming...
# 14
I got it wrong it's:frame.getContentPane().add(BorderLayout.SOUTH, panel);frame.getContentPane().add(BorderLayout.CENTER, controlPanel);
gaffer_uka at 2007-7-16 20:45:13 > top of Java-index,Java Essentials,Java Programming...
# 15
Thinking 3:30 in morning aint good for the brain. Ouch.
gaffer_uka at 2007-7-20 19:13:21 > top of Java-index,Java Essentials,Java Programming...
# 16

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class MoveToRectangle implements ActionListener

{

private JFrame frame;

private JPanel panel;

private JTextField xTextField;

private JTextField yTextField;

private int x=0;

private int y=0;

private final int WIDE=40;

private final int HIGH=60;

public void go()

{

frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setTitle("MoveToRectangle Frame");

JButton locationbutton = new JButton("Move To");

locationbutton.addActionListener(this);

JLabel xLabel = new JLabel("X");

JLabel yLabel = new JLabel("Y");

xTextField = new JTextField(6);

yTextField = new JTextField(6);

xTextField.setText("0");

yTextField.setText("0");

JPanel controlPanel = new JPanel();

controlPanel.add(xLabel);

controlPanel.add(xTextField);

controlPanel.add(yLabel);

controlPanel.add(yTextField);

controlPanel.add(locationbutton);

panel = new RectanglePanel();

frame.getContentPane().add(BorderLayout.SOUTH, panel);

frame.getContentPane().add(BorderLayout.CENTER, controlPanel);

frame.setSize(300, 300);

frame.setVisible(true);

}

public void actionPerformed(ActionEvent e)

{

String xStr, yStr;

xStr = xTextField.getText();

x = Integer.parseInt(xStr);

yStr = yTextField.getText();

y = Integer.parseInt(yStr);

panel.repaint();

}

class RectanglePanel extends JPanel

{

public void paintComponent(Graphics g)

{

Graphics2D g2d = (Graphics2D) g;

Rectangle box = new Rectangle(x, y, WIDE, HIGH);

g2d.draw(box);

}

}

}

gaffer_uka at 2007-7-20 19:13:21 > top of Java-index,Java Essentials,Java Programming...