如何在没有获得重点的情况下重新绘制焦点对话框? [英] How to repaint out of focus dialog without gaining its focus?

查看:262
本文介绍了如何在没有获得重点的情况下重新绘制焦点对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一些菜单,它是更新同步变量(网格上的文本),然后失焦对话框必须重新绘制网格。这里是截图:





主控板始终处于最高位置,数据显示面板始终位于其后面。当按下前面板上的按钮时,数据显示器必须更新其网格。目前,网格上的公共变量0.4是通过添加监听器来更新的,并且工作正常。但是网格本身不再重绘。 如何实时重新绘制离焦对话框?



以下是前面板的代码:



pre $ public $ {code $}
DisplayForm dF = new DisplayForm();
....
public MainDisplayForm(){
initComponents();
Btn_IncreaseGain.addActionListener(new ButtonListener_IncreaseGain());
}
....
} // MainDisplayForm在这里结束。

类ButtonListener_IncreaseGain实现ActionListener {

DisplayForm dF = new DisplayForm();
Storage st = new Storage();

ButtonListener_IncreaseGain()
{

}

public void actionPerformed(ActionEvent e){

st .iGain = 20;

dF.revalidate();
dF.repaint();
System.out.println(Testing);
}
} //监听器在这里结束。

以下是数据显示的代码:

