用java怎么编写一个图形界面应用程序,其中包含一个按钮。当鼠标移到按钮上时,隐藏按钮
希望把代码写下,思路我也知道,就是不知道怎么写,可以的话最好用awt包下的....swing包下的弄得不是很清楚...
希望把代码写下,思路我也知道,就是不知道怎么写,可以的话最好用awt包下的....swing包下的弄得不是很清楚
展开
3个回答
展开全部
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import java.awt.Rectangle;
public class HideButton extends JFrame {
private JPanel jPanel = null;
private JButton jButton = null;
/**
* This method initializes
*
*/
public HideButton() {
super();
initialize();
}
/**
* This method initializes this
*
*/
private void initialize() {
this.setSize(new Dimension(239, 129));
this.setContentPane(getJPanel());
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
jPanel = new JPanel();
jPanel.setLayout(null);
jPanel.add(getJButton(), null);
}
return jPanel;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(72, 29, 109, 39));
jButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent e) {
jButton.setVisible(false);
}
public void mouseExited(java.awt.event.MouseEvent e) {
jButton.setVisible(true);
}
});
}
return jButton;
}
public static void main(String args[]){
new HideButton().setVisible(true);
}
}
import java.awt.Dimension;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import java.awt.Rectangle;
public class HideButton extends JFrame {
private JPanel jPanel = null;
private JButton jButton = null;
/**
* This method initializes
*
*/
public HideButton() {
super();
initialize();
}
/**
* This method initializes this
*
*/
private void initialize() {
this.setSize(new Dimension(239, 129));
this.setContentPane(getJPanel());
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
jPanel = new JPanel();
jPanel.setLayout(null);
jPanel.add(getJButton(), null);
}
return jPanel;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(72, 29, 109, 39));
jButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent e) {
jButton.setVisible(false);
}
public void mouseExited(java.awt.event.MouseEvent e) {
jButton.setVisible(true);
}
});
}
return jButton;
}
public static void main(String args[]){
new HideButton().setVisible(true);
}
}
展开全部
给Button添加一个MouseListener,并实现void mouseEntered(MouseEvent e)这个方法,在这个方法中调用Button的setVisible(false)方法就可以了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
拿netbeans里面的图形界面工具拖 增加一个事件就可以了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询