
import javax.swing.JButton
import javax.swing.JFrame
public class c {
public static void main(String[] args) {
// 你懂的
JFrame jf = new JFrame()
// 你懂的
jf.setSize(300, 400)
// JFrame做为一个画板,他里面可以有控件,有控件就有控件布局的样式,样式为null可以理解为自由布局
jf.setLayout(null)
// JFrame的setVisible的默认值为false,值的意义是是否显示.
jf.setVisible(true)
// 按钮
JButton jb = new JButton("按钮")
// 添加按钮
jf.add(jb)
// 因为上面的布局样式,他设置了x50y80坐标和长100宽30
jb.setBounds(50, 80, 100, 30)
}
}
用Java创建按钮组的程序如下:
import java.awt.GridLayoutimport java.awt.event.ActionEvent
import java.awt.event.ActionListener
import javax.swing.JButton
import javax.swing.JFrame
public class A extends JFrame implements ActionListener{
JButton[] b=new JButton[10]
A(){
setLayout(new GridLayout(3,4,5,5))
for(int i=0i<10i++){
b[i]=new JButton(String.valueOf(i))
b[i].addActionListener(this)
add(b[i])
}
setSize(300,300)
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
setVisible(true)
}
public static void main(String[] args) {
new A()
}
public void actionPerformed(ActionEvent ae) {
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)