您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
【第18章:图形界面】_按钮组件:JButton
发布时间:2021-01-14 18:12:53编辑:雪饮阅读()
用Jbutton实现一个按钮组件
import java.awt.Dimension ;
import javax.swing.JFrame ;
import javax.swing.JButton ;
public class Hello{
public static void main(String args[]) throws Exception{
JFrame frame = new JFrame("Welcome To kasumi") ;
JButton but = new JButton("click me") ;
frame.add(but) ;
frame.setSize(200,70) ;
frame.setLocation(300,200) ;
frame.setVisible(true) ;
}
};
JButton按钮组件也可以用Icon实现背景图片
import java.awt.Dimension ;
import javax.swing.JFrame ;
import javax.swing.JButton ;
import javax.swing.Icon ;
import javax.swing.ImageIcon ;
import java.io.File ;
public class Hello{
public static void main(String args[]) throws Exception{
JFrame frame = new JFrame("Welcome To kasumi") ;
String picPath = "d:" + File.separator + "kasumi.png" ;
Icon icon = new ImageIcon(picPath) ;
JButton but = new JButton(icon) ;
frame.add(but) ;
frame.setSize(300,160) ;
frame.setLocation(300,200) ;
frame.setVisible(true) ;
}
};
不过这样看起来比较尬,要在项目中使用的时候进行细调
关键字词:java,JButton