有人能提供一个Java ui的基本例子吗?在这个例子中,点击按钮可以在旁边的jpanel上绘制一个矩形?
你可以找到捕捉鼠标的绘图示例,或者加载ui时的静态绘图示例,但我找不到一个组件用于(单击)在另一个组件上绘图的示例。
我有一个用户界面,用户可以在其中定义框的数量(行和列),ok按钮应该在模拟一张纸的JPanel上绘制这些框。
谢谢你的帮助,非常感激。
如果您想在组件上绘制内容,请覆盖其paintComponent方法
基本伪示例,使用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(...);
}
}
以下是我的操作方法。请告诉我您将如何改进此初学者代码!)
从本质上讲,这里有一个绘制矩形的按钮的诀窍:
-
将主应用程序类扩展到JFrame(或JComponent)或???
-
在主应用程序的顶部声明要绘制的类(DrawCanvas)并扩展到JPanel。
-
就在你的主应用程序类的顶部声明了一个ArrayList来存放你要画的东西。
-
在主应用程序类的顶部为绘图类声明一个变量。
-
在你的控制事件(在我的例子中是按钮)中,使用一个函数来准备要绘制的东西(我使用了一个名为AddRectangle()的函数)。
-
在绘图类中,覆盖paintComponent,并使用for each来绘制数组中存放的所有东西。
由于您不能直接控制绘图,因此您必须了解,每次重新绘制()被调用时,都会调用绘图函数。这意味着你必须像一只带血的松鼠一样把所有的东西都藏起来,这样绘图方法才能正确地绘制或重新绘制屏幕。最后,通常会使用一堆数组,每个循环使用一堆。
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);
}
}