我正在尝试在 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);
}