Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么这个JDialog在Win7模式下闪烁?_Java_Windows 7_Flicker_Java 7_Jdialog - Fatal编程技术网

Java 为什么这个JDialog在Win7模式下闪烁?

Java 为什么这个JDialog在Win7模式下闪烁?,java,windows-7,flicker,java-7,jdialog,Java,Windows 7,Flicker,Java 7,Jdialog,在Windows7和Java1.7中(如果是1.6,请注释掉所示的两行),创建一个名为“test”的NetBeans/Eclipse项目,其中包含两个类,test和DroppableFrame,并将此代码粘贴到其中。然后运行它 如果使用modal=true运行,它将闪烁。如果使用modal=false运行,则不会。我做错了什么 // test.java package test; import java.awt.Color; import java.awt.Container; import

在Windows7和Java1.7中(如果是1.6,请注释掉所示的两行),创建一个名为“test”的NetBeans/Eclipse项目,其中包含两个类,test和DroppableFrame,并将此代码粘贴到其中。然后运行它

如果使用modal=true运行,它将闪烁。如果使用modal=false运行,则不会。我做错了什么

// test.java
package test;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Test implements KeyListener, MouseListener
{
    public Test(boolean m_modal)
    {
        Dimension prefSize;
        Insets inset;
        Font fontLabel, fontInput, fontButtons;
        int buttonCenters, buttonBackoff, buttonWidth, buttonCount, thisButton, buttonTop;
        String caption;

        JLayeredPane m_pane;
        JLabel m_lblBackground;
        JScrollPane m_txtInputScroll;
        int m_width, m_height, m_actual_width, m_actual_height;
        boolean m_singleLineInput   = true;
        String m_caption            = "Sample Modal Input";

        m_width     = 450;
        m_height    = m_singleLineInput ? /*single line*/180 : /*multi-line*/250;
        m_frame     = new DroppableFrame(false);
        caption     = m_caption;
        m_frame.setTitle(caption);

        // Compute the actual size we need for our window, so it's properly centered
        m_frame.pack();
        Insets fi       = m_frame.getInsets();
        m_actual_width  = m_width  + fi.left + fi.right;
        m_actual_height = m_height + fi.top  + fi.bottom;
        m_frame.setSize(m_width  + fi.left + fi.right,
                        m_height + fi.top  + fi.bottom);

        prefSize = new Dimension(m_width  + fi.left + fi.right,
                                 m_height + fi.top  + fi.bottom);
        m_frame.setMinimumSize(prefSize);
        m_frame.setPreferredSize(prefSize);
        m_frame.setMinimumSize(prefSize);
        m_frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        m_frame.setSize(m_width, m_height);
        m_frame.setLocationRelativeTo(null);    // Center window
        m_frame.setLayout(null);                // We handle all redraws

        Container c = m_frame.getContentPane();
        c.setBackground(Color.WHITE);
        c.setForeground(Color.BLACK);

        m_pane = new JLayeredPane();
        m_pane.setLayout(null);
        m_pane.setBounds(0, 0, m_width, m_height);
        m_pane.setVisible(true);
        m_pane.setBorder(BorderFactory.createEmptyBorder());
        c.add(m_pane);

        // Set the background image
        m_lblBackground = new JLabel();
        m_lblBackground.setBounds(0, 0, m_width, m_height);
        m_lblBackground.setHorizontalAlignment(JLabel.LEFT);
        m_lblBackground.setVerticalAlignment(JLabel.TOP);
        m_lblBackground.setVisible(true);
        m_lblBackground.setBackground(Color.WHITE);
        m_pane.add(m_lblBackground);
        m_pane.moveToFront(m_lblBackground);

        // Create the fonts
        fontLabel   = new Font("Calibri", Font.BOLD, 20);
        fontInput   = new Font("Calibri", Font.BOLD, 14);
        fontButtons = fontLabel;

        // Create the visible label contents
        JLabel m_lblLabel = new JLabel();
        m_lblLabel.setBounds(15, 52, 423, 28);
        m_lblLabel.setHorizontalAlignment(JLabel.LEFT);
        m_lblLabel.setVerticalAlignment(JLabel.CENTER);
        m_lblLabel.setFont(fontLabel);
        m_lblLabel.setForeground(Color.BLUE);
        m_lblLabel.setText("A sample input:");
        m_lblLabel.setVisible(true);
        m_pane.add(m_lblLabel);
        m_pane.moveToFront(m_lblLabel);

        // Create the input box
        if (m_singleLineInput)
        {   // It's a single-line input box
            m_txtInputSingleLine = new JTextField();
            m_txtInputSingleLine.setBounds(15, 85, 421, 25);
            m_txtInputSingleLine.setFont(fontInput);
            m_txtInputSingleLine.setText("Initial Value");
            m_txtInputSingleLine.setVisible(true);
            m_txtInputSingleLine.addKeyListener(this);
            m_pane.add(m_txtInputSingleLine);
            m_pane.moveToFront(m_txtInputSingleLine);

        } else {
            m_txtInput          = new JTextArea();
            m_txtInputScroll    = new JScrollPane(m_txtInput);
            m_txtInputScroll.setBounds(15, 83, 421, 100);
            m_txtInputScroll.setAutoscrolls(true);
            m_txtInputScroll.setVisible(true);
            m_txtInput.setFont(fontInput);
            m_txtInput.setLineWrap(true);
            m_txtInput.setWrapStyleWord(true);
            m_txtInput.setTabSize(2);
            m_txtInput.setText("Initial Value");
            m_txtInput.setVisible(true);
            m_pane.add(m_txtInputScroll);
            m_pane.moveToFront(m_txtInputScroll);
        }

        // Determine which buttons are specified
        buttonCount = 0;
        m_buttons = _CANCEL_BUTTON + _OKAY_BUTTON;
        if ((m_buttons & _NEXT_BUTTON) != 0)
        {
            m_btnNext = new JButton("Next");
            m_btnNext.setFont(fontButtons);
            m_btnNext.addMouseListener(this);
            inset = m_btnNext.getInsets();
            inset.left = 3;
            inset.right = 3;
            m_btnNext.setMargin(inset);
            m_pane.add(m_btnNext);
            m_pane.moveToFront(m_btnNext);
            ++buttonCount;
        }
        if ((m_buttons & _CANCEL_BUTTON) != 0)
        {
            m_btnCancel = new JButton("Cancel");
            m_btnCancel.setFont(fontButtons);
            m_btnCancel.addMouseListener(this);
            inset = m_btnCancel.getInsets();
            inset.left = 3;
            inset.right = 3;
            m_btnCancel.setMargin(inset);
            m_pane.add(m_btnCancel);
            m_pane.moveToFront(m_btnCancel);
            ++buttonCount;
        }
        if ((m_buttons & _ACCEPT_BUTTON) != 0)
        {
            m_btnAccept = new JButton("Accept");
            m_btnAccept.setFont(fontButtons);
            m_btnAccept.addMouseListener(this);
            inset = m_btnAccept.getInsets();
            inset.left = 3;
            inset.right = 3;
            m_btnAccept.setMargin(inset);
            m_pane.add(m_btnAccept);
            m_pane.moveToFront(m_btnAccept);
            ++buttonCount;
        }
        if ((m_buttons & _OKAY_BUTTON) != 0)
        {
            m_btnOkay = new JButton("Okay");
            m_btnOkay.setFont(fontButtons);
            m_btnOkay.addMouseListener(this);
            inset = m_btnOkay.getInsets();
            inset.left = 3;
            inset.right = 3;
            m_btnOkay.setMargin(inset);
            m_pane.add(m_btnOkay);
            m_pane.moveToFront(m_btnOkay);
            ++buttonCount;
        }

        // Determine the coordinates for each button
        buttonCenters   = (m_width / (buttonCount + 1));
        buttonWidth     = (int)((double)buttonCenters * 0.80);
        buttonBackoff   = (m_width / (buttonCount + 2)) / 2;

        // Position the buttons
        thisButton  = 1;
        buttonTop   = m_singleLineInput ? 130 : 200;
        if (m_btnNext != null)
        {   // Position and make visible this button
            m_btnNext.setBounds( + (thisButton * buttonCenters) - buttonBackoff, buttonTop, buttonWidth, 40);
            m_btnNext.setVisible(true);
            ++thisButton;
        }
        if (m_btnCancel != null)
        {   // Position and make visible this button
            m_btnCancel.setBounds((thisButton * buttonCenters) - buttonBackoff, buttonTop, buttonWidth, 40);
            m_btnCancel.setVisible(true);
            ++thisButton;
        }
        if (m_btnAccept!= null)
        {   // Position and make visible this button
            m_btnAccept.setBounds((thisButton * buttonCenters) - buttonBackoff, buttonTop, buttonWidth, 40);
            m_btnAccept.setVisible(true);
            ++thisButton;
        }
        if (m_btnOkay != null)
        {   // Position and make visible this button
            m_btnOkay.setBounds((thisButton * buttonCenters) - buttonBackoff, buttonTop, buttonWidth, 40);
            m_btnOkay.setVisible(true);
            ++thisButton;
        }

// The modal code causes some slow component rendering.
// Needs looked at to figure out why
        if (m_modal)
        {   // Make it a modal window
            m_frame.setModal(m_width, m_height, fi, m_pane);

        } else {
            // Make it a non-modal window
            m_frame.setVisible(true);
            m_frame.forceWindowToHaveFocus();
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
    }

    @Override
    public void keyReleased(KeyEvent e) {
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        m_frame.dispose();
        System.exit(0);
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    public static void main(String[] args)
    {
        boolean modal = true;
        Test t = new Test(modal);
    }

    private DroppableFrame  m_frame;

    private int             m_buttons;
    private JTextField      m_txtInputSingleLine;
    private JTextArea       m_txtInput;
    private JButton         m_btnNext;
    private JButton         m_btnCancel;
    private JButton         m_btnAccept;
    private JButton         m_btnOkay;

    public static final int _NEXT_BUTTON    = 1;
    public static final int _CANCEL_BUTTON  = 2;
    public static final int _ACCEPT_BUTTON  = 4;
    public static final int _OKAY_BUTTON    = 8;
    public static final int _NEXT_CANCEL    = 3;
    public static final int _ACCEPT_CANCEL  = 6;
    public static final int _OKAY_CANCEL    = 10;
}


        // DroppableFrame.java
    package test;

    import java.awt.*;
    import java.awt.event.ComponentEvent;
    import java.io.*;
    import java.util.*;
    import java.awt.dnd.*;
    import java.awt.datatransfer.*;
    import java.awt.event.ComponentListener;
    import java.awt.event.InputEvent;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import javax.swing.*;

    public class DroppableFrame extends JFrame
                                implements  DropTargetListener,
                                            DragSourceListener,
                                            DragGestureListener,
                                            ComponentListener
    {
        public DroppableFrame(boolean isResizeable)
        {
            super("JFrame");

            m_dragSource    = DragSource.getDefaultDragSource();
            m_dragSource.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY_OR_MOVE, this);
            this.setDropTarget(new DropTarget(this, DnDConstants.ACTION_COPY_OR_MOVE, this));
            addComponentListener(this);

            m_isResizeable  = isResizeable;
            m_modal         = false;
            setResizable(isResizeable);
        }

        /**
         * Sets the frame to a modal state
         * @param width
         * @param height
         */
        public void setModal(int            width,
                             int            height,
                             Insets         fi,
                             JLayeredPane   m_pan)
        {
            int i;
            Component[] comps;
            Component c;
            Point p;

            m_modal         = true;
            m_modalDialog   = new Dialog((JFrame)null, getTitle(), true);
            m_modalDialog.setLayout(null);
            m_modalDialog.setResizable(m_isResizeable);
            m_modalDialog.setSize(width + fi.left + fi.right, height + fi.top + (fi.bottom * 2));
            m_modalDialog.setAlwaysOnTop(true);
            m_modalDialog.setLocationRelativeTo(null);
            if (m_pan != null)
            {   // Add/copy the pane's components
                comps = m_pan.getComponents();
                if (comps != null)
                    for (i = 0; i < comps.length; i++)
                    {   // Add and reposition the component taking into account the insets
                        c = m_modalDialog.add(comps[i]);
                        p = c.getLocation();
                        c.setLocation(p.x + fi.left, p.y + fi.top + fi.bottom);
                    }
            }
            m_modalDialog.setVisible(true);
        }

        @Override
        public void paint(Graphics g)
        {
            Dimension d = getSize();
            Dimension m = getMaximumSize();

            boolean resize = d.width > m.width || d.height > m.height;
            d.width = Math.min(m.width, d.width);
            d.height = Math.min(m.height, d.height);
            if (resize)
            {
                Point p = getLocation();
                setVisible(false);
                setSize(d);
                setLocation(p);
                setVisible(true);
            }
            super.paint(g);
        }

        @Override
        public void dragDropEnd(DragSourceDropEvent DragSourceDropEvent){}

        @Override
        public void dragEnter(DragSourceDragEvent DragSourceDragEvent){}

        @Override
        public void dragExit(DragSourceEvent DragSourceEvent){}

        @Override
        public void dragOver(DragSourceDragEvent DragSourceDragEvent){}

        @Override
        public void dropActionChanged(DragSourceDragEvent DragSourceDragEvent){}

        @Override
        public void dragEnter (DropTargetDragEvent dropTargetDragEvent)
        {
            dropTargetDragEvent.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);
        }

        @Override
        public void dragExit (DropTargetEvent dropTargetEvent) {}

        @Override
        public void dragOver (DropTargetDragEvent dropTargetDragEvent) {}

        @Override
        public void dropActionChanged (DropTargetDragEvent dropTargetDragEvent){}

        @Override
        public synchronized void drop(DropTargetDropEvent dropTargetDropEvent)
        {
            try
            {
                Transferable tr = dropTargetDropEvent.getTransferable();
                if (tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor))
                {
                    dropTargetDropEvent.acceptDrop (DnDConstants.ACTION_COPY_OR_MOVE);
                    java.util.List fileList = (java.util.List)tr.getTransferData(DataFlavor.javaFileListFlavor);
                    Iterator iterator = fileList.iterator();
                    while (iterator.hasNext())
                    {
                        File file = (File)iterator.next();
                        if (file.getName().toLowerCase().endsWith(".xml"))
                        {   // Do something with the file here

                        } else {
                            System.out.println("Ignored dropped file: " + file.getAbsolutePath());
                        }
                    }
                    dropTargetDropEvent.getDropTargetContext().dropComplete(true);

                } else {
                    dropTargetDropEvent.rejectDrop();

                }

            } catch (IOException io) {
                dropTargetDropEvent.rejectDrop();

            } catch (UnsupportedFlavorException ufe) {
                dropTargetDropEvent.rejectDrop();

            }
        }

        @Override
        public void dragGestureRecognized(DragGestureEvent dragGestureEvent)
        {
        }

        public void setTranslucency(float opaquePercent)
        {
            try
            {
                if (System.getProperty("java.version").substring(0,3).compareTo("1.6") <= 0)
                {   // Code for translucency works in 1.6, raises exception in 1.7
                    Class awtUtilitiesClass = Class.forName("com.sun.awt.AWTUtilities");
                    Method mSetWindowOpacity;
                    mSetWindowOpacity = awtUtilitiesClass.getMethod("setWindowOpacity", Window.class, float.class);
                    if (mSetWindowOpacity != null)
                    {
                        if (!m_modal)
                            mSetWindowOpacity.invoke(null, this, opaquePercent);
                    }

                } else {
// If compiling in 1.6 or earlier, comment-out these next two lines
                    if (!m_modal)
                        setOpacity(opaquePercent);
                }

            } catch (NoSuchMethodException ex) {
            } catch (SecurityException ex) {
            } catch (ClassNotFoundException ex) {
            } catch (IllegalAccessException ex) {
            } catch (IllegalArgumentException ex) {
            } catch (InvocationTargetException ex) {
            } catch (IllegalComponentStateException ex) {
            } catch (Throwable t) {
            } finally {
            }
        }

        @Override
        public void componentResized(ComponentEvent e)
        {
            Dimension d = getSize();
            Dimension m = getMaximumSize();

            boolean resize = d.width > m.width || d.height > m.height;
            d.width = Math.min(m.width, d.width);
            d.height = Math.min(m.height, d.height);
            if (resize)
                setSize(d);
        }

        @Override
        public void componentMoved(ComponentEvent e) {
        }

        @Override
        public void componentShown(ComponentEvent e) {
        }

        @Override
        public void componentHidden(ComponentEvent e) {
        }

        @Override
        public void dispose()
        {
            if (m_modal)
            {
                m_modalDialog.setVisible(false);
                m_modalDialog = null;
            }
            super.dispose();
        }

        public void forceWindowToHaveFocus()
        {
            Rectangle bounds;
            Insets insets;
            Robot robot = null;

            if (m_modal)
                m_modalDialog.setVisible(true);
            setVisible(true);
            toFront();

            bounds = getBounds();
            insets = getInsets();

            try {
                robot = new Robot();
                robot.mouseMove(bounds.x + bounds.width / 2, bounds.y + insets.top / 2);
                robot.mousePress(InputEvent.BUTTON1_MASK);
                robot.mouseRelease(InputEvent.BUTTON1_MASK);

            } catch (AWTException ex) {
            }
        }

        protected DragSource            m_dragSource;
        protected boolean               m_isResizeable;
        protected boolean               m_modal;
        protected Dialog                m_modalDialog;
    }
//test.java
包装试验;
导入java.awt.Color;
导入java.awt.Container;
导入java.awt.Dimension;
导入java.awt.Font;
导入java.awt.Insets;
导入java.awt.event.KeyEvent;
导入java.awt.event.KeyListener;
导入java.awt.event.MouseEvent;
导入java.awt.event.MouseListener;
导入javax.swing.BorderFactory;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JLayeredPane;
导入javax.swing.JScrollPane;
导入javax.swing.JTextArea;
导入javax.swing.JTextField;
公共类测试实现KeyListener、MouseListener
{
公共测试(布尔m_模式)
{
尺寸和尺寸;
插图;
字体字体标签、字体输入、字体按钮;
int按钮中心,按钮后退,按钮宽度,按钮计数,此按钮,按钮顶部;
字符串标题;
JLayeredPane m_pane;
JLabel m_lblbbackground;
JScrollPane Mu txInputScroll;
int m_宽度、m_高度、m_实际宽度、m_实际高度;
布尔m_singleLineInput=true;
字符串m_caption=“示例模态输入”;
m_宽度=450;
m_高度=m_单线输入?/*单线*/180:/*多线*/250;
m_帧=新的DroppableFrame(假);
字幕=m_字幕;
m_frame.setTitle(标题);
//计算窗口的实际大小,使其正确居中
m_frame.pack();
Insets fi=m_frame.getInsets();
m_实际_宽度=m_宽度+fi.左+fi.右;
m_实际高度=m_高度+fi.top+fi.bottom;
m_帧设置大小(m_宽度+fi左+fi右,
m_高度+fi顶部+fi底部);
prefSize=新尺寸(m_宽度+fi.left+fi.right,
m_高度+fi顶部+fi底部);
m_帧设置最小尺寸(预尺寸);
m_frame.setPreferredSize(prefSize);
m_帧设置最小尺寸(预尺寸);
m_frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
m_框架设置尺寸(m_宽度、m_高度);
m_frame.setLocationRelativeTo(null);//中心窗口
m_frame.setLayout(null);//我们处理所有重画
容器c=m_frame.getContentPane();
c、 挫折地面(颜色:白色);
c、 设置前景(颜色为黑色);
m_pane=new JLayeredPane();
m_pane.setLayout(空);
m_窗格。收进边界(0,0,m_宽度,m_高度);
m_窗格。设置可见(真);
m_pane.setBorder(BorderFactory.createEmptyByOrder());
c、 添加(m_窗格);
//设置背景图像
Mlblbackground=新的JLabel();
m_lbackground.立根(0,0,m_宽度,m_高度);
Mlbulbackground.设置水平对齐(JLabel.左);
Mlbul背景设置垂直对齐(JLabel.顶部);
Mlblbackground.setVisible(真);
Mlblbackground.setBackground(颜色:白色);
m_窗格。添加(m_lbackground);
m_窗格。移动到前面(m_lbackground);
//创建字体
fontLabel=新字体(“Calibri”,Font.BOLD,20);
fontInput=新字体(“Calibri”,Font.BOLD,14);
fontButtons=fontLabel;
//创建可见的标签内容
JLabel m_lblLabel=新JLabel();
m_lblLabel.立根(15,52,423,28);
m_lblLabel.设置水平对齐(JLabel.左);
m_lblLabel.设置垂直对齐(JLabel.中心);
m_lblLabel.setFont(fontLabel);
m_lblLabel.设置前景(颜色:蓝色);
m_lblLabel.setText(“示例输入:”);
m_lblLabel.setVisible(真);
m_窗格。添加(m_lblLabel);
m_窗格。移动到前面(m_lblLabel);
//创建输入框
如果(m_singleLineInput)
{//这是一个单行输入框
m_txtInputSingleLine=新的JTextField();
m_txInputSingleLine.setBounds(15,85,421,25);
m_txtInputSingleLine.setFont(fontInput);
m_txInputSingleline.setText(“初始值”);
m_txtInputSingleLine.setVisible(true);
m_txtInputSingleLine.addKeyListener(this);
添加(m_txtInputSingleLine);
m_窗格。移动到前面(m_txInputSingleLine);
}否则{
m_txtInput=新的JTextArea();
m_txtInputScroll=新的JScrollPane(m_txtInput);
m_txInputScroll.setBounds(15,83421100);
m_txInputScroll.setAutoscrolls(真);
m_txInputScroll.setVisible(真);
m_txtInput.setFont(fontInput);
m_txtInput.setLineWrap(真);
m_txtInput.setWrapStyleWord(真);
m_txtInput.setTabSize(2);
m_txtInput.setText(“初始值”);
m_txtInput.setVisible(真);
添加(m_txtInputScroll);
m_窗格。移动到前面(m_txInputScroll);
}
//确定指定了哪些按钮
buttonCount=0;
m_按钮=_取消按钮+_确定按钮;
如果((m_按钮和下一个_按钮)!=0)
{
m_btnNext=新按钮(“下一步”);
m_btnNext.setFont(字体按钮);
m_btnNext.addMouseListener(本);
inset=m_btnNext.getInsets();
插图左=3;
插图右=3;
m_btnNext.设置边距(插图);
m_窗格。添加(m_btnNext);
m_窗格。移动到前面(m_btnNext);
++按钮计数;
}
如果((m按钮和取消按钮)!=0)
{
m_btnCancel=新按钮(“取消”);
m_btnCancel.setFont(字体按钮);
m_btnCa