java paint绘图添加组件不能显示
代码如下:packagemy_struct;importjavax.swing.JFrame;importjavax.swing.JPanel;importjavax.s...
代码如下:
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);
改用这种方法也不能显示组件! 展开
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);
改用这种方法也不能显示组件! 展开
1个回答
2010-10-02
展开全部
public void paint(Graphics g){
super.paint(g);
改成:
public void paintComponent(Graphics g){
super.paintComponent(g);
super.paint(g);
改成:
public void paintComponent(Graphics g){
super.paintComponent(g);
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询