本文介绍了使用paint(Graphics p)时在java中删除一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下函数画了一行:

I have drawn a line using the below function:

public void paint(Graphics p) {
    super.paint(p);
    p.drawLine(600, 200, 580, 250);
}

我想知道有没有办法可以删除这一行?

I am wondering is there a way that I can delete this line?

那么是否可以在程序的 main()方法中调用此函数?

Then is it possible to call this function in the main() method of a program?

推荐答案

您可以使用标志来了解该行是否正在显示。

You can use a flag to know whether the line is displaying or not.

正如我之前所说,你需要建立你的GUI JPanel s而不是 JFrame s。还覆盖 paintComponent 而不是 paint 方法。

As I said before you need to build your GUI towards JPanels and not JFrames. Also overriding paintComponent and not paint method.

For例如,当您单击 JButton 时,以下程序会显示一行或隐藏它,根据您自己的条件将该逻辑调整到您自己的程序。

For example, the following program displays a line or hides it when you click the JButton, adapt that logic to your own program with your own conditions.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class LineDrawer {
    private JFrame frame;
    private JButton button;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new LineDrawer()::createAndShowGui); //Put our program on the EDT
    }

    private void createAndShowGui() {
        frame = new JFrame(getClass().getSimpleName());

        MyPane pane = new MyPane(); //Create an instance of our custom JPanel class
        button = new JButton("Hide/Show");

        button.addActionListener(e -> {
            pane.setShowLine(!pane.isShowLine()); //Change the state of the flag to its inverse: true -> false / false -> true
        });

        frame.add(pane);
        frame.add(button, BorderLayout.SOUTH);

        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    //Our custom class that handles painting.
    @SuppressWarnings("serial")
    class MyPane extends JPanel {
        private boolean showLine;

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            Graphics2D g2d = (Graphics2D) g;
            if (showLine) { //If true, show line
                g2d.draw(new Line2D.Double(50, 50, 100, 50));
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(300, 300); //For the size of our JPanel
        }

        public boolean isShowLine() {
            return showLine;
        }

        public void setShowLine(boolean showLine) {
            this.showLine = showLine;
            this.repaint(); //Everytime we set a new state to showLine, repaint to make the changes visible
        }
    }
}

我现在无法发布GIF,但是程序本身有效。顺便说一句上面的代码被称为,并且在您接下来的问题上,我们鼓励您发布一个代码以便获得具体,更快,更好的问题答案。

I can't post a GIF right now, but the program itself works. Btw the above code is called a Minimal, Complete and Verifiable Example and on your next questions you're encouraged to post one in order to get specific, faster and better answers to your questions.

这篇关于使用paint(Graphics p)时在java中删除一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 05:04