//当前目录下要有一个p.jpg文件
//压缩这个文件使它的大小尽量小,最好只有几十KB
//否则无法显示背景图片
package java525
import java.awt.*
import java.awt.event.*
import javax.swing.*
public class MainWindow extends JFrame implements ActionListener {
JMenuBar menuBar//菜单条
JMenu menu1, menu2, menu3, menu4//菜单
JMenuItem m11, m12, m13//菜单项
JMenuItem m21, m22, m23
JMenuItem m31, m32, m33
JMenuItem m41, m42
Image pic
public static void main(String[] args) {
new MainWindow()
}
public MainWindow() {
pic=Toolkit.getDefaultToolkit().getImage("p.jpg")
MediaTracker t=new MediaTracker(this)
t.addImage(pic,1)
try
{
t.waitForID(1)
}
catch(Exception e)
{
}
//createMenu()
setIconImage((new ImageIcon("icon.gif")).getImage())//设置图标
setSize(600, 400)//设置窗口大小
Dimension size = Toolkit.getDefaultToolkit().getScreenSize()//取屏幕大小
setLocation((size.width - getWidth()) / 2, (size.height - getHeight()) / 2)
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)//设置关闭行为
setVisible(true)//使窗口可见
//this.setExtendedState(JFrame.MAXIMIZED_BOTH)
}
public void actionPerformed(ActionEvent e) {
}
public void paint(Graphics g) {
super.paint(g)
g.drawImage(pic,0,0,null)
}
}
import java.awt.Graphicsimport java.awt.Image
import javax.swing.ImageIcon
import javax.swing.JPanel
public class PictureJPanel extends JPanel{
private ImageIcon icon
private Image image
public PictureJPanel() {
icon = new ImageIcon(getClass().getResource("/background.jpg"))
image = icon.getImage()
}
public void paintComponent(Graphics g) {
super.paintComponent(g)
//下面这行是为了背景图片可以跟随窗口自行调整大小,可以自己设置成固定大小
g.drawImage(image, 0, 0,this.getWidth(), this.getHeight(), this)
}
}
你可以自己新建一个绘制的面板.
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)