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);
}
}
}
> 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.
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.
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...
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
}
I would admit though, having a at least a brief description of the problem would of helped.
oy oy... gommenasai.... erm sorry for the lack of info but thanks for the info
Also what are you adding panel to, you haven't done this.
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
That's what I stated earlier: remove "implements ActionListener" from your main class, since it's not overriding actionPerformed, you don't need it...
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);
}
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();
}
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);
}
}
}
Yay!! Thanks for typing that up, I didn't have the patience... :P
I got it wrong it's:frame.getContentPane().add(BorderLayout.SOUTH, panel);frame.getContentPane().add(BorderLayout.CENTER, controlPanel);
Thinking 3:30 in morning aint good for the brain. Ouch.
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);
}
}
}
