如何在JDialog中添加完整图像这个对话框是由JOptionPane创建的

时间:2013-01-12 18:22:20

标签: java swing

我的问题是我要添加完整的背景图片JDialogJDialog是由JOptionPane创建的。此图片未涵盖完整Dialog

如果您有任何解决方案,请告诉我。

public class BrowseFilePath {

    public static final String DIALOG_NAME = "what-dialog";
    public static final String PANE_NAME = "what-pane";
    private static JDialog loginRegister;
    private static String path;
    private static JPanel Browse_panel = new JPanel(new BorderLayout());
    private static JLabel pathLbl = new JLabel("Please Choose Folder / File");  
    private static JTextField regtxt_file = new JTextField(30);

    private static JButton browse_btn = new JButton("Browse");
    private static JButton ok_btn = new JButton("Ok");
    private static JButton close_btn = new JButton("Cancel");

    /*public static void main(String [] arg){
        showFileDialog();
    }*/
    public static void showFileDialog() {

        JOptionPane.setDefaultLocale(null);
        JOptionPane pane = new JOptionPane(createRegInputComponent());
        pane.setName(PANE_NAME);
        loginRegister = pane.createDialog("ShareBLU");
       /* try {
        loginRegister.setContentPane(new JLabel(new ImageIcon(ImageIO.read(AlertWindow.getBgImgFilePath()))));
        } catch (IOException e) {
            e.printStackTrace();
        }*/

        loginRegister.setName(DIALOG_NAME);
        loginRegister.setSize(380,150);
        loginRegister.setVisible(true);
        if(pane.getInputValue().equals("Ok")){
            String getTxt = regtxt_file.getText();
            BrowseFilePath.setPath(getTxt);
        }
        else if(pane.getInputValue().equals("Cancel")){
            regtxt_file.setText("");
            System.out.println("Pressed Cancel Button =======********=");
            System.exit(0);
        }
    }
    public static String getPath() {
        return path;
    }


    public static void setPath(String path) {
        BrowseFilePath.path = path;
    }


    private static JComponent createRegInputComponent() {
        Browse_panel = new JBackgroundPanel();

        Browse_panel.setLayout(new BorderLayout());

        Box rows = Box.createVerticalBox();
         Browse_panel.setBounds(0,0,380,150);

         Browse_panel.add(pathLbl);
         pathLbl.setForeground(Color.white);
         pathLbl.setBounds(20, 20, 200, 20);

         Browse_panel.add(regtxt_file);
         regtxt_file.setToolTipText("Select File/Folder..");
         regtxt_file.setBounds(20, 40, 220, 20);
         Browse_panel.add(browse_btn);
         browse_btn.setToolTipText("Browse");
         browse_btn.setBounds(250, 40, 90, 20);
         Browse_panel.add(ok_btn);
         ok_btn.setToolTipText("Ok");
         ok_btn.setBounds(40, 75, 80, 20);
         Browse_panel.add(close_btn);
         close_btn.setToolTipText("Cancel");
         close_btn.setBounds(130, 75, 80, 20);
        ActionListener chooseMe = createChoiceAction();
        ok_btn.addActionListener(chooseMe);
        close_btn.addActionListener(chooseMe);
        browse_btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();
                int selection = JFileChooser.FILES_AND_DIRECTORIES;
                fileChooser.setFileSelectionMode(selection);
                fileChooser.setAcceptAllFileFilterUsed(false);
                int rVal = fileChooser.showOpenDialog(null);
                if (rVal == JFileChooser.APPROVE_OPTION) {
                    path = fileChooser.getSelectedFile().toString();
                    regtxt_file.setText(path);
                }
            }

        });
         rows.add(Box.createVerticalStrut(105));
         Browse_panel.add(rows,BorderLayout.CENTER);
        return Browse_panel;
    }

    public  static ActionListener createChoiceAction() {
        ActionListener chooseMe = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JButton choice = (JButton) e.getSource();

                // find the pane so we can set the choice.
                Container parent = choice.getParent();
                while (!PANE_NAME.equals(parent.getName())) {
                    parent = parent.getParent();

                }

                JOptionPane pane = (JOptionPane) parent;
                pane.setInputValue(choice.getText());

                // find the dialog so we can close it.
                while ((parent != null) && !DIALOG_NAME.equals(parent.getName()))
                { 
                    parent = parent.getParent();
                //parent.setBounds(0, 0, 350, 150);
                }

               if (parent != null) {
                    parent.setVisible(false);
                }
            }
        };
        return chooseMe;
    } 
}

1 个答案:

答案 0 :(得分:2)

请勿使用JOptionPane但请使用完整的JDialog。将内容窗格设置为覆盖JComponent的{​​{1}},并返回相应的paintComponent()

以下示例代码:

getPreferredSize()

不要忘记提供合适的父import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Frame; import java.awt.Graphics; import java.net.MalformedURLException; import java.net.URL; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JPanel; import javax.swing.SwingUtilities; public class TestBackgroundImage { private static final String BACKHGROUND_IMAGE_URL = "http://cache2.allpostersimages.com/p/LRG/27/2740/AEPND00Z/affiches/blue-fiber-optic-wires-against-black-background.jpg"; protected void initUI() throws MalformedURLException { JDialog dialog = new JDialog((Frame) null, TestBackgroundImage.class.getSimpleName()); dialog.setModal(true); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); final ImageIcon backgroundImage = new ImageIcon(new URL(BACKHGROUND_IMAGE_URL)); JPanel mainPanel = new JPanel(new BorderLayout()) { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(backgroundImage.getImage(), 0, 0, getWidth(), getHeight(), this); } @Override public Dimension getPreferredSize() { Dimension size = super.getPreferredSize(); size.width = Math.max(backgroundImage.getIconWidth(), size.width); size.height = Math.max(backgroundImage.getIconHeight(), size.height); return size; } }; mainPanel.add(new JButton("A button"), BorderLayout.WEST); dialog.add(mainPanel); dialog.setSize(400, 300); dialog.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { try { new TestBackgroundImage().initUI(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } }