展开全部
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
final class popdemo extends JFrame {
final static private Dimension SIZE = new Dimension(800, 600);
public String getTitle() {return "popdemo";}
public Dimension getPreferredSize() {return SIZE;}
public Dimension getMinimumSize() {return SIZE;}
public Dimension getMaximumSize() {return SIZE;}
public Dimension getSize() {return SIZE;}
private JComponent popPane;
popdemo() throws HeadlessException {
init();
attachListeners();
doLay();
setVisible(true);
}
private void init() {
popPane=new JComponent() {
public Border getBorder() {
return BorderFactory.createDashedBorder(Color.RED);
}
};
popPane.setBounds(40,40,100,100);
JPopupMenu popupMenu = new JPopupMenu("POP");
popupMenu.add(new JMenuItem("menu 1"));
popPane.setComponentPopupMenu(popupMenu);
}
private void attachListeners() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(1);
}
});
}
private void doLay() {
Container container=getContentPane();
container.setLayout(null);
container.add(popPane);
pack();
}
public static void main(String... args) {
System.setProperty("swing.defaultlaf", "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.invokeLater(popdemo::new);
}
}