我试图让按钮一旦点击就改变它的位置。我已经读过,为了设置我的组件'手动定位。因此我将Frame的默认布局管理器声明为null。但是我的按钮保持不变,尽管调试器告诉我每次单击按钮时我都会获得新的(x,y)。这是我的源代码:
类GameUI.java:
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import de.tk1.g4.ta1.model.Player;
public class GameUI extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private String header[] = new String[]{"Player", "Score"};
private JTable table = new JTable();
private DefaultTableModel model = (DefaultTableModel) table.getModel();
private JPanel sidePanel = new JPanel();
private ImagePanel bodyPanel = new ImagePanel();
public GameUI() {
model.setColumnIdentifiers(header);
table.setEnabled(false);
getContentPane().setLayout(null);
getContentPane().add(sidePanel);
getContentPane().add(bodyPanel);
sidePanel.setLayout(new BoxLayout(sidePanel, BoxLayout.Y_AXIS));
sidePanel.add(table);
sidePanel.add(Box.createHorizontalStrut(10));
sidePanel.setBounds(0, 0, 300, 500);
bodyPanel.setBounds(300, 0, 700, 500);
setMaximumSize(new Dimension(700 + 300, 500));
setPreferredSize(new Dimension(700 + 300, 500));
setResizable(false);
super.paintComponents(getGraphics());
this.pack();
}
public void addPlayerToList (Player thePlayer) {
model.addRow(new Object[]{thePlayer.getName(), thePlayer.getScore()});
super.paintComponents(getGraphics());
}
public void clearPlayersTable() {
while(model.getRowCount() > 0)
{
model.removeRow(0);
}
}
public void setPokemonLocation (int x, int y) {
bodyPanel.getBtnPokemon().setLocation(x, y);
//repaint();
}
public JButton getPokButton () {
return bodyPanel.getBtnPokemon();
}
public ImagePanel getBodyPanel() {
return bodyPanel;
}
public void setBodyPanel(ImagePanel bodyPanel) {
this.bodyPanel = bodyPanel;
}
public void setImage(String path) throws IOException {
try {
FileInputStream fis = new FileInputStream(new File(path));
Image img = ImageIO.read(fis);
bodyPanel.getBtnPokemon().setIcon(new ImageIcon(img));
}catch(IOException e){
e.printStackTrace();
System.out.println(path);
}
}
public void setPokButton(int x,int y,String path) {
bodyPanel.setImage(path);
bodyPanel.getBtnPokemon().setLocation(x, y);
}
public void addPokemonListener(ActionListener listenForPokemon) {
bodyPanel.getBtnPokemon().addActionListener(listenForPokemon);
}
@Override
public void actionPerformed(ActionEvent e) {
super.paintComponents(getGraphics());
}
}
Class ImagePanel.java:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
public class ImagePanel extends JPanel implements ActionListener{
private static final long serialVersionUID = 1L;
private JButton btnPokemon = new JButton("");
private BufferedImage image;
public ImagePanel() {
super();
this.setBorder(BorderFactory.createTitledBorder(""));
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
this.setMaximumSize(new Dimension(700, 450));
this.setMinimumSize(new Dimension(700, 450));
this.add(btnPokemon);
}
@Override
public void paintComponent(Graphics g) {
if (g != null && image != null) {
super.paintComponent(g);
g.drawImage(image, btnPokemon.getX(), btnPokemon.getY(), null);
}
}
@Override
public void actionPerformed(ActionEvent e) {
super.paintComponents(getGraphics());
}
public void setImage(String path) {
try {
this.image = ImageIO.read(new File(path));
btnPokemon.setIcon(new ImageIcon(image));
} catch (IOException e) {
e.printStackTrace();
}
}
public JButton getBtnPokemon() {
return btnPokemon;
}
public void setBtnPokemon(JButton btnPokemon) {
this.btnPokemon = btnPokemon;
}
public BufferedImage getImage() {
return image;
}
public void setImage(BufferedImage image) {
this.image = image;
}
}
虽然我的控制器拨打了this.gameUI.revalidate();
和this.gameUI.repaint();
。
请在我的源代码中找到任何提示。