我有2个JPanels
,每个都有自己的类Jpanel
,每个类都在jDialog
中。我在其中一个类中放置了一个click事件,然后在发生单击的位置获取颜色。现在我怎么能告诉第二个JPanel
重绘自己?
我的jDialog中的构造函数:
public ColorPickerDialog(java.awt.Frame parent, boolean modal){
super(parent, modal);
initComponents();
cpp = new ColorPicker();
cpp.setBounds(5, 5, 300, 300);
cpp.setVisible(true);
cpp.setOpaque(true);
cpp.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
add(cpp);
// Click event is in this class:
ColorSlider colorSlide = new ColorSlider();
colorSlide.setBounds(315, 5, 20, 300);
colorSlide.setVisible(true);
colorSlide.setOpaque(true);
colorSlide.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
add(colorSlide);
}
我的点击事件:
public void mouseClick(MouseEvent evt){
Point pt = evt.getPoint();
BufferedImage img = (BufferedImage)this.createImage(getWidth(), getHeight());
int[] colors = new int[3];
img.getRaster().getPixel(evt.getX(), evt.getY(), colors);
ColorPickerDialog.sldColor = new Color(colors[0], colors[1], colors[2]);
}
答案 0 :(得分:2)
为您的项目使用工厂模式。创建或启动第二个JPanel
时,将其添加到包含对象实例的Factory。进入mouseClick
,从工厂调用您的第二个面板并实施:panel.repaint();
一些例子:
public void mouseClick(MouseEvent evt){
....
SecondPanel panel = PanelFactory.loadPanel(2);
panel.revalidate();
panel.repaint();
....
}
public class PanelFactory{
private static PanelFactory instance = new PanelFactory();
private JPanel mSomePanel = null;
public static void storePanel(JPanel panel){
instance.mSomePanel = panel;
}
public static JPanel loadPanel(int index){
......
return instance.mSomePanel;
}
}
答案 1 :(得分:2)
其中一个JPanel可能是Observer,另一个可能是Observable。然后,当您需要更新时,只需致电:
setChanged();
notifyObservers();
你的Observable JPanel中的。