gpt4 book ai didi

java - JPopupMenu 的面积是否太小?

转载 作者:行者123 更新时间:2023-12-02 07:23:01 26 4
gpt4 key购买 nike

在上一个问题中,我向社区询问为什么我的 JPopupMenu 没有出现在屏幕上。
我无法想出一个简单的、可运行的、可编译的示例。
所以,这就是我为你们所做的:
enter image description here

  • 区域太小,无法绘制弹出窗口吗?

  • 我希望我的弹出窗口是这样的:
    pop我所做的代码在第一张照片中可见。

    代码:

    /* The old code entered here has been removed */

    完整代码可以是found here

    编辑2

    我将各种 JRadioButtonMenuItemsetupJPopup() 复制到一个新文件中并运行。有用。为什么它在 ScreenRecorder 类中不起作用?代码封装演示;

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

    import javax.swing.ButtonGroup;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
    import javax.swing.JPopupMenu;
    import javax.swing.JRadioButtonMenuItem;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;

    public class PopupTrial {


    public PopupTrial(){
    setupJPopup();
    JFrame frame = new JFrame();
    try{
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }catch(Exception e){

    }
    frame.getContentPane().add(label);
    label.addMouseListener(new MouseAdapter(){
    @Override
    public void mouseClicked(MouseEvent e){
    popup.show(e.getComponent(), e.getX(), e.getY());
    }
    });
    frame.setVisible(true);
    frame.setSize(300, 300);
    }

    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable(){
    @Override
    public void run(){
    new PopupTrial();
    }
    });
    }
    public void setupJPopup(){
    encodingGroup.add(avi);
    encodingGroup.add(quicktime);

    popup.add(avi);
    popup.add(quicktime);
    popup.addSeparator();

    recordingAreaGroup.add(entireScreen);
    recordingAreaGroup.add(custom);

    popup.add(entireScreen);
    popup.add(custom);
    popup.addSeparator();

    cursorGroup.add(selectBlackCursor);
    cursorGroup.add(selectWhiteCursor);
    cursorGroup.add(selectNoCursor);

    selectCursor.add(selectBlackCursor);
    selectCursor.add(selectWhiteCursor);
    selectCursor.add(selectNoCursor);

    popup.add(selectCursor);
    popup.pack();
    }
    JLabel label = new JLabel("Click Me");
    ButtonGroup recordingAreaGroup = new ButtonGroup();
    ButtonGroup cursorGroup = new ButtonGroup();
    ButtonGroup encodingGroup = new ButtonGroup();
    JPopupMenu popup = new JPopupMenu();
    JRadioButtonMenuItem avi = new JRadioButtonMenuItem("AVI",true);
    JRadioButtonMenuItem quicktime = new JRadioButtonMenuItem("QuickTime",false);
    JRadioButtonMenuItem entireScreen = new JRadioButtonMenuItem("Entire Screen",true);
    JRadioButtonMenuItem custom = new JRadioButtonMenuItem("Custom...",false);
    JMenuItem selectCursor = new JMenu("Select a cursor");
    JRadioButtonMenuItem selectWhiteCursor = new JRadioButtonMenuItem("White Cursor",true);
    JRadioButtonMenuItem selectBlackCursor = new JRadioButtonMenuItem("Black Cursor",false);
    JRadioButtonMenuItem selectNoCursor = new JRadioButtonMenuItem("No Cursor",false);
    }

    enter image description here

    最佳答案

    不,JFrame 的大小与 PopupMenu 不显示的原因无关。这是一个示例,显示与您想要的类似(并使用类似的方法)的工作:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class PopupMenu extends Box{

    Dimension preferredSize = new Dimension(400,30);

    public PopupMenu(){
    super(BoxLayout.Y_AXIS);

    final JPopupMenu menu = new JPopupMenu("Options");
    for(int i = 1; i < 20; i++)
    menu.add(new JMenuItem("Option" + i));

    JLabel clickMe = new JLabel("ClickMe");
    clickMe.setAlignmentX(RIGHT_ALIGNMENT);
    clickMe.addMouseListener(new MouseAdapter(){

    @Override
    public void mouseClicked(MouseEvent e) {
    menu.show(e.getComponent(), e.getX(), e.getY());
    }});

    add(clickMe);
    }

    public Dimension getPreferredSize(){
    return preferredSize;
    }

    public static void main(String[] args) {

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(new PopupMenu());
    frame.validate();
    frame.pack();
    frame.setVisible(true);
    }
    }

    关于java - JPopupMenu 的面积是否太小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13953968/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com