<$ public void paint(Graphics g)
{
g2 =(Graphics2D)g;
paintComponents(g2);

//增加的数字用于调整。
int x = this.jPanel1.getX()+ 8;
int y = this.jPanel1.getY()+ 30;
int width = this.jPanel1.getWidth()+ 19;
int height = this.jPanel1.getHeight()+ 40;


//贴标电压
label0.setText(st.zero);
label1.setText(st.v1);
label2.setText(st.v2);
label3.setText(st.v3);
label4.setText(st.v4);
label5.setText(st.v3);
label6.setText(st.v4);


g2.setColor(Color.darkGray);

for(int i = x; i {
g2.drawLine(i,y,i,height);
}

int j = 0;
for(int i = y; i {
j ++;
//st.iGain
g2.setColor(Color.orange);
if(j == 1)
{
double k1 = st.iGain * 0.4;
st.v1 = Double.toString(k1);

g2.drawString(st.v1,x + 5,y + 10);


if(j == 2)
{
double k2 = st.iGain * 0.3;
st.v2 = Double.toString(k2);

g2.drawString(st.v2,x + 5,y + 90);
}
g2.setColor(Color.DARK_GRAY);
g2.drawLine(x,i,width,i);
....

} //网格信息尚未完成。

谢谢,

解决方案

焦点不是问题,与当前的问题无关。解决的办法是通过更新setter方法所包含的字段来更改数据网格的属性,并调用对数据网格所保存的JComponent(可能是JPanel,或最终从JComponent派生的其他某个组件)的repaint。这个组件的paintComponent方法应该使用它的类字段来更新它所绘制的内容。

几乎不会绘制JComponent的paint方法,当然也不希望直接绘制到顶层窗口中。您也可能不想设置JLabels,JTextFields或任何其他JTextComponent的文本。来自paint / paintComponent。



我看不出为什么你的代码不工作,只能猜测你的问题的可能原因是代码没有显示。

编辑1:

只是猜测,但您可能有引用问题。我注意到你的监听器类创建了新的DisplayForm和Storage对象:

  DisplayForm dF = new DisplayForm(); 
Storage st = new Storage();

很有可能这些对象不是正在显示的对象,特别是在别处创建这些对象时并显示它们。再次,我只是猜测,因为我没有看到你的代码的其余部分,但也许你应该通过构造函数或设置方法参数将这些对象的引用传递到DisplayForm。

编辑2:

例如

this.dF = dF;
}

//同存储

主程序:

  public MainDisplayForm(){
initComponents();
ButtonListener_IncreaseGain btnListenerIncreaseGain = new ButtonListener_IncreaseGain();
btnListenerIncreaseGain.setDisplayForm(....);
btnListenerIncreaseGain.setStorage(....);
Btn_IncreaseGain.addActionListener(btnListenerIncreaseGain);
}


I made some menu and it is to update conmmon variables (for text on grid) then the out-of-focus dialog must repaint the grid. Here is the screenshot:

The main control panel is always at top position and 'Data Display' panel is always sitting behind it. When press a button on front panel, Data Display must update its grid. Currently, the common variable 0.4 on the grid is updated by adding listener and works fine. But the grid itself is not repainting anymore. How can I repaint the out-of-focus dialog in real time?

Here is the code of the front panel:

public class MainDisplayForm extends javax.swing.JFrame {

Storage st = new Storage();
DisplayForm dF = new DisplayForm();
....
public MainDisplayForm() {
    initComponents();
    Btn_IncreaseGain.addActionListener(new ButtonListener_IncreaseGain());
}
....
} //MainDisplayForm ends here.

class ButtonListener_IncreaseGain implements ActionListener {

DisplayForm dF = new DisplayForm();
Storage st = new Storage();

ButtonListener_IncreaseGain()
{

}

public void actionPerformed(ActionEvent e) {    

    st.iGain = 20;

    dF.revalidate();
    dF.repaint();
    System.out.println("Testing"); 
    }
}//Listener ends here.

Here is code of Data Display:

public void paint(Graphics g)
{        
    g2 = (Graphics2D) g;
    paintComponents(g2);

    //added numbers are for adjustment.
    int x = this.jPanel1.getX()+8;
    int y = this.jPanel1.getY()+30;
    int width = this.jPanel1.getWidth()+19;
    int height = this.jPanel1.getHeight()+40;


    //labelling voltages
    label0.setText(st.zero);
    label1.setText(st.v1);
    label2.setText(st.v2);
    label3.setText(st.v3);
    label4.setText(st.v4);
    label5.setText(st.v3);
    label6.setText(st.v4);


    g2.setColor(Color.darkGray);

    for(int i=x; i<width; i=i+80)
    {      
        g2.drawLine(i, y, i, height);          
    }              

    int j = 0;
    for(int i=y; i<height; i=i+80)
    {   
        j++;
        //st.iGain
        g2.setColor(Color.orange);
        if(j==1)
        {
           double k1 = st.iGain * 0.4;
           st.v1 = Double.toString(k1);

           g2.drawString(st.v1, x+5, y+10);
        }            

        if(j==2)
        {               
           double k2 = st.iGain * 0.3;
           st.v2 = Double.toString(k2);

           g2.drawString(st.v2, x+5, y+90);
        }
        g2.setColor(Color.DARK_GRAY);
        g2.drawLine(x, i, width, i); 
       ....         

    } //grid info is not completed yet.

Thanks,

解决方案

Focus isn't the issue and has nothing to do with your current problem. The solution is to change the properties of the data grid by updating fields it contains via setter methods and calling repaint on the JComponent (perhaps a JPanel, or some other component that derives ultimately from JComponent) held by the data grid. The paintComponent method of this component should use its class fields to update what it draws.

You almost never paint in the paint method of a JComponent and certainly you don't want to draw directly into a top-level window. You also probably don't want to set text of JLabels, JTextFields, or any other JTextComponent. from within paint/paintComponent.

I can't see why your code is not working and can only guess that the likely cause of your problem is in code not shown.

Edit 1:
Just guessing, but you may have a problem of references. I notice that your listener class creates new DisplayForm and Storage objects:

DisplayForm dF = new DisplayForm();
Storage st = new Storage();

There's a good possibility that these objects are not the ones being displayed, especially if you create these objects elsewhere and display them. Again I'm just guessing since I don't see the rest of your code, but perhaps you should to pass references for these objects into the DisplayForm via constructor or setter method parameters.

Edit 2:
e.g.,

public void setDisplayForm(DisplayForm dF) {
   this.dF = dF;
}

// same for Storage

And in the main program:

public MainDisplayForm() {
    initComponents();
    ButtonListener_IncreaseGain btnListenerIncreaseGain = new ButtonListener_IncreaseGain();
    btnListenerIncreaseGain.setDisplayForm(....);
    btnListenerIncreaseGain.setStorage(....);
    Btn_IncreaseGain.addActionListener(btnListenerIncreaseGain);
}

这篇关于如何在没有获得重点的情况下重新绘制焦点对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