我正在尝试制作图形的x轴和y轴,当窗口大小发生变化时,该轴可以自动调整大小This is my code, I'm trying to use addComponentlistener to help resize the axis but it doesn't work. I guess it have to do with graphics g is inside another anonymous class, but I'm not sure how to fix it. (I'm noob)
display = new JPanel(){
@Override
public void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
addComponentListener(new ComponentAdapter(){
public void componentResized(ComponentEvent e){
Component c = (Component)e.getSource();
int x1 = c.getWidth()/8;
int y1 = c.getHeight()/4;
int x2 = c.getWidth()*7/8;
int y2 = c.getHeight()/4;
g.drawLine(10,100,100,100 );
}});
}
};