在没有扩展 JDialog 的内部类的 Swing 中的 JDialog Box 中添加组件,如下面的代码 [英] add components inside JDialog Box in Swing without inner class which extends JDialog like the code below
问题描述
我想在 Swing 中的 JDialog Box 内添加组件而不扩展像下面的代码一样扩展 JDialog 的内部类,SimpleAboutDialog 是我类中的一个内部类,基于某些条件,我正在实例化它的类以提供带有 JLabels 的 JDialog 框但我不想使用它的对象
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);
}
}
}
推荐答案
JOptionPane.showInputDialog(parentComponent, message, title, messageType, icon, selectionValues, initialSelectionValue)是另一种添加 Jcombo Box 和 Button 的方式.
JOptionPane.showInputDialog(parentComponent, message, title, messageType, icon, selectionValues, initialSelectionValue) is another way of adding Jcombo Box and Button.
这篇关于在没有扩展 JDialog 的内部类的 Swing 中的 JDialog Box 中添加组件,如下面的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!