Popup 中的导航问题以及如何使用 setLocationRelativeto()
Navigation problem in Popup and how to use setLocationRelativeto()
我有一个 JButton
名为 Graphics
:
当我按下它时,出现一个新的弹出菜单,用户选择屏幕分辨率:
我已将自定义图像添加到菜单项,我无法使用箭头键导航到任何菜单项。
第二个问题是弹出菜单没有固定在框架内,请告诉我如何让它固定在 JFrame
或图形按钮内。
我已经尝试过 KeyListener 但是没有用。
在下面的代码中,我创建了一个 JPopupMenu
并对其进行了初始化,在 KeyListener
函数中,我试图解决导航问题,但没有成功。
popgraphics=new JPopupMenu();
JMenuItem p1920=new JMenuItem();
JMenuItem p1536=new JMenuItem();
JMenuItem p1280=new JMenuItem();
p1920.setIcon(new ImageIcon(getClass().getResource("icons\1920p.png")));
p1536.setIcon(new ImageIcon(getClass().getResource("icons\1536p.png")));
p1280.setIcon(new ImageIcon(getClass().getResource("icons\1280p.png")));
p1920.setBorder(b1);
// p1920.setText("1920x1080 Pixels");
// p1536.setText("1536x1440 Pixels");
// p1280.setText("1280x720 Pixels");
p1920.setFont(new Font("MV Boli",0,14));
p1536.setFont(new Font("MV Boli",0,14));
p1280.setFont(new Font("MV Boli",0,14));
p1920.setOpaque(false);
p1536.setOpaque(false);
p1536.setOpaque(false);
// popgraphics.setSelected(null);
// popgraphics.setSelectionModel(SingleSelectionModel);
popgraphics.addKeyListener(new KeyListener()
{
@Override
public void keyTyped(KeyEvent e) {
if(e.getKeyChar()==(char)38)
{
if(popgraphics.getSelectionModel().getSelectedIndex()==1)
{
popgraphics.getSelectionModel().setSelectedIndex(3);
}
else if(popgraphics.getSelectionModel().getSelectedIndex()==2)
{
popgraphics.getSelectionModel().setSelectedIndex(1);
}
else if(popgraphics.getSelectionModel().getSelectedIndex()==3)
{
popgraphics.getSelectionModel().setSelectedIndex(2);
}
}
if(e.getKeyChar()==(char)40)
{
if(popgraphics.getSelectionModel().getSelectedIndex()==1)
{
popgraphics.getSelectionModel().setSelectedIndex(2);
}
else if(popgraphics.getSelectionModel().getSelectedIndex()==2)
{
popgraphics.getSelectionModel().setSelectedIndex(3);
}
else if(popgraphics.getSelectionModel().getSelectedIndex()==3)
{
popgraphics.getSelectionModel().setSelectedIndex(1);
}
}
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyChar()==(char)38)
{
if(popgraphics.getSelectionModel().getSelectedIndex()==1)
{
popgraphics.getSelectionModel().setSelectedIndex(3);
}
else if(popgraphics.getSelectionModel().getSelectedIndex()==2)
{
popgraphics.getSelectionModel().setSelectedIndex(1);
}
else if(popgraphics.getSelectionModel().getSelectedIndex()==3)
{
popgraphics.getSelectionModel().setSelectedIndex(2);
}
}
if(e.getKeyChar()==(char)40)
{
if(popgraphics.getSelectionModel().getSelectedIndex()==1)
{
popgraphics.getSelectionModel().setSelectedIndex(2);
}
else if(popgraphics.getSelectionModel().getSelectedIndex()==2)
{
popgraphics.getSelectionModel().setSelectedIndex(3);
}
else if(popgraphics.getSelectionModel().getSelectedIndex()==3)
{
popgraphics.getSelectionModel().setSelectedIndex(1);
}
}
}
@Override
public void keyReleased(KeyEvent e) {
}
});
popgraphics.add(p1280);
popgraphics.add(p1536);
popgraphics.add(p1920);
Point p=graghics.getLocation();
popgraphics.setLocation(p);
popgraphics.setBackground(new Color(20,20,20));
popgraphics.setOpaque(false);
如果需要任何其他信息,请告诉我。
这是一个简单的程序,显示带有大图标的 JButton
。 JPopupMenu
连接到 JButton
。 JPopupMenu
包含两个 JMenuItem
,其中每个都有自己独立的图标。 运行 程序。将鼠标指针放在 JButton
上。调出 JPopupMenu
。 (在我的 Windows 10 机器上,我单击鼠标右键显示 JPopupMenu
。)然后按键盘上的向下箭头键。
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
public class PopupTst {
private void createAndDisplayGui() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Icon ico = new ImageIcon("first icon"); // replace with actual path to icon.
JButton button = new JButton(ico);
JPopupMenu popup = new JPopupMenu();
Icon ico2 = new ImageIcon("second icon"); // replace with actual path to icon.
JMenuItem one = new JMenuItem(ico2);
popup.add(one);
Icon ico3 = new ImageIcon("third icon"); // replace with actual path to icon.
JMenuItem two = new JMenuItem(ico3);
popup.add(two);
button.setComponentPopupMenu(popup);
frame.add(button);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
PopupTst instance = new PopupTst();
EventQueue.invokeLater(() -> instance.createAndDisplayGui());
}
}
我有一个 JButton
名为 Graphics
:
当我按下它时,出现一个新的弹出菜单,用户选择屏幕分辨率:
我已将自定义图像添加到菜单项,我无法使用箭头键导航到任何菜单项。
第二个问题是弹出菜单没有固定在框架内,请告诉我如何让它固定在 JFrame
或图形按钮内。
我已经尝试过 KeyListener 但是没有用。
在下面的代码中,我创建了一个 JPopupMenu
并对其进行了初始化,在 KeyListener
函数中,我试图解决导航问题,但没有成功。
popgraphics=new JPopupMenu();
JMenuItem p1920=new JMenuItem();
JMenuItem p1536=new JMenuItem();
JMenuItem p1280=new JMenuItem();
p1920.setIcon(new ImageIcon(getClass().getResource("icons\1920p.png")));
p1536.setIcon(new ImageIcon(getClass().getResource("icons\1536p.png")));
p1280.setIcon(new ImageIcon(getClass().getResource("icons\1280p.png")));
p1920.setBorder(b1);
// p1920.setText("1920x1080 Pixels");
// p1536.setText("1536x1440 Pixels");
// p1280.setText("1280x720 Pixels");
p1920.setFont(new Font("MV Boli",0,14));
p1536.setFont(new Font("MV Boli",0,14));
p1280.setFont(new Font("MV Boli",0,14));
p1920.setOpaque(false);
p1536.setOpaque(false);
p1536.setOpaque(false);
// popgraphics.setSelected(null);
// popgraphics.setSelectionModel(SingleSelectionModel);
popgraphics.addKeyListener(new KeyListener()
{
@Override
public void keyTyped(KeyEvent e) {
if(e.getKeyChar()==(char)38)
{
if(popgraphics.getSelectionModel().getSelectedIndex()==1)
{
popgraphics.getSelectionModel().setSelectedIndex(3);
}
else if(popgraphics.getSelectionModel().getSelectedIndex()==2)
{
popgraphics.getSelectionModel().setSelectedIndex(1);
}
else if(popgraphics.getSelectionModel().getSelectedIndex()==3)
{
popgraphics.getSelectionModel().setSelectedIndex(2);
}
}
if(e.getKeyChar()==(char)40)
{
if(popgraphics.getSelectionModel().getSelectedIndex()==1)
{
popgraphics.getSelectionModel().setSelectedIndex(2);
}
else if(popgraphics.getSelectionModel().getSelectedIndex()==2)
{
popgraphics.getSelectionModel().setSelectedIndex(3);
}
else if(popgraphics.getSelectionModel().getSelectedIndex()==3)
{
popgraphics.getSelectionModel().setSelectedIndex(1);
}
}
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyChar()==(char)38)
{
if(popgraphics.getSelectionModel().getSelectedIndex()==1)
{
popgraphics.getSelectionModel().setSelectedIndex(3);
}
else if(popgraphics.getSelectionModel().getSelectedIndex()==2)
{
popgraphics.getSelectionModel().setSelectedIndex(1);
}
else if(popgraphics.getSelectionModel().getSelectedIndex()==3)
{
popgraphics.getSelectionModel().setSelectedIndex(2);
}
}
if(e.getKeyChar()==(char)40)
{
if(popgraphics.getSelectionModel().getSelectedIndex()==1)
{
popgraphics.getSelectionModel().setSelectedIndex(2);
}
else if(popgraphics.getSelectionModel().getSelectedIndex()==2)
{
popgraphics.getSelectionModel().setSelectedIndex(3);
}
else if(popgraphics.getSelectionModel().getSelectedIndex()==3)
{
popgraphics.getSelectionModel().setSelectedIndex(1);
}
}
}
@Override
public void keyReleased(KeyEvent e) {
}
});
popgraphics.add(p1280);
popgraphics.add(p1536);
popgraphics.add(p1920);
Point p=graghics.getLocation();
popgraphics.setLocation(p);
popgraphics.setBackground(new Color(20,20,20));
popgraphics.setOpaque(false);
如果需要任何其他信息,请告诉我。
这是一个简单的程序,显示带有大图标的 JButton
。 JPopupMenu
连接到 JButton
。 JPopupMenu
包含两个 JMenuItem
,其中每个都有自己独立的图标。 运行 程序。将鼠标指针放在 JButton
上。调出 JPopupMenu
。 (在我的 Windows 10 机器上,我单击鼠标右键显示 JPopupMenu
。)然后按键盘上的向下箭头键。
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
public class PopupTst {
private void createAndDisplayGui() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Icon ico = new ImageIcon("first icon"); // replace with actual path to icon.
JButton button = new JButton(ico);
JPopupMenu popup = new JPopupMenu();
Icon ico2 = new ImageIcon("second icon"); // replace with actual path to icon.
JMenuItem one = new JMenuItem(ico2);
popup.add(one);
Icon ico3 = new ImageIcon("third icon"); // replace with actual path to icon.
JMenuItem two = new JMenuItem(ico3);
popup.add(two);
button.setComponentPopupMenu(popup);
frame.add(button);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
PopupTst instance = new PopupTst();
EventQueue.invokeLater(() -> instance.createAndDisplayGui());
}
}