gpt4 book ai didi

java - 调用绘制后 JPanel 不显示在 JFrame 中

转载 作者:行者123 更新时间:2023-12-02 07:18:59 24 4
gpt4 key购买 nike

我目前正在实现一个简单的 GUI 界面来与 Lego Mindstorm NXT 交互。我当前的问题在于界面上的绘画问题。当我的 MainUI 加载时,它会调用一个名为 GirdPanel() 的方法来设置我的 GridPanel。MainUI 扩展了 JFrame,然后将此面板添加到其 JFrame 调用中。以下是与此问题相关的 MainUI 的完整代码。

  public MainUI(){
setSize(700, 600);

PathPanel pathPanel = new PathPanel(controller);
add(pathPanel, BorderLayout.WEST);
CurrentPath.getInstance().addPathDataListener(pathPanel);
CurrentPath.getInstance().addPointSelectionListener(pathPanel);

gridPanel();
add(gridPanel, BorderLayout.CENTER);


robotControlBar();
add(robotControls, BorderLayout.NORTH);

setJMenuBar(menuPanel());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}

public void gridPanel(){
gridPanel = new JPanel(new BorderLayout());
WGraph graph = new WGraph();

gridPanel.add(graph, BorderLayout.CENTER);

}

WGraph 是我的类,它扩展了 JPanel 并控制该程序的图形显示。

public class WGraph extends JPanel{

public WGraph(){
//WGraph Panel property setup
setLayout(new BorderLayout());
//Variable creation
points = new ArrayList<Dot>();
//Label to display coordinates of selected point
pointDisplay = new JLabel("Selected point at: None Chosen");

//testPoints(); //Comment Out when test dots not needed.

//Create Graph Panel
panel = new JPanel();
panel.setBackground(PANEL_COLOR);

//Mouse Listeners for Panel
MouseEventHandler mouseListener = new MouseEventHandler();
panel.addMouseListener(mouseListener);
panel.addMouseMotionListener(mouseListener);


//Adding components to the WGraph panel
add(pointDisplay, BorderLayout.NORTH);
add(panel, BorderLayout.CENTER);

repaint();
}
public void paintComponent(Graphics g){
// invokes default painting for JFrame; must have this!
super.paintComponent(g);

// paint on the canvas rather than the JFrame
Graphics pg = panel.getGraphics();
System.out.println("*"); //Print out to see when repaint has been called. for testing only
int width = panel.getWidth();
int height = panel.getHeight();
pg.setColor(GRID_COLOR);

for (int i = 50; i < width; i+=50) {
pg.drawLine(i, 0, i, height);
}

for (int i = 50; i < width; i+=50) {
pg.drawLine(0, i, width, i);
}

Dot previousPoint = null;

for (int i = 0; i < points.size(); i++) {
Dot currentPoint = points.get(i);
currentPoint.draw(pg);

if (previousPoint != null) {
pg.setColor(Dot.DESELECTED_COLOR);
pg.drawLine(new Float(previousPoint.getCenter().x).intValue(),
new Float(previousPoint.getCenter().y).intValue(),
new Float(currentPoint.getCenter().x).intValue(),
new Float(currentPoint.getCenter().y).intValue());
}

previousPoint = currentPoint;
}
}

毕竟我可以描述我的问题了。问题在于图形面板它也不会按预期显示。我正在尝试确定原因。目前,当程序加载时,它看起来像这样。链接1它根本不显示图表,但是当我下拉 JComboBox 时它就会出现。链接2当 JComboBox 选择了一个项目并关闭时,它也会进行重新检查。LINK3然而,当您尝试与它交互时,它会再次消失。评论中的LINK4

有人在我的 JFrame 或 JPanel 构造中看到任何明显的错误吗?您有什么建议可以帮助我弄清楚发生了什么事吗?

旁注:当框架首次加载时,Paint 函数会被调用 3 次。当 JComboBox 打开时再次出现。当 JComboBox 关闭时再次出现。最后,更多次尝试通过单击图表来与图表进行交互。

最佳答案

为什么使用这一行Graphics pg = panel.getGraphics();并使用pg对象在面板上绘制点?为什么不创建另一个扩展 JPanel 的类并重写其 paintComponent 方法来绘制所有所需的点,然后将重写的 Jpanel 类的对象添加到 WGraph 面板?.

例如,考虑下面给出的代码:

import java.awt.Container;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

class MyFrame extends JFrame implements ActionListener
{
private JComboBox jcbShape;
private WGraph jpGraph;
public MyFrame()
{
super("GridFrame");
}
public void prepareGUI()
{
Object[] items= {"Line","Rectangle","Circle"};
jcbShape = new JComboBox(items);
jpGraph = new WGraph();
Container container = getContentPane();
container.add(jpGraph);
container.add(jcbShape,BorderLayout.NORTH);
jcbShape.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300,400);
}
@Override
public void actionPerformed(ActionEvent evt)
{
String sShape = (String)jcbShape.getSelectedItem();
jpGraph.setShape(sShape);
}
public static void main(String[] st)
{
SwingUtilities.invokeLater( new Runnable()
{
@Override
public void run()
{
MyFrame myFrame = new MyFrame();
myFrame.prepareGUI();
myFrame.setVisible(true);
}
});
}
}
class WGraph extends JPanel
{
private String sShape = "Line";
public void setShape(String shape)
{
sShape = shape;
repaint();
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if ("Line".equalsIgnoreCase(sShape))
{
g.drawLine(10, 20, 100, 200);
}
else if ("Circle".equalsIgnoreCase(sShape))
{
g.drawOval(50, 100 , 200, 200);
}
else if ("Rectangle".equalsIgnoreCase(sShape))
{
g.drawRect(10, 20, 150, 200);
}
}
}

关于java - 调用绘制后 JPanel 不显示在 JFrame 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14506485/

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