当前位置: 代码迷 >> J2SE >> pain方法的使用解决思路
  详细解决方案

pain方法的使用解决思路

热度:316   发布时间:2016-04-24 18:12:43.0
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();     } }