【发布时间】:2020-06-15 09:17:23
【问题描述】:
我正在尝试调整尺寸为 100 x 100 的图像,但我不明白我做错了什么。我把代码放在那里调整它的大小,但它不会正确编译。部分任务是照原样拍摄图像并使用代码调整其大小。我以为我可以事先调整它的大小,然后在那里输入,但这是不允许的。请帮忙。
这是代码,这是图片BlackCat
package catmover;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.awt.Image
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SpringLayout;
public class MainPage extends JFrame implements ActionListener {
private static JFXPanel fxContainer;
private JButton up, down, left, right;
private JLabel catHolder;
private SpringLayout sl;
private JPanel jp;
public MainPage() {
this.jp = new JPanel();
this.sl = new SpringLayout();
}
void init() {
fxContainer = new JFXPanel();
add(fxContainer, BorderLayout.CENTER);
Platform.runLater(this::createScene);
}
private void createScene() {
try {
prepareCatHolder();
prepareButtons();
this.add(jp);
this.setVisible(true);
} catch (IOException ex) {
Logger.getLogger(MainPage.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void prepareCatHolder() throws IOException {
BufferedImage img = ImageIO.read(new File("Black_Cat.png"));
Image newImage = ("Black_cat.png").getScaledInstance(100, 100, Image.SCALE_DEFAULT);
ImageIcon icon = new ImageIcon(img);
catHolder = new JLabel(icon);
jp.setLayout(sl);
sl.putConstraint(SpringLayout.WEST, catHolder,
100,
SpringLayout.WEST, jp);
sl.putConstraint(SpringLayout.NORTH, catHolder,
100,
SpringLayout.NORTH, jp);
jp.add(catHolder);
}
private void prepareButtons() {
up = new JButton("UP");
up.setPreferredSize(new Dimension(100, 50));
up.addActionListener(this);
down = new JButton("DOWN");
down.setPreferredSize(new Dimension(100, 50));
down.addActionListener(this);
left = new JButton("LEFT");
left.setPreferredSize(new Dimension(100, 50));
left.addActionListener(this);
right = new JButton("RIGHT");
right.setPreferredSize(new Dimension(100, 50));
right.addActionListener(this);
sl.putConstraint(SpringLayout.WEST, up,
this.getWidth() - 300, SpringLayout.WEST, jp);
sl.putConstraint(SpringLayout.NORTH, up,
this.getHeight() - 300, SpringLayout.NORTH, jp);
sl.putConstraint(SpringLayout.WEST, left,
this.getWidth() - 450, SpringLayout.WEST, jp);
sl.putConstraint(SpringLayout.NORTH, left,
this.getHeight() - 200, SpringLayout.NORTH, jp);
sl.putConstraint(SpringLayout.WEST, right,
this.getWidth() - 150, SpringLayout.WEST, jp);
sl.putConstraint(SpringLayout.NORTH, right,
this.getHeight() - 200, SpringLayout.NORTH, jp);
sl.putConstraint(SpringLayout.WEST, down,
this.getWidth() - 300, SpringLayout.WEST, jp);
sl.putConstraint(SpringLayout.NORTH, down,
this.getHeight() - 100, SpringLayout.NORTH, jp);
jp.add(up);
jp.add(down);
jp.add(left);
jp.add(right);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(up)) {
jp.remove(catHolder);
sl.putConstraint(SpringLayout.NORTH, catHolder,
0,
SpringLayout.NORTH, jp);
sl.putConstraint(SpringLayout.WEST, catHolder,
(this.getWidth() / 2) - (catHolder.getWidth() / 2),
SpringLayout.WEST, jp);
jp.add(catHolder);
this.revalidate();
} else if (e.getSource().equals(down)) {
jp.remove(catHolder);
sl.putConstraint(SpringLayout.SOUTH, catHolder,
0,
SpringLayout.SOUTH, jp);
sl.putConstraint(SpringLayout.WEST, catHolder,
(this.getWidth() / 2) - (catHolder.getWidth() / 2),
SpringLayout.WEST, jp);
jp.add(catHolder);
this.revalidate();
} else if (e.getSource().equals(left)) {
jp.remove(catHolder);
sl.putConstraint(SpringLayout.WEST, catHolder,
0,
SpringLayout.WEST, jp);
sl.putConstraint(SpringLayout.NORTH, catHolder,
(this.getHeight() / 2) - (catHolder.getHeight() / 2),
SpringLayout.NORTH, jp);
jp.add(catHolder);
this.revalidate();
} else if (e.getSource().equals(right)) {
jp.remove(catHolder);
sl.putConstraint(SpringLayout.EAST, catHolder,
0,
SpringLayout.EAST, jp);
sl.putConstraint(SpringLayout.NORTH, catHolder,
(this.getHeight() / 2) - (catHolder.getHeight() / 2),
SpringLayout.NORTH, jp);
jp.add(catHolder);
this.revalidate();
}
}
}
【问题讨论】:
-
“无法正确编译” - 有什么错误?
标签: java javafx netbeans compiler-errors resize