用Java编写一个应用程序窗体上有3个按钮3个标签采用GridLayout方式放置参考界面如下:需要标题注意:既有文字又有图片的标签构造函数为:JLabel字符串图片JLabelLEFT 最后一个参数可以是其它对其方式。
import javax.swing.; import java.awt.;
public class MyApplication extends JFrame { public MyApplication() { setTitle("My Application");
// 创建3个标签
JLabel label1 = new JLabel("Label 1");
JLabel label2 = new JLabel("Label 2");
JLabel label3 = new JLabel("Label 3");
// 创建3个按钮
ImageIcon icon1 = new ImageIcon("button1.png");
JButton button1 = new JButton("Button 1", icon1);
ImageIcon icon2 = new ImageIcon("button2.png");
JButton button2 = new JButton("Button 2", icon2);
ImageIcon icon3 = new ImageIcon("button3.png");
JButton button3 = new JButton("Button 3", icon3);
// 使用GridLayout布局方式
setLayout(new GridLayout(2, 3));
// 添加标签和按钮到窗体
add(label1);
add(label2);
add(label3);
add(button1);
add(button2);
add(button3);
// 设置窗体大小、位置和关闭方式
setSize(500, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
MyApplication app = new MyApplication();
app.setVisible(true);
}
原文地址: https://www.cveoy.top/t/topic/hsP9 著作权归作者所有。请勿转载和采集!