gpt4 book ai didi

java - 使用其图形在另一个内部绘制控件

转载 作者:行者123 更新时间:2023-11-29 07:08:46 25 4
gpt4 key购买 nike

我基本上是在尝试通过调用第二个组件的绘制并将其传递给第一个组件的图形来在另一个内部绘制一个 JComponent。

我正在尝试创建一个 GUI 编辑器,(重新发明轮子,我知道,这只是一个概念证明)所以我有一个扩展 JPanel 的类,我想在其中从 VectorControls 绘制组件。

到目前为止,我在扩展的 JPanel 中得到了这个方法:

@SuppressWarnings("serial")
public class Sketch extends JPanel {
private Vector<JComponent> controls = new Vector<JComponent>();

public Sketch() {
super();
this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
}

public void addControl(JComponent c) {
Dimension d = new Dimension(100,50);
c.setPreferredSize(d);
c.setMinimumSize(d);
c.setMaximumSize(d);
controls.add(c);
this.repaint();
this.revalidate();
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
for(int i=controls.size()-1; i>=0; i--) {
JComponent c = controls.get(i);
c.paint(g);
}
}
}

我正在像这样构建/附加 Sketch 面板:

public GUIEditor() {
mainFrame = new JFrame("GUI EDITOR");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Sketch mainPanel = new Sketch();
mainPanel.setPreferredSize(new Dimension(640,480));

GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();

mainFrame.setLayout(gbl);

JPanel toolsPanel = new JPanel();
toolsPanel.setPreferredSize(new Dimension(160,480));
toolsPanel.setLayout(new GridLayout(0,1));

for(Control c : toolBoxItems ) {
AbstractAction action = new ToolBoxButtonAction(mainPanel, c.type);
JButton b = new JButton(action);
b.setText(c.title);
toolsPanel.add(b);
}

gbc.gridx = 0;
gbc.gridy = 0;
gbl.setConstraints(mainPanel, gbc);
mainFrame.add(mainPanel);

gbc.gridx = 1;
gbc.gridy = 0;
gbl.setConstraints(toolsPanel, gbc);
mainFrame.add(toolsPanel);

mainFrame.pack();
mainFrame.setLocationRelativeTo(null);
mainFrame.setVisible(true);
}

在 ToolBoxButtonAction 中,基本上我是这样做的:

public void actionPerformed(ActionEvent e) {
try {
sketch.addControl(control.newInstance());
} catch (InstantiationException e1) {
e1.printStackTrace();
} catch (IllegalAccessException e1) {
e1.printStackTrace();
}
}

但我写这个是因为它不起作用。

关于如何实现这一点有什么想法吗?

最佳答案

I'm basically trying to draw a JComponent inside another by calling the second component's paint passing it the first component's Graphics.

只有当组件具有非零尺寸时才能绘制组件。通常,组件的大小由布局管理器决定。

您的基本代码看起来很合理,但除非您有代码来确定组件的大小和位置,否则您将看不到任何东西。如果您只是设置大小,那么所有组件都会相互叠加。

或者问题可能是您的父面板没有尺寸,所以它甚至没有被绘制。默认的 FlowLayout 使用子组件的首选大小来确定面板大小。由于您不直接向面板添加组件,因此没有子组件,因此首选大小将为 0。当您重新发明轮子时,您需要重新发明一切。

如果没有 SSCCE,您如何使用此代码的上下文是未知的,我们所能做的就是猜测。

编辑:

遇到问题时创建 SSCCE,并在尝试让它动态工作之前让它使用硬编码值。像这样的东西:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class Sketch extends JComponent
{
private Vector<JComponent> controls = new Vector<JComponent>();

public void addControl(JComponent c)
{
c.setSize(100, 50);
int location = controls.size() * 50;
c.setLocation(location, location);
controls.add(c);
repaint();
}

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

for(int i=controls.size()-1; i>=0; i--)
{
JComponent c = controls.get(i);
Point location = c.getLocation();
g.translate(location.x, location.y);
c.paint(g);
g.translate(-location.x, -location.y);
}
}

private static void createAndShowUI()
{
Sketch sketch = new Sketch();
sketch.addControl( new JButton("button") );
sketch.addControl( new JTextField(10) );
sketch.addControl( new JCheckBox("Checkbox") );

JFrame frame = new JFrame("Sketch");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( sketch );
frame.setSize(400, 400);
frame.setLocationByPlatform( true );
frame.setVisible( true );
}

public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}

关于java - 使用其图形在另一个内部绘制控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16484575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com