JFrame 的奇怪显示问题
在下面的简单代码中,我只是创建一个框架,并向其添加一个 JPanel
和 menubar
。
public class MainFrame extends JFrame {
private DrawPanel drawPanel;
public MainFrame()
{
super("Coordinate Geometry Visualiser");
drawPanel = new DrawPanel();
add(drawPanel);
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic('F');
JMenuItem newItem = new JMenuItem("New");
newItem.setMnemonic('N');
fileMenu.add(newItem);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
menuBar.add(fileMenu);
JMenu editMenu = new JMenu("Edit");
editMenu.setMnemonic('E');
menuBar.add(editMenu);
}
}
绘制面板代码 -
public class DrawPanel extends JPanel {
public DrawPanel()
{
}
public void paintComponent(Graphics g)
{
super.paintComponents(g);
setBackground(Color.BLACK);
g.setColor(Color.RED);
g.drawLine(100, 50, 150, 100);
}
}
最后使用 main()
绘制应用程序
public class CGVApplication {
public static void main(String[] args) {
MainFrame appFrame = new MainFrame();
appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
appFrame.setSize(300, 275);
appFrame.setVisible(true);
}
}
在 eclipse 内运行应用程序,这就是我得到的 -
为什么有双菜单栏和线条?这很烦人。在循环应用程序或出现弹出窗口时,重新绘制的窗口很好(右侧图像)。
另外,在我的 DrawPanel paintComponent
中,我已将背景设置为黑色,但没有效果?
造成上述两个问题的原因是什么?请帮忙!
In the following simple code I simply create a Frame, and add a JPanel
and menubar
to it.
public class MainFrame extends JFrame {
private DrawPanel drawPanel;
public MainFrame()
{
super("Coordinate Geometry Visualiser");
drawPanel = new DrawPanel();
add(drawPanel);
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic('F');
JMenuItem newItem = new JMenuItem("New");
newItem.setMnemonic('N');
fileMenu.add(newItem);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
menuBar.add(fileMenu);
JMenu editMenu = new JMenu("Edit");
editMenu.setMnemonic('E');
menuBar.add(editMenu);
}
}
Draw Panel code -
public class DrawPanel extends JPanel {
public DrawPanel()
{
}
public void paintComponent(Graphics g)
{
super.paintComponents(g);
setBackground(Color.BLACK);
g.setColor(Color.RED);
g.drawLine(100, 50, 150, 100);
}
}
and finally the application with main()
public class CGVApplication {
public static void main(String[] args) {
MainFrame appFrame = new MainFrame();
appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
appFrame.setSize(300, 275);
appFrame.setVisible(true);
}
}
On running the application inside eclipse, this is what i get -
Why the double menubar and line? This is very annoying. On cycling through applications or when a popup comes the redrawn window is fine (Right side image).
Also in my DrawPanel paintComponent
i have set background as black, but no effect?
What is the reason for above two issues? Please help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在调用 Container.paintComponents() 方法。它必须是
super.paintComponent(g)
。You are calling Container.paintComponents() method. It must be
super.paintComponent(g)
.javadoc 提到
将 setBackground 放入构造函数中,并在
paintComponent
中添加这两行代码(在绘制红线之前)使面板变黑。另请注意,Swing 组件应始终在 EDT 中创建和修改。你的主要方法应该是这样的:
The javadoc mentions that
Putting setBackground in the constructor, and adding these two lines of code in
paintComponent
(before painting the red line) makes the panel black.Also note that Swing components should always be created and modified in the EDT. Your main method should be like this: