package com.mstanford;
import java.awt.FlowLayout;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class MainFrame extends JFrame {
public JButton btn1,btn2,btn3,btn4,btn5;
public MainFrame() {
this.setTitle("这是一个swing窗体");
this.setBounds(200, 100, 800, 500);
this.setVisible(true);
this.setResizable(false);
// 实例化标签
JLabel lblPwd = new JLabel();
// 增加图片
ImageIcon imgIcon = new ImageIcon("E:/images/imagen8.jpg");
JLabel lblImg = new JLabel(imgIcon);
this.getContentPane().add(lblImg);
// 创建表格
// String columnNames[]={"会员管理","姓名","电话","城市"};
// DefaultTableModel model=new DefaultTableModel(columnNames,0);
// JTable Tb = new JTable(model);
//流动布局
this.setLayout(new FlowLayout(FlowLayout.LEFT));
// 实例化按钮
JButton btn1 = new JButton("按钮1");
JButton btn2 = new JButton("按钮2");
JButton btn3 = new JButton("按钮3");
JButton btn4 = new JButton("按钮4");
JButton btn5 = new JButton("按钮5");
// 添加面板
this.getContentPane().add(btn1);
this.getContentPane().add(btn2);
this.getContentPane().add(btn3);
this.getContentPane().add(btn4);
this.getContentPane().add(btn5);
}
public static void main(String[] args) {
new MainFrame();
}
}怎样将图片设置为背景并且全覆盖窗体,还有按钮是怎样定位啊!请大神们指导
1 回答
奔跑的虫子
TA贡献33条经验 获得超21个赞
添加背景图片 可以重绘JPanel的paint方法:如下 将你的Jlabel换成下面的类 class MyPanel extends JPanel{ private Image img=null; public MyPanel(){ img=Toolkit.getDefaultToolkit().getImage("E:/images/imagen8.jpg"); } public void paint(Graphics g){ super.paint(g); g.drawImage(img,0,0,800,500,this); } } 按钮的布局可以使用绝对布局; this.setLayout(null); JButton btn1 = new JButton("按钮1"); btn1.setBounds(100, 100, 100, 60); //x,y,width,height
添加回答
举报
0/150
提交
取消