java初学者 求教怎么在窗口中添加一张图片
packagejava525;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclas...
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;
public MainWindow() {
createMenu();
setTitle("学生管理系统"); //设置窗口标题
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);
}
想在 "学生管理系统"这个大窗口下添加一张图片 展开
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;
public MainWindow() {
createMenu();
setTitle("学生管理系统"); //设置窗口标题
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);
}
想在 "学生管理系统"这个大窗口下添加一张图片 展开
2个回答
展开全部
//注意:
//当前目录下要有一个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();
setTitle("学生管理系统"); //设置窗口标题
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);
}
}
//当前目录下要有一个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();
setTitle("学生管理系统"); //设置窗口标题
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 javax.imageio.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
/**
* 格子绘图演示
* @author hardneedl
*/
final class P extends JFrame{
private static final Dimension SIZE = new Dimension(600,400);
public Dimension getMinimumSize() {return SIZE;}
public Dimension getMaximumSize() {return SIZE;}
public Dimension getPreferredSize() {return SIZE;}
public String getTitle() {return "画笔绘图示意";}
private JComponent canvas;
private P() throws HeadlessException {
super();
init();
addListeners();
doLay();
}
private void init() {
canvas = new MyCanvas() {
private Brush b;
Brush getBrush() {
try {
return b == null ?
b = new PictureBrush(ImageIO.read(new File(System.getProperty("app.picture"))))
:
b;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
};
}
private void doLay(){
getContentPane().add(canvas, BorderLayout.CENTER);
pack();
setVisible(true);
}
private void addListeners(){
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String... args) {
SwingUtilities.invokeLater(P::new);
}
/*抽象画布*/
abstract private static class MyCanvas extends JComponent {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
getBrush().paint(g);
}
abstract Brush getBrush();
}
/*画笔*/
private interface Brush {
void paint(Graphics g);
}
/*图片画笔*/
private static class PictureBrush implements Brush, Observer {
private Image img;
private PictureBrush(Image img) {
this.img = img;
}
public void paint(Graphics g) {
if (img == null) return;
Graphics g2 = g.create();
g2.drawImage(img, 0, 0, null);
g2.dispose();
}
public void update(Observable o, Object arg) {}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询