嘿,我试图在JFrame内的JWindow的中间放置一条垂直线,但是无论出于什么原因,我都不会在JFrame或JWindow中显示该线。我尝试了很多paint和paintComponent的组合,但到目前为止没有任何效果。我已经成功地在另一个项目中成功地使用了绘画,但是由于某种原因它不会出现在这个项目中。
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);
}
}
感谢您的帮助,谢谢!