开发者

add components inside JDialog Box in Swing without inner class which extends JDialog like the code below

开发者 https://www.devze.com 2023-03-07 06:34 出处:网络
I want to add components inside JDialog Box in Swing without extending inner class which extends JDialog like the code below,SimpleAboutDialog is an inner class inside my class, based on some conditio

I want to add components inside JDialog Box in Swing without extending inner class which extends JDialog like the code below,SimpleAboutDialog is an inner class inside my class, based on some condition I m instantiating its class to give a JDialog box with JLabels but I dont want to use object of it

package com.project.swings.layout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

public class PopUpMenu{
      JPopupMenu Pmenu;
      JMenuItem menuItem;
      public static void main(String[] args) {
        PopUpMenu p = new PopUpMenu();
      }

      public PopUpMenu(){
        JFrame frame = new JFrame("Creating a Popup Menu");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Pmenu = new JPopupMenu();
        menuItem = new JMenuItem("Cut");
        menuItem.addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e){
                  if(e.getActionCommand().equals("Cut")){
开发者_高级运维                      SimpleAboutDialog sad=new SimpleAboutDialog(new JFrame());
                      sad.setSize(200, 200);
                      sad.setVisible(true);
                  }
              }
              });
        Pmenu.add(menuItem);
        menuItem = new JMenuItem("Copy");
        menuItem.addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e){
                  if(e.getActionCommand().equals("Copy")){
                      JOptionPane.showInputDialog(null, "Please choose a name", "Example 1",
                                JOptionPane.QUESTION_MESSAGE, null, new Object[] { "Amanda",
                                    "Colin", "Don", "Fred", "Gordon", "Janet", "Jay",
                                    "Joe", "Judie", "Kerstin", "Lotus", "Maciek", "Mark",
                                    "Mike", "Mulhern", "Oliver", "Peter", "Quaxo", "Rita",
                                    "Sandro", "Tim", "Will" }, "Joe");
                  }
              }
              });
        Pmenu.add(menuItem);
        menuItem = new JMenuItem("Paste");
        menuItem.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
              if(e.getActionCommand().equals("Paste")){
                  String string[]={"Sam","Samarth","Mourya"};
                  JComboBox combo=new JComboBox();

                  JOptionPane.showInputDialog(new JFrame(),"Combo","Dialog",1,null,string ,"Mourya");
              }
          }
        }
          );
        Pmenu.add(menuItem);
        menuItem = new JMenuItem("Delete");
        Pmenu.add(menuItem);
        menuItem = new JMenuItem("Undo");
        Pmenu.add(menuItem);
        menuItem.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent e){
              if(e.getActionCommand().equals("Cut")){
                  SimpleAboutDialog sad=new SimpleAboutDialog(new JFrame());
                  sad.setSize(200, 200);
                  sad.setVisible(true);
              }
          }
          });
        frame.addMouseListener(new MouseAdapter(){
          public void mouseReleased(MouseEvent Me){
            if(Me.isPopupTrigger()){
              Pmenu.show(Me.getComponent(), Me.getX(), Me.getY());
            }
          }
        });
        frame.setSize(400,400);
        frame.setVisible(true);
      }

      private class SimpleAboutDialog extends JDialog {


        public SimpleAboutDialog(JFrame parent) {
            super(parent, "About Dialog", true);

            Box b = Box.createVerticalBox();
            b.add(Box.createGlue());
            b.add(new JLabel("J"));
            b.add(new JButton("g"));

            b.add(Box.createGlue());
            getContentPane().add(b, "Center");

            JPanel p2 = new JPanel();
            JButton ok = new JButton("Ok");
            p2.add(ok);
            getContentPane().add(p2, "South");

            ok.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent evt) {
                setVisible(false);
              }
            });

            setSize(250, 150);
          }

      }
    }


Everything you do could be done outside the SimpleAboutDialog class.

Just like you call f.show(); you would add a reference to the JDialog like so f.setSize(250, 150); or f.getContentPane().add(p2, "South");


JOptionPane.showInputDialog(parentComponent, message, title, messageType, icon, selectionValues, initialSelectionValue) is another way of adding Jcombo Box and Button.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号