我似乎是在JPanel下画画

时间:2014-02-04 01:01:46

标签: java swing jpanel paintcomponent

我一直在尝试使用MYSQL数据库创建一个简单的登录程序,当我添加JTextField和JPasswordField时,我在paintComponent方法下绘制的任何东西都会被覆盖。当我不添加我需要的面板时,我看不到我画的东西的问题就消失了。这是我的代码示例。

public void createWindow(){
    frame.add(this);
    frame.setSize(1200, 720);
    etc.. all the necessary stuff
    //userfield is my JTextField
    panel.add(userfield);
    //This next line is the problem
    frame.add(panel);
}

@Override
public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.drawString("Hello", 0, 0);
}

1 个答案:

答案 0 :(得分:1)

JPanel默认是不透明的,这意味着它所覆盖的任何内容都不会被绘制。

尝试使用JPanel透明JPanel#setOpaque透明false