[問題] 請問有辦法把JPanel當參數傳嗎?
不好意思:
小的意思是這樣的... 因為書上/網路上看到的都是長這樣的
畫圖:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class InputDialog2
{
public static void main(String[] args)
{
InputDialog2Frame f=new InputDialog2Frame();
f.setVisible(true);
}
}
class InputDialog2Frame extends JFrame
{
InputDialog2Frame()
{
super( "InputDialog2 ");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocation(400,300);
setSize(300,200);
ButtonPane p=new ButtonPane();
Container c=getContentPane();
c.add(p);
}
}
class ButtonPane extends JPanel
{
String input= " ";
ButtonPane()
{
JButton click=new JButton( "click me ");
add(click);
click.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
input=JOptionPane.showInputDialog(this, "Please input something ");
repaint();
}
});
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawString(input, 50,100);
}
}
簡單來說 就是畫圖的method: paintComponent()
一定要放在繼承JPanel的class裡面 才能劃劃
------------------------------------------------------------
但是我想要把JPanel 當成參數傳給畫圖method 讓畫圖的method 在上面畫畫
OR
或是把JPanel 和 畫圖的method 拆開 我就是不能把畫圖的方法寫在JPanel裡面
,可是這樣一來,Graphics g一劃,就不知道要畫到哪去...
該怎麼好呢?
因為每個不同的class有不同的劃法阿! 我只能在適當時call用該劃此元件
的method阿...不能只有一個paintComponent阿...
好痛苦阿xd... 為什麼 Google 大家都寫得一樣 T-T
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.115.155.193
推
11/07 02:08, , 1F
11/07 02:08, 1F
推
11/07 09:24, , 2F
11/07 09:24, 2F
→
11/07 18:32, , 3F
11/07 18:32, 3F
→
11/07 18:34, , 4F
11/07 18:34, 4F
→
11/07 22:27, , 5F
11/07 22:27, 5F
推
11/08 16:36, , 6F
11/08 16:36, 6F
→
11/08 17:44, , 7F
11/08 17:44, 7F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 2 篇):