gpt4 book ai didi

java - LookAndFeel 行为之间的区别

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:13:06 27 4
gpt4 key购买 nike

我创建了一个 JPopupmenu 并添加了一个 JTextField。当我使用 metal 或 nimbus 时,一切都很好。问题是当我将 LookAndFeel 切换到 Windows 时。我不能按右 ALT,因为如果我按这个键,JPopupmenu 将隐藏。

我可以使用右 ALT 在 Windows LookAndFeel 中书写国家标志吗?

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

public class Popup extends JFrame {

JPopupMenu popup;
JPanel panel;
JTextField field;

public Popup(){
setSize(500,400);
try {
//UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (ClassNotFoundException | InstantiationException
| IllegalAccessException | UnsupportedLookAndFeelException e1) {
e1.printStackTrace();
}
SwingUtilities.updateComponentTreeUI(this);

popup = new JPopupMenu();
field = new JTextField(10);
popup.add(field);
JButton button = new JButton("Options");
button.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
popup.show(e.getComponent(), e.getX(), e.getY());
}
});

panel = new JPanel();
panel.add(button);
add(panel);
}

public static void main(String[] args){
Popup pop = new Popup();
pop.setVisible(true);
}

}

最佳答案

JPopupMenu 有一组非常具体的操作要求,是的,它们确实在外观和感觉之间发生变化,这就是重点。

您可以使用未修饰的 JFrame 创建您自己的弹出窗口。这里的技巧是根据需要模拟尽可能多的弹出窗口,例如,当另一个组件获得焦点时自动关闭,使用转义键关闭弹出窗口的能力......等等......

这只是一个提供概念验证的简单示例,我个人还会为转义键添加一个键绑定(bind),某种允许搜索 Pane 请求关闭弹出窗口的监听器界面以及能够当窗口可见时自动聚焦某些组件,但那只是我......

Popup

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestPopup {

public static void main(String[] args) {
new TestPopup();
}

public TestPopup() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

private JButton show;

public TestPane() {
setLayout(new GridBagLayout());
show = new JButton("...");
show.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
PopupWindow window = new PopupWindow();
window.show(show, 0, show.getHeight());
}
});
add(show);
}

}

public class SearchPane extends JPanel {

private JList list;
private JTextField search;

public SearchPane() {
setLayout(new BorderLayout());
list = new JList();
list.setPrototypeCellValue("This is just a test");
list.setVisibleRowCount(20);
search = new JTextField(10);
add(new JScrollPane(list));
add(search, BorderLayout.SOUTH);
}

}

public class PopupWindow extends JFrame {

private SearchPane searchPane;

public PopupWindow() {
setUndecorated(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
addWindowFocusListener(new WindowFocusListener() {

@Override
public void windowGainedFocus(WindowEvent e) {
}

@Override
public void windowLostFocus(WindowEvent e) {
dispose();
}
});
searchPane = new SearchPane();
add(searchPane);
pack();
}

public void show(JComponent parent, int x, int y) {
Point point = new Point(x, y);
SwingUtilities.convertPointToScreen(point, parent);
setLocation(point);
setVisible(true);
}

}

}

关于java - LookAndFeel 行为之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24619960/

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