-1

有人可以提供一个 Java ui 的基本示例,其中单击按钮会在旁边的 jpanel 上绘制一个矩形?

您可以找到捕获鼠标的绘图示例,或者通过加载 ui 进行静态绘图的示例,但我找不到用于(单击)在另一个组件上绘制的一个组件的示例。

我有一个用户界面,其中用户定义了框(行和列)的数量,并且确定按钮应该在模拟一张纸的 JPanel 上绘制这些框。

感谢您的帮助,非常感谢。

4

2 回答 2

0

这就是我的做法。请让我知道您将如何改进这个初学者代码!;)

本质上,这里的技巧是让一个按钮来绘制矩形:

  • 将您的主应用程序类扩展为 JFrame(或 JComponent)或 ???

  • 在主应用程序的顶部声明要绘制的类 (DrawCanvas) 并扩展到 JPanel。

  • 在您的主应用程序类的顶部声明了一个 ArrayList 来保存您将绘制的内容。

  • 在主应用程序类的顶部为绘图类声明一个变量。

  • 在您的控制事件(在我的情况下为按钮)中,使用一个函数来准备要绘制的东西(我使用了一个名为 AddRectangle() 的函数)。

  • 在您的绘图类中,覆盖paintComponent 并使用a for each 来绘制您存储在数组中的所有内容。

由于您无法直接控制绘图,因此您必须了解每次调用 repaint() 时都会调用该绘图函数。这意味着你必须像该死的松鼠一样把你所有的东西藏起来,以便绘图方法正确地绘制或重绘屏幕。最后通常是一堆你最终会使用的数组和一堆 for每个循环都要经过它们。

package views;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.*;



public class appMainWindow extends JFrame
{
class PdfLocation
{
    public double xPos;
    public double yPos; 
}

class DrawCanvas extends JPanel
{
      @Override
      public void paintComponent(Graphics g) 
      {
         super.paintComponent(g);
         for (PdfLocation p : PdfLocations)
         {
             g.drawRect((int)p.xPos, (int)p.yPos, 35, 20);
             repaint();
         }
      }
}

public void AddRectangle()
{
    PdfImagesCount++;
    lblPdfcount.setText(Integer.toString(PdfImagesCount));

    PdfLocation rect = new PdfLocation();

    if (PdfLocations.isEmpty() == false)
    {
        PdfLocation spot = PdfLocations.get(PdfLocations.size() - 1);
        rect.xPos = spot.xPos + 45;
        rect.yPos = 10;
    }   
    else
    {
        rect.xPos = 10;
        rect.yPos = 10;         
    }
    PdfLocations.add(rect);             
}

private JFrame frame;
public ArrayList<PdfLocation> PdfLocations = new  ArrayList<PdfLocation>();
public int PdfImagesCount = 0;


public static final int CANVAS_HEIGHT = 700;
public static final int CANVAS_WIDTH = 1000;

private DrawCanvas canvas;
private JLabel lblPdfcount;

public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { appMainWindow window = new appMainWindow(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); }

public appMainWindow() 
{ 

    // Set up a custom drawing JPanel
    canvas = new DrawCanvas();
    canvas.setBackground(Color.WHITE);
    canvas.setBounds(150, 25, CANVAS_WIDTH, CANVAS_HEIGHT);
    initialize(); 
}

private void initialize() 
{
    frame = new JFrame();
    frame.setBounds(100, 100, 1280, 850);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JButton btnAddARectangle = new JButton("Add a rectangle");
    btnAddARectangle.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) 
        {
            AddRectangle();
            repaint();
        }
    });
    btnAddARectangle.setBounds(0, 6, 151, 29);
    frame.getContentPane().add(btnAddARectangle);


    frame.getContentPane().add(canvas);

    lblPdfcount = new JLabel("PdfCount");
    lblPdfcount.setBounds(10, 43, 61, 16);
    frame.getContentPane().add(lblPdfcount);
}

}

于 2014-09-09T17:00:02.190 回答
0

如果你想在一个组件上一些东西,覆盖它的paintComponent-Method。

基本的示例,使用 JPanel :

public class MyPanel extends JPanel
{
   public void paintComponent(Graphics g)
   {
      super.paintComponent(g);
      //here your draw stuff
      //like: 
      Graphics2D g2d = (Graphics2D)g;
      g.drawLine(...);
   }
}
于 2014-08-31T21:12:49.223 回答