I have a code below that is suppose to draw lines across each otherin a window f
ID: 3608165 • Letter: I
Question
I have a code below that is suppose to draw lines across each otherin a window frame using a Borderline layout.My code is saying my variables lineNorth lineSouth etc are nonstatic and cannot be referenced from a static context. What can ido about that???
import java.awt.*;
import javax.swing.*;
import java.awt.Color;
public class X2 extends JComponent {
private Color lc = Color.blue ;
private X2 lineNorth ;
private X2 lineEast ;
private X2 linelSouth ;
private X2 linelWest ;
public X2() {
setPreferredSize(new java.awt.Dimension(25,25));
lc = Color.blue ;
lineNorth = new X2() ;
lineEast = new X2() ;
linelSouth = new X2() ;
linelWest = new X2() ;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(lc);
g.drawLine(0, 0, getWidth()-1,getHeight()-1);
g.drawLine(0, getHeight()-1, getWidth()-1,0);
}
public static void main(String[] agrs )
{
JFrame win = new JFrame("Test Component");
win.add(lineNorth, BorderLayout.NORTH);
win.add(lineSouth, BorderLayout.SOUTH);
win.add(lineEast, BorderLayout.EAST);
win.add(linelWest, BorderLayout.WEST);
win.setSize(400, 300);
win.setVisible(true);
}
public Color getLineColor() {
return lc;
}
public void setLineColor(Color rgb ) {
lc = rgb;
repaint();
}
}
Explanation / Answer
please rate - thanks public static void main(String[] agrs){
JFrame win = new JFrame("Test Component");
win.add(lineNorth, BorderLayout.NORTH);
win.add(lineSouth,BorderLayout.SOUTH);
win.add(lineEast, BorderLayout.EAST);
win.add(linelWest, BorderLayout.WEST);
win.setSize(400, 300);
win.setVisible(true);
}
getting rid of the static (in red above) will get rid of thecompilation error. note: lineSouth should be line1South. I just compiled it didn't run it!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.