我想知道為什么 repiant() 方法沒有按預期作業。我的代碼:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
public class RightPanel extends JPanel implements ActionListener{
JButton buttono;
JButton buttonu;
MyFrame frame;
ButtomPanel s;
public RightPanel(MyFrame frame){
super();
this.frame=frame;
s= new ButtomPanel(frame);
this.setPreferredSize(new Dimension((frame.getWidth()/3),frame.getHeight()));
setBackground(Color.green);
setLayout(new BorderLayout());
buttono = new JButton ("up");
buttonu = new JButton ("down");
buttono .addActionListener(this);
buttonu .addActionListener(this);
add(buttono , BorderLayout.NORTH);
add(buttonu , BorderLayout.SOUTH);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource()==buttono) {
System.out.println("Up");
s.x=s.x 10;
s.repaint();
}
}
我想重繪以下課程:
import java.awt.Color;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class ButtomPanel extends JPanel {
MyFrame frame;
BufferedImage image;
boolean geklickt=false;
public static int x=0;;
public ButtomPanel(MyFrame frame) {
super();
this.frame=frame;
this.setPreferredSize(new Dimension(((frame.getWidth()/3)*2),585));
setBackground(Color.blue);
java.net.URL resource = getClass().getResource("/resources/siegel.jpg");
try {
image = ImageIO.read(resource);
} catch (IOException e) {
e.printStackTrace();
}
setVisible(true);
}
public void paint(Graphics g) {
super.paint(g);
g.drawImage(image, 150 x, 150 x, 150, 150, null);}
}
}
我想用 RightPanel 類中的 ActionListener 更改 BottomPanel 類中的 x,然后重新繪制它,但它不起作用。我可以提高 x,但 s.repaint() 不呼叫paint-Method。
uj5u.com熱心網友回復:
s永遠不會添加到任何東西中,因此永遠不會被繪制。
this.setPreferredSize(new Dimension((frame.getWidth()/3),frame.getHeight()));是一個非常糟糕的主意。視窗應該符合組件的大小,而不是相反。事實上,以MyFrame這種方式向組件公開從來沒有真正好的理由,它為這些組件提供了MyFrame它們不應該擁有的控制權。
java.net.URL resource = getClass().getResource("/resources/siegel.jpg");
try {
image = ImageIO.read(resource);
} catch (IOException e) {
e.printStackTrace();
}
是個壞主意。該類應該向使用它的人拋出例外,以便他們知道出了什么問題。這樣做不僅會以難以追蹤的方式消耗錯誤,而且還會讓您NullPointerException在呼叫時做好準備g.drawImage(image, 150 x, 150 x, 150, 150, null)
不要覆寫paint
public void paint(Graphics g) {
super.paint(g);
g.drawImage(image, 150 x, 150 x, 150, 150, null);
}
paintComponent而是 更喜歡。paint實際上是一個非常復雜的方法。有關更多詳細資訊,請參閱在 AWT 和 Swing 中繪畫和執行自定義繪畫
public static int x=0;出于三個原因,這是一個壞主意:
static不是你的朋友。當你有多個實體時會發生什么ButtomPanel?一般來說,當你static以這種方式使用時,它是一個危險信號,告訴你你的設計是錯誤的。public正在提供對財產的不受控制的訪問。通常不鼓勵這樣做,您可能更喜歡使用 setter 和 getter 與屬性互動。JPanel已經有一個x屬性,這可能會讓人感到困惑(即,如果有人使用getX而不是x)。最好將其重命名為 of 之類imageX的imageXOffset。
可運行的示例...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public final class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
JFrame frame = new JFrame();
frame.add(new RightPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
public class RightPanel extends JPanel implements ActionListener {
JButton buttono;
JButton buttonu;
ButtomPanel s;
public RightPanel() throws IOException {
super();
s = new ButtomPanel();
setBackground(Color.green);
setLayout(new BorderLayout());
buttono = new JButton("up");
buttonu = new JButton("down");
buttono.addActionListener(this);
buttonu.addActionListener(this);
add(buttono, BorderLayout.NORTH);
add(buttonu, BorderLayout.SOUTH);
add(s);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == buttono) {
System.out.println("Up");
s.addToImageXOffset(10);
}
}
}
public class ButtomPanel extends JPanel {
private BufferedImage image;
private int imageXOffset = 0;
public ButtomPanel() throws IOException {
super();
setBackground(Color.blue);
image = ImageIO.read(getClass().getResource("/images/Heart.png"));
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
public void addToImageXOffset(int delta) {
setImageXOffset(getImageXOffset() delta);
}
public void setImageXOffset(int imageXOffset) {
this.imageXOffset = imageXOffset;
repaint();
}
public int getImageXOffset() {
return imageXOffset;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g); //To change body of generated methods, choose Tools | Templates.
g.drawImage(image, 150 imageXOffset, 150 imageXOffset, this);
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/468340.html
