2

使用 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 64bit,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);
  }
}
4

2 回答 2

1

移动方法文档说:

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

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

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

于 2012-06-28T11:14:15.807 回答
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();
    }
}

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

于 2016-04-02T19:10:06.253 回答