JDialog setLocation被忽略

时间:2012-06-28 10:55:05

标签: java swing

使用SSCCE和变通方法编辑

我想以编程方式更改JDialog的位置。

public class SSCCE {
  public static void main(String[] pArgs) {
    JDialog dialog = new JDialog();
    dialog.setSize(300, 300);
    dialog.setLocation(10, 10);
    dialog.setVisible(true);
  }
}

但是,这不起作用,新对话框始终位于屏幕中央。我怀疑它与我的L& F(Windows 7 64位,jdk 1.6_30)有什么关系,当设置对话框未修饰时,

public class SSCCEWorksButUndecorated {
  public static void main(String[] pArgs) {
    JDialog dialog = new JDialog();
    dialog.setSize(300, 300);
    dialog.setLocation(10, 10);
    dialog.setUndecorated(true);
    dialog.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
    dialog.setVisible(true);
  }
}

我指定的位置是可靠的。

但是我需要一个装饰对话框,之前有其他人看过这个问题还是我错过了什么?

针对模态对话框 setVisible()之后的设置位置进行解决方法

public class SSCCEWorkaroundForModalDialogs {
  public static void main(String[] pArgs) {
    final JDialog dialog = new JDialog();
    dialog.setSize(300, 300);
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        dialog.setLocation(10, 10);
      }
     });        
     dialog.setVisible(true);
  }
}

2 个答案:

答案 0 :(得分:1)

移动方法文档说:

  

此方法更改与布局相关的信息,因此,   使组件层次结构无效。

     

该方法更改与几何相关的数据。因此,本土   窗口系统可能会忽略此类请求,或者它可能会修改   请求的数据,以便以某种方式放置和调整Window对象的大小   与桌面设置密切相关。

我假设您可以在再次设置对话框之前调用revalidaite()

答案 1 :(得分:0)

我按照我的方式做到了 -

package com.whodesire.element;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;

public class MessageEdge {
    private JDialog messageEdge = null;
    private JDialog tempMessage = null;
    private Point elmLoc = null;
    private String[] option;
    private JButton btnOK;
    private JLayeredPane lp = null;
    private int top = 70;

    MessageEdge(JFrame owner){
        owner.setAlwaysOnTop(false);
    }

    public void ShowMessage(final String msg){
        ShowMessage(msg, "OK");
    }

    public void ShowMessage(final String msg, final String... option){
        this.option = option;
        init();
    }

    protected void setRootPaneStroking(JRootPane rootPane){
        rootPane.setWindowDecorationStyle(JRootPane.NONE);
        KeyStroke stroke = KeyStroke.getKeyStroke("ESCAPE");
        Action action = new AbstractAction() {
            private static final long serialVersionUID = 1L;
            public void actionPerformed(ActionEvent e) {
                disposeMessage();
            }
        };

        InputMap inputMap = rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        inputMap.put(stroke, "ESCAPE");
        rootPane.getActionMap().put("ESCAPE", action);
    };

    private void init(){
        top = 70;

        tempMessage = new JDialog(){
            private static final long serialVersionUID = 1L;
                {
                    setUndecorated(true);
                    setType(Type.UTILITY);
                    setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                    setBackground(new Color(0, 0, 0, 0));

                    elmLoc = Elementos.getElementosLocationPoint();
                    setSize(460, 57);
                    setLayout(new GridLayout(1, 1));
                    if(lp == null) 
                        lp = getModelJLayeredPane();
                    add(lp);

                    setLocation(elmLoc.x-10, elmLoc.y+top);
                    setAlwaysOnTop(true);

                    validate();
                    setVisible(true);
                }
        };

        new Thread(new Runnable(){
            public void run(){
                try{
                    OneMethod.playSound("catchyMessage");
                    whileLoop:
                    while(true){
                        //System.out.println("top value is : " + top + ", is top value bigger than -47 : " + (top > -47));
                        if(top > -47){
                            top -= 7;
                            tempMessage.setLocation(elmLoc.x-10, elmLoc.y+top);
                        }else{
                            top = -47;
                            initMessageEdge();
                            break whileLoop;
                        }
                        Thread.sleep(4);
                    }
                }catch(InterruptedException ie){
                    ie.printStackTrace();
                }
            }
        }).start();
    }

    private void initMessageEdge(){
        tempMessage.setVisible(false);
        tempMessage.remove(lp);
        tempMessage.dispose();

        //Elementos is my JFrame
        messageEdge = new JDialog(Elementos.getFrame(), true){
            private static final long serialVersionUID = 1L;
                {
                    setUndecorated(true);
                    setType(Type.UTILITY);
                    setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

                    setRootPaneStroking(getRootPane());
                    setBackground(new Color(0, 0, 0, 0));

                    requestFocusInWindow();

                    elmLoc = Elementos.getElementosLocationPoint();
                    setSize(460, 57);
                    setLayout(new GridLayout(1, 1));
                    if(lp == null) 
                        lp = getModelJLayeredPane();
                    add(lp);
                    setLocation(elmLoc.x-10, elmLoc.y+top);
                    setAlwaysOnTop(true);   
                    validate();
                }
        };
        messageEdge.setVisible(true);
        messageEdge.requestFocus();
    }

    private JLayeredPane getModelJLayeredPane(){
        JLayeredPane layer = new JLayeredPane();
        layer.setBounds(0, 0, 460, 57);
        layer.setLayout(null);

        JPanel panel = new JPanel(){
            private static final long serialVersionUID = 1L;
            public void paintComponent(Graphics gr){
                super.paintComponent(gr);
                Graphics2D g = (Graphics2D) gr;
                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
                g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN, 0.01f));
                g.setColor(new Color(255, 196, 51));
                g.fillRect(0, 0, 460, 57);

                g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.75f));
                g.setColor(new Color(255, 196, 51));
                g.fillRect(0, 0, 460, 47);

                g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
                g.setColor(new Color(244, 171, 50));
                int[] xxPoly = {0, 10,  10};
                int[] yyPoly = {47, 47, 57}; 
                Polygon pplg = new Polygon(xxPoly, yyPoly, xxPoly.length);
                g.fillPolygon(pplg);
            }
        };
        panel.setSize(450, 57);
        panel.setOpaque(true);
        panel.setLayout(null);
        layer.add(panel);

        btnOK = new JButton(){
            private static final long serialVersionUID = 1L;
            @Override
            protected void paintComponent(Graphics gr) {
                final Graphics2D g = (Graphics2D) gr.create();
                g.setPaint(new GradientPaint(new Point(0, 0), Color.WHITE, 
                        new Point(0, getHeight()), new Color(255, 196, 51)));
                g.fillRect(0, 0, getWidth(), getHeight());
                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
                g.setColor(Color.BLACK);
                g.setFont(new Font(OneMethod.getSegoeUIBoldFont().getFamily(), Font.BOLD, 11));
                g.drawString(option[0], 8, 17);
                g.dispose();
                super.paintComponent(g);
            }
        };
        btnOK.setFocusPainted(false);
        btnOK.setBorder(BorderFactory.createLineBorder(new Color(244, 171, 50)));
        btnOK.setBounds(460-30, (int)((47/2)-(25/2)), 30, 25);
        layer.add(btnOK);
        btnOK.addMouseListener(new MouseAdapter(){
            @Override
            public void mousePressed(MouseEvent evt){
                btnOK.setBorder(BorderFactory.createEmptyBorder());
                btnOK.setBorder(BorderFactory.createLoweredBevelBorder());
            }
            @Override
            public void mouseReleased(MouseEvent evt){
                btnOK.setBorder(BorderFactory.createLineBorder(new Color(244, 171, 50)));
                disposeMessage();
                Elementos.getFrame().setAlwaysOnTop(true);
            }
        });
        layer.moveToFront(btnOK);

        return layer;
    }


    private void disposeMessage(){
        messageEdge.setModal(false);
        messageEdge.setVisible(false);
        messageEdge.dispose();
    }
}

希望它可以帮助你和其他人,干杯......