首页 > 解决方案 > 尝试在 JWindow 内使用绘画,但没有显示

问题描述

嘿,我试图在 JFrame 内部的 JWindow 中间放置一条垂直线,但无论出于某种原因我做什么,这条线都不会出现在 JFrame 或 JWindow 中。我已经尝试了很多油漆和油漆组件的组合,但到目前为止没有任何效果。我已经能够在另一个项目中成功地使用油漆,但由于某种原因它不会出现在这个项目中。

import javax.swing.*; 
import java.awt.*;
import java.awt.geom.Line2D;
import java.awt.geom.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

public class MaxwellsDemon extends JFrame {

   static JFrame frame;

public static void main(String[] args)
{
    MaxwellsDemon mwd = new MaxwellsDemon();
}

public MaxwellsDemon()
{
    frameCreator();
    repaint();
}

public void frameCreator()
{

    //Create and set up the window.
    frame = new JFrame("Maxwell's Demon");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(800,600);



    JLabel label = new JLabel("Maxwell's Demon");
    frame.getContentPane().add(label);

    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(null);

    JButton particleAddButton = new JButton("Add Particle");
    particleAddButton.setBounds(710,100,80,20);
    JButton resetGameButton = new JButton("Reset Game");
    resetGameButton.setBounds(710,200,80,20);

    buttonPanel.add(resetGameButton);
    buttonPanel.add(particleAddButton);
    frame.getContentPane().add(buttonPanel);

    JWindow mainWindow = new JWindow(frame);
    JPanel windowPanel = new JPanel();
    JLabel windowLabel = new JLabel("Window Test");
    mainWindow.add(windowPanel);
    windowPanel.add(windowLabel);

    mainWindow.setSize(700,524);
    windowPanel.setBackground(Color.white);
    windowPanel.setBorder(BorderFactory.createLineBorder(Color.black)); 
    mainWindow.setVisible(true);
    mainWindow.setLocation(0,100);

    frame.setVisible(true);
}
 @Override
 public void paintComponents(Graphics g) {
        System.out.println("Reaching Paint");
        super.paintComponents(g);
        Graphics2D g2 = (Graphics2D) g;
        Line2D lin = new Line2D.Float(100, 100, 100, 400);
        g2.draw(lin);
    }

}

任何帮助表示赞赏,在此先感谢!

标签: javajframe

解决方案


推荐阅读