专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > J2SE

pain方法的使用解决思路

发布时间:2010-06-05 05:30:27 文章来源:www.iduyao.cn 采编人员:星星草
pain方法的使用
package javaclass;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class ComponentEvents extends JFrame implements ActionListener
{
JButton b1=new JButton("改变框架的主题.");
JButton b2=new JButton("还原框架的主题");
JButton btnColor=new JButton("改变按钮的颜色");
JButton btnRestore=new JButton("还原按钮颜色");
//Graphics gra=new Graphics();
Color color=btnColor.getBackground();
public ComponentEvents ()
{
super("ComponentEvents");
setSize(600,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b1.addActionListener(this);
b2.addActionListener(this);
btnColor.addActionListener(this);
btnRestore.addActionListener(this);
JPanel pane=new JPanel();
pane.setLayout(null);
pane.add(b1);
pane.add(b2);
pane.add(btnColor);
pane.add(btnRestore);
setContentPane(pane);
setVisible(true);
}
public void actionPerformed(ActionEvent evt)
{
Object source=evt.getSource();
if(source==b1)
setTitle("dkf");
else if(source==b2)
setTitle("ComponentEvents");
else if (source==btnColor)
{
btnColor.setBackground(Color.orange);
}
else if (source==btnRestore)
{
btnColor.setBackground(color);
}
}
public void paint(Graphics g)
{
b1.setBounds(10,20,150,20);
b2.setBounds(160,20,150,20);
btnColor.setBounds(10,60,150,20);
btnRestore.setBounds(160,60,150,20);
g.drawLine(10,170,500,170);
//repaint();
}
public static void main(String[] args) 
{
ComponentEvents comevt= new ComponentEvents();
}
}


我想问下,当程序运行时,窗口有四个按钮与一条直线,但当最小化时,再还原,四个按钮消失.请问大家这是什么问题.

------解决方案--------------------
我给你改了哈,可以了,我觉得不要继承JFrame这个类好些


Java code

import java.awt.event.*; 
import javax.swing.*; 
import java.awt.*; 
public class ComponentEvents implements ActionListener { 

    JFrame jf = new JFrame();
    JButton b1=new JButton("改变框架的主题."); 
    JButton b2=new JButton("还原框架的主题"); 
    JButton btnColor=new JButton("改变按钮的颜色"); 
    JButton btnRestore=new JButton("还原按钮颜色"); 
    //Graphics gra=new Graphics(); 
    Color color=btnColor.getBackground(); 
    public ComponentEvents () { 
        jf.setTitle("ComponentEvents");
        jf.setSize(600,300); 
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        b1.addActionListener(this); 
        b2.addActionListener(this); 
        btnColor.addActionListener(this); 
        btnRestore.addActionListener(this); 
        b1.setBounds(10,20,150,20); 
        b2.setBounds(160,20,150,20); 
        btnColor.setBounds(10,60,150,20); 
        btnRestore.setBounds(160,60,150,20); 
        JPanel pane=new JPanel(); 
        pane.setLayout(null); 
        pane.add(b1); 
        pane.add(b2); 
        pane.add(btnColor); 
        pane.add(btnRestore); 
        jf.add(pane); 
        jf.setVisible(true); 
    } 
    public void actionPerformed(ActionEvent evt) { 
        Object source=evt.getSource(); 
        if(source==b1) 
            jf.setTitle("dkf"); 
        else if(source==b2) 
            jf.setTitle("ComponentEvents"); 
        else if (source==btnColor) 
        { 
            btnColor.setBackground(Color.orange); 
        } 
        else if (source==btnRestore) 
        { 
            btnColor.setBackground(color); 
        } 
    } 
//    public void paint(Graphics g) 
//    { 
//        b1.setBounds(10,20,150,20); 
//        b2.setBounds(160,20,150,20); 
//        btnColor.setBounds(10,60,150,20); 
//        btnRestore.setBounds(160,60,150,20); 
//        g.drawLine(10,170,500,170); 
//        //repaint(); 
//    } 
    public static void main(String[] args) 
    { 
        ComponentEvents comevt= new ComponentEvents(); 
    } 
}
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: