java中的按钮JButton可否自己调整大小,用什么方法?
1个回答
展开全部
当然可以!
直接上代码:
package com.laozhao.test;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* @author dozhao
* @version create time:Feb 14, 2012 3:45:06 PM
*
*/
public class TestButton {
/**
* @param args
*/
public static void main(String[] args) {
JFrame frame = new JFrame("test button");
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(null);
final JButton button = new JButton("click me bigger");
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
int width = button.getSize().width;
int height = button.getSize().height;
button.setSize(width + 4, height + 4);
}
});
button.setSize(50, 30);
buttonPanel.setSize(400,500);
buttonPanel.add(button);
frame.getContentPane().add(buttonPanel);
frame.pack();
frame.setSize(400,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
直接上代码:
package com.laozhao.test;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* @author dozhao
* @version create time:Feb 14, 2012 3:45:06 PM
*
*/
public class TestButton {
/**
* @param args
*/
public static void main(String[] args) {
JFrame frame = new JFrame("test button");
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(null);
final JButton button = new JButton("click me bigger");
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
int width = button.getSize().width;
int height = button.getSize().height;
button.setSize(width + 4, height + 4);
}
});
button.setSize(50, 30);
buttonPanel.setSize(400,500);
buttonPanel.add(button);
frame.getContentPane().add(buttonPanel);
frame.pack();
frame.setSize(400,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询