我不知道我在做什么 - 油漆方法等

时间:2015-11-05 01:48:02

标签: java swing paint

我一直在努力使用Paint方法和绘制组件,并扩展JFrame,并且已经尝试了各种方法来绘制一个简单的矩形。这是一个名为Window的类:

import javax.swing.*;
import java.awt.Graphics;

public class Window extends JFrame 
{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    Window()
    {
        setSize(300,300);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    public void paint(Graphics g)
    {
        g.drawRect(300,300,300,300);
    }
}

然后是主要的

public class Main {

        public static void main(String args[])
        {
            Window mainWindow = new Window();
            mainWindow.setBounds(100,100,300,300);
        }
}

这个程序的唯一目的就是绘制一个该死的矩形。我不知道我做错了什么,而且我一直在尝试drawRect或drawString几天,但无济于事。我也试过一个小组。

2 个答案:

答案 0 :(得分:4)

请勿覆盖paint等顶级容器的JFrame,这是结束奇怪和意外结果的最快捷方式。

在框架实际表面与使用之间,有一个JRootPane,一个contentPane,可能还有一个glassPane ......

RootPane

所有这些都可以在paint方法中界面/删除您绘制的内容。

相反,请先使用JPanel并覆盖paintComponent。创建一个这样的实例,并在想要显示它时将其放在JFrame的实例上。

有关详细信息,请查看Painting in AWT and SwingPerforming Custom Painting

另外,请注意,在绘画时,0x0是组件的顶部/左侧,因此在您的示例中,您开始在300x300绘画,但您的框架仅为300x300,所以你实际上在画画了

举个例子:

enter image description here

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestPaint {

    public static void main(String[] args) {
        new TestPaint();
    }

    public TestPaint() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new PaintPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class PaintPane extends JPanel {

        public PaintPane() {
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.drawRect(10, 10, getWidth() - 20, getHeight() - 20);
            g2d.dispose();
        }

    }

}

答案 1 :(得分:-1)

你能试试这段代码

吗?

import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main extends JPanel {

   public static void main(String[] a) {
      JFrame f = new JFrame();
      f.setSize(400, 400);
      f.add(new Main());
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.setVisible(true);
   }

   public void paint(Graphics g) {
      g.fillRect (5, 15, 50, 75);
   }
}

相关问题