我有一個JFrame
其中有2 JPanel
其中:a PaintPanel
(與paint()
方法)和ButtonPanel
(帶按鈕)。當我調用PaintPanel
的repaint()
(但單擊按鈕)ButtonPanel
的按鈕正在繪製在PaintPanel
!它不是可點擊或任何東西,它只是在那裏。重繪時JButton被複制?
我試圖重新使用這段代碼的問題:
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("frame");
frame.setSize(400,400);
frame.setLayout(new GridLayout(2,1));
PaintPanel paint = new PaintPanel();
ButtonPanel buttons = new ButtonPanel(paint);
frame.add(paint);
frame.add(buttons);
frame.setVisible(true);
}
}
public class PaintPanel extends JPanel{
public void paint(Graphics g){
g.drawRect(10, 10, 10, 10);
}
}
public class ButtonPanel extends JPanel implements ActionListener{
private PaintPanel paintPanel;
public ButtonPanel(PaintPanel paintPanel){
this.paintPanel=paintPanel;
JButton button = new JButton("button");
button.addActionListener(this);
add(button);
}
@Override
public void actionPerformed(ActionEvent arg0) {
paintPanel.repaint();
}
}
這重新前人的精力我有問題(對不起,奇碼標記,似乎無法得到它的權利)。
我真的希望你一個人知道這裏發生了什麼,因爲我不...
這種渲染物體往往不能兌現[透明度]出現( http://java.sun.com/products/jfc/tsc/articles/painting/index.html#props)屬性。此外,「Swing程序應該重寫'paintComponent()',而不是重寫'paint()'。」 - [* AWT和Swing中繪製:繪製方法*](http://java.sun.com/products/jfc /tsc/articles/painting/index.html#callbacks)。 – trashgod