java JButton显示问题
在窗口中增加两个button,和一个JComponent。一个是Button,另一个是JButton.JComponent的唯一功能是给JComponent填满白色。一开...
在窗口中增加两个button,和一个JComponent。 一个是Button, 另一个是JButton.JComponent 的唯一功能是给JComponent 填满白色。一开始两个button 是可显示的,JComponent 是不可显示的。当点击任何一个按钮后,让JComponent 显示,从而遮挡住这两个按钮。现在的问题是,Button 可以被挡住,但是JButton 总是会显示,这是问什么啊。
代码如下
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainFrame extends JFrame {
OverLayer overLayer;
public static void main(String [] argv){
MainFrame mainFrame = new MainFrame();
}
public MainFrame(){
JButton button1 = new JButton("click1");
Button button2 = new Button("click2");
overLayer = new OverLayer();
overLayer.setVisible(false);
this.setPreferredSize(new Dimension(400,400));
this.setLayout(null);
this.add(overLayer);
this.add(button1);
this.add(button2);
pack();
overLayer.setBounds(0, 0, getWidth(), getHeight());
button1.setBounds(20,100,80,40);
button2.setBounds(20,150,80,40);
this.setVisible(true);
button1.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
overLayer.setVisible(true);
}
});
button2.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
overLayer.setVisible(true);
}
});
}
}
class OverLayer extends JComponent{
public void paint(Graphics g){
g.setColor(Color.white);
g.fillRect(0, 0, getWidth(), getWidth());
}
} 展开
代码如下
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainFrame extends JFrame {
OverLayer overLayer;
public static void main(String [] argv){
MainFrame mainFrame = new MainFrame();
}
public MainFrame(){
JButton button1 = new JButton("click1");
Button button2 = new Button("click2");
overLayer = new OverLayer();
overLayer.setVisible(false);
this.setPreferredSize(new Dimension(400,400));
this.setLayout(null);
this.add(overLayer);
this.add(button1);
this.add(button2);
pack();
overLayer.setBounds(0, 0, getWidth(), getHeight());
button1.setBounds(20,100,80,40);
button2.setBounds(20,150,80,40);
this.setVisible(true);
button1.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
overLayer.setVisible(true);
}
});
button2.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
overLayer.setVisible(true);
}
});
}
}
class OverLayer extends JComponent{
public void paint(Graphics g){
g.setColor(Color.white);
g.fillRect(0, 0, getWidth(), getWidth());
}
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询