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();
}
}
我想问下,当程序运行时,窗口有四个按钮与一条直线,但当最小化时,再还原,四个按钮消失.请问大家这是什么问题.
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();
}
}
我想问下,当程序运行时,窗口有四个按钮与一条直线,但当最小化时,再还原,四个按钮消失.请问大家这是什么问题.
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();
}
}
JButton 等是 swing 软件包里定义的组件。它们都是轻型的(lightweight). paint 方法是 awt软件包定义的方法。awt 软件包定义的组件, 都是重型的 (heavyweight)。两者画在一起要"打架"。 结果,重型的产品掩盖轻型的产品而显示出来。
解决方法:分别部署,就是说,
将轻型的 swing 包的组件(四个按钮),另外装到一个容器内, 则会永远显示。