猿问

代码如下:paint绘图添加组件不能显示,这是什么情况?

package my_struct;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
public class Gaosi extends JFrame{

private static Point origin;
Gaosi frame=this;

public static void main(String[] args){
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Gaosi frame = new Gaosi();
frame.setVisible(true);
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}

public Gaosi(){
super();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setBounds(300,100,500,460);
this.setUndecorated(true);
com.sun.awt.AWTUtilities.setWindowOpaque(frame,false);

JPanel pane=new JPanel(){
public void paint(Graphics g){
super.paint(g);

g.setColor(new Color(25,205,10));
g.fillRoundRect(0, 0, getWidth(),getHeight(),80,80);
Graphics2D graphics = (Graphics2D) getGraphics();
setFont(new Font("微软雅黑",Font.BOLD,18));
String str = "主元素高斯消去算法";
graphics.drawString(str,156,36);
}
};
this.setContentPane(pane);
pane.setLayout(null);

JTextField text1=new JTextField();
text1.setBounds(150,300,50,22);
pane.add(text1);
this.origin=new Point();
this.addMouseListener(
new MouseAdapter(){
public void mousePressed(MouseEvent e){
origin.x = e.getX();
origin.y = e.getY();
}
//窗体上单击鼠标右键关闭程序
public void mouseClicked(MouseEvent e) {
if(e.getButton()==MouseEvent.BUTTON3)
System.exit(0);
}

}
);

this.addMouseMotionListener(
new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){ //鼠标拖动
Point p = getLocation();
setLocation(p.x + e.getX() - origin.x, p.y + e.getY() - origin.y );
}
}
);
com.sun.awt.AWTUtilities.setWindowOpacity(frame,(float)0.6);

}
}
public void paint(Graphics g){
super.paint(g);
改成:
public void paintComponent(Graphics g){
super.paintComponent(g);
改用这种方法也不能显示组件!

慕田峪4524236
浏览 86回答 1
1回答

jeck猫

public void paint(Graphics g){super.paint(g);改成:public void paintComponent(Graphics g){super.paintComponent(g);
随时随地看视频慕课网APP

相关分类

Java
我要回答