我正在尝试创建一个JFrame,其中包含一个JMenubar和一个带有背景图像的JPanel。到目前为止,我已经创建了两个窗口,一个JFrame和一个JPanel,JFrame有JMenuBar,JPanel有背景,我可以把它们放在一个窗口中吗?
package RPGPackage;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.JMenuBar;
public class RPG extends JFrame {
public RPG() {
JFrame frame = new JFrame("RPG");
frame.setVisible(true);
frame.setSize(1005,710);
frame.setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
frame.add(panel);
setLocation(0,0);
setVisible(true);
setSize(1005,710);
setResizable(false);
JMenuBar menubar = new JMenuBar();
frame.setJMenuBar(menubar);
JMenu file = new JMenu("File");
file.setMnemonic(KeyEvent.VK_A);
menubar.add(file);
JMenuItem Load = new JMenuItem("Load",KeyEvent.VK_L);
file.add(Load);
JMenuItem Save = new JMenuItem("Save",KeyEvent.VK_S);
file.add(Save);
JMenuItem exit = new JMenuItem(new AbstractAction("Quit"){//Adds Exit button
public void actionPerformed(ActionEvent e) {//adds an action to the JMenuItem "Exit"
System.exit(0);}//when action is performed program will exit
});
file.add(exit);//adds the JMenuitem to the JMenuBar thats above.
Background background = new Background();
add(background);
}
public static void main(String[] args) {
RPG window = new RPG();
}
}
package RPGPackage;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Background extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
ImageIcon background = new ImageIcon("Image.png");
background.paintIcon(this, g, 0, 0);
}
}
1)您的RPG
类扩展了JFrame
,您不需要创建帧的另一个实例
(JFrame frame = new JFrame("RPG");
)。使用您的RPG
实例。
2) 使用BufferedImage
而不是ImageIcon
,并绘制如下图像:
private BufferedImage img = ImageIO.read(getClass().getResourceAsStream("Image.png"));
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(img, 0, 0, this);
}
3) 在接口构建结束时调用setVisible(true);
或类似的下一个:
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
setVisible(true);
}
});
4) setResizable(false);这是不好的做法。借助LayoutManager
管理组件大小调整。
5) 使用pack();
方法而不是setSize(...)
我已经修复了你的代码,检查一下:
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
public class RPG extends JFrame {
public RPG() {
super("RPG");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
JMenuBar menubar = new JMenuBar();
setJMenuBar(menubar);
JMenu file = new JMenu("File");
file.setMnemonic(KeyEvent.VK_A);
menubar.add(file);
JMenuItem Load = new JMenuItem("Load", KeyEvent.VK_L);
file.add(Load);
JMenuItem Save = new JMenuItem("Save", KeyEvent.VK_S);
file.add(Save);
JMenuItem exit = new JMenuItem(new AbstractAction("Quit") {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
file.add(exit);
Background background = new Background();
add(background);
setSize(200,200);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
RPG window = new RPG();
}
class Background extends JPanel {
private BufferedImage img;
public Background() {
try {
img = ImageIO.read(getClass().getResourceAsStream("Image.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(img, 0, 0, this);
}
}
}
此外,你可以使用JLabel
来保持和绘制图像,而不是绘制图像,它可以帮助你从盒子中获得一些功能:
class Background extends JPanel {
private ImageIcon img;
public Background() {
URL resource = getClass().getResource("Image.jpg");
img = new ImageIcon(resource);
JLabel l = new JLabel(img);
add(l);
}
}