java - 尝试在 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);
}
}
任何帮助表示赞赏,在此先感谢!
解决方案
推荐阅读
- c# - 无法在 WindowsForm 继承上调整 UI 组件的大小 (Visual Studio 2015)
- ruby-on-rails - 如何在 JS 框中显示带有 Active Storage 的图像变体?
- javascript - Lightbox/iframe 中的 Worldpay Javascript API 托管集成不起作用
- ios - 在 NSMutableAttributedString 的子类中调用 append 时应用程序崩溃
- reactjs - 更改所选选项卡的图标颜色 - react-native-router-flux
- devextreme-angular - 以自定义语言本地化 devextreme dx-date-box Angular 2+
- html - &#_integer_; 是什么意思?在 URL SRC 中做什么?
- java - CMD 上不显示 JAVA 版本
- mininet - 如何在 mininet 上添加控制器
- spring-boot - Spring @Transactional annonation 不起作用