我正在尝试在Java程序中绘制一条线,但该线尚未绘制
我已经尝试了所有功能,但在JLable上仍然没有一行
我不知道为什么我画线后JLable的图形不更新,而且它仍然是空的。
请帮帮我
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class t
{
private static JFrame frame;
private static JLabel field;
public static void main(String[] args)
{
frame = new JFrame("Simple Server");
frame.setLayout(new FlowLayout());
frame.setPreferredSize(new Dimension(1200, 700));
frame.setSize(new Dimension(1200, 700));
frame.setMinimumSize(new Dimension(1200, 700));
frame.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent we)
{
System.gc();
System.exit(0);
}
});
int maxW = 1000, maxH = 600;
field = new JLabel();
field.setSize(maxW, maxH);
field.setPreferredSize(new Dimension(maxW, maxH));
field.setMaximumSize(new Dimension(maxW, maxH));
field.setMinimumSize(new Dimension(maxW, maxH));
field.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
field.setBackground(Color.GREEN);
field.setOpaque(true);
frame.add(field, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
Graphics g = field.getGraphics();
g.drawLine(0, 0, 100, 100);
field.paintComponents(g);
field.paint(g);
field.paintAll(g);
field.update(g);
field.repaint();
frame.paint(g);
frame.paintAll(g);
frame.paintComponents(g);
frame.update(g);
frame.repaint();
frame.setVisible(true);
}
若要使摆动图形一致,请执行以下操作:
public class CrossedLabel extends JLabel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(0, 0, 100, 100);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句