pro*_*mer 3 java swing image jframe
我是Java新手.我只是想加载图像作为背景JFrame.我想要做的是从C驱动器(这不是我的工作区)获取图像所以我做了什么Board.java:
ImageIcon i = new ImageIcon("C:/image.png");
img =i.getImage();
Run Code Online (Sandbox Code Playgroud)
并尝试绘制这样的东西:
public void paint(Graphics g )
{
super.paint(g);
Graphics2D g2d= (Graphics2D) g;
g2d.drawImage(img, 0, 100, null);
}
Run Code Online (Sandbox Code Playgroud)
然后我就像这样在我的主要课堂上打电话
public static void main(String[] args)
{
JFrame frame= new JFrame(" Game") ;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1200, 365);
frame.setVisible(true);
frame.add(new Board());
}
Run Code Online (Sandbox Code Playgroud)
但我没有显示任何图像,这是合法的添加方式Image吗?
paint()在JFramesetSize()上JFrame,而使用JFrame#pack()它设置可见之前这是我做的一个例子:

JPanel/ JLabel实例paintComponent(..)在JPanel/JLabelgetPreferredSize()以返回尺寸正确的尺寸/组件ImageJPanel/ Jlabel到JFrame实例JFrame通过JFrame#pack()JFrame可见Test.java:
//necessary imports
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
static String filename = "c:/test.jpg";//your file path and name here use / as it will work on linux platforms too so get into the habbit
/**
* Default constructor
*/
public Test() throws Exception {
initComponents();
}
/**
* Initialize GUI and components (including ActionListeners etc)
*/
private void initComponents() throws Exception {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final Image background = ImageIO.read(new File(filename));
final Dimension jpanelDimensions = new Dimension(new ImageIcon(background).getIconWidth(), new ImageIcon(background).getIconHeight());
frame.add(new JPanel() {
@Override
protected void paintComponent(Graphics grphcs) {
super.paintComponent(grphcs);
grphcs.drawImage(background, 0, 0, null);
}
//return a JPanel that matches images size
@Override
public Dimension getPreferredSize() {
return jpanelDimensions;
}
});
frame.setResizable(false);
//pack frame (size JFrame to match preferred sizes of added components and set visible
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
/**
* Create GUI and components on Event-Dispatch-Thread
*/
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
//set nimbus look and feel
for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
try {
//create GUI instance
Test test = new Test();
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2388 次 |
| 最近记录: |