我是Java的新手并且正在开发一个项目。作为制作我的主菜单的测试,我一直在试验JFrame和powerrangers的图片。我的问题是我已经尝试了很多次,并花了很多时间试图让我的游戏和我的帮助按钮在我的窗口左侧。我已经尝试制作一个4格的网格并放入一个空标签,但它不会起作用。 这是我的代码:
package be.kdg.AngryTanksKlad;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Main {
public static void main(String[] args) throws IOException {
final Image image = ImageIO.read(new URL("http://www.modernmom.com/sites/default/files/images/Power%2BRangers%2BPowerRangersMMPR.jpg"));
final JFrame frame = new JFrame();
frame.add(new ImagePanel(image));
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
@SuppressWarnings("serial")
class ImagePanel extends JPanel {
private Image image;
private boolean tile;
private JPanel menu = new JPanel(new FlowLayout(FlowLayout.LEFT));
ImagePanel(Image image) {
this.image = image;
this.tile = false;
final JButton play = new JButton("PLAY");
final JButton help = new JButton("HELP");
final JLabel empty = new JLabel();
}
menu.setLayout(new GridLayout(4,4));
menu.add(empty);
menu.add(play);
menu.add(help);
add(menu,BorderLayout.CENTER);
menu.setOpaque(false);
setVisible(true);
};
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (tile) {
int iw = image.getWidth(this);
int ih = image.getHeight(this);
if (iw > 0 && ih > 0) {
for (int x = 0; x < getWidth(); x += iw) {
for (int y = 0; y < getHeight(); y += ih) {
g.drawImage(image, x, y, iw, ih, this);
}
}
}
} else {
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
}
}
}
答案 0 :(得分:3)
这样的东西?
请参阅代码中的注释。
import java.awt.*;
import java.awt.image.*;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.*;
public class SillyNAmeForAMainClassGivenIHave100CalledMain {
public static void main(String[] args) throws IOException {
final Image image = new BufferedImage(400,200,BufferedImage.TYPE_INT_RGB);
final JFrame frame = new JFrame();
frame.add(new ImagePanel(image));
//frame.setSize(800, 600);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
@SuppressWarnings("serial")
class ImagePanel extends JPanel {
private Image image;
private boolean tile;
// add 5px spacing between buttons
private JPanel menu = new JPanel(new GridLayout(4,4,5,5));
ImagePanel(Image image) {
super(new BorderLayout()); // default is FlowLayout!
this.image = image;
this.tile = false;
final JButton play = new JButton("PLAY");
final JButton help = new JButton("HELP");
menu.add(play);
menu.add(help);
JPanel buttonConstrain = new JPanel(new FlowLayout());
// pad the top of it..
buttonConstrain.setBorder(new EmptyBorder(20,5,5,5));
buttonConstrain.setOpaque(false);
buttonConstrain.add(menu);
// it won't help to use a BorderLayout contraint when the default is FlowLayout!
// LINE_START = 'left' for left-to-right locales, right for the other
add(buttonConstrain,BorderLayout.LINE_START);
menu.setOpaque(false);
setVisible(true);
}
@Override
public Dimension getPreferredSize() {
Dimension d = null;
if (image != null) {
d = new Dimension(image.getWidth(this), image.getHeight(this));
} else {
d = new Dimension(600,400);
}
return d;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (tile) {
int iw = image.getWidth(this);
int ih = image.getHeight(this);
if (iw > 0 && ih > 0) {
for (int x = 0; x < getWidth(); x += iw) {
for (int y = 0; y < getHeight(); y += ih) {
g.drawImage(image, x, y, iw, ih, this);
}
}
}
} else {
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
}
}
}
答案 1 :(得分:2)
“我的问题是我已经尝试了很多次,花了很多时间试图将我的游戏和我的帮助按钮放在窗口的左边。”
JPanel
ImagePanel
的布局设置为BorderLayout
。JPanel
添加到BorderLayout.WEST
ImagePanel
setOpaque(false)
到新JPanel
JPanel panel = new JPanel();
panel.setOpaque(false);
panel.add(menu);
setLayout(new BorderLayout());
add(panel, BorderLayout.WEST);
工作正常
import java.awt.*;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Main {
public static void main(String[] args) throws IOException {
final Image image = ImageIO.read(new URL("http://www.modernmom.com/sites/default/files/images/Power%2BRangers%2BPowerRangersMMPR.jpg"));
final JFrame frame = new JFrame();
frame.add(new ImagePanel(image));
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
@SuppressWarnings("serial")
class ImagePanel extends JPanel {
private Image image;
private boolean tile;
private JPanel menu = new JPanel(new FlowLayout(FlowLayout.LEFT));
ImagePanel(Image image) {
this.image = image;
this.tile = false;
final JButton play = new JButton("PLAY");
final JButton help = new JButton("HELP");
final JLabel empty = new JLabel();
menu.setLayout(
new GridLayout(4, 4));
menu.add(empty);
menu.add(play);
menu.add(help);
JPanel panel = new JPanel();
panel.setOpaque(false);
panel.add(menu);
setLayout(new BorderLayout());
add(panel, BorderLayout.WEST);
menu.setOpaque(false);
setVisible(true);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (tile) {
int iw = image.getWidth(this);
int ih = image.getHeight(this);
if (iw > 0 && ih > 0) {
for (int x = 0; x < getWidth(); x += iw) {
for (int y = 0; y < getHeight(); y += ih) {
g.drawImage(image, x, y, iw, ih, this);
}
}
}
} else {
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
}
}
}