Java秋千菜单,弹出式,Combobox GUI问题
当我单击在框架或对话框边界外扩展的任何弹出菜单时,无论是从组合还是菜单,这都没关系,我的整个GUI都会很时髦。它开始复制面板,但仅在视觉上。一切都可以使用,只是一团糟。我将程序恢复到裸露的骨头,1帧,2个按钮,一个菜单和1个对话框。我收缩了框架,因此当我单击菜单时,弹出窗口向下延伸过框架的底部,单击Menuitem,该Menuitem带有一个带有几个按钮的对话框。第一次很好,但是第二次发生后事情很奇怪。如果弹出窗口留在框架内,一切正常。请告诉我其他人会遇到这一点。任何信息都将不胜感激。 Google对此一无所知:)
我删除了所有不必要的进口,我确保所有GUI创建/编辑都在EDT上,我分解了扩展课程,仅使用基本课程,尝试隐藏和重复使用对话框,我尝试处置,我发现这个uimanager.put(“ popupmenu.consumeeventonclose”,false),它没有多大意义,但无论如何我都尝试过,没有什么可用。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Test implements ActionListener
{
JFrame frame;
JDialog dialog;
public Test()
{
int width = 336, height = 225;
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
this.frame = new JFrame();
this.frame.setTitle("Frame");
this.frame.setLocation((dim.width - width) / 2, (dim.height - height) / 2);
this.frame.setSize(width, height);
JMenuBar menuBar = new JMenuBar();
JMenu tools = new JMenu("Tools");
JMenuItem settings = new JMenuItem("Settings");
settings.addActionListener(this);
tools.add(settings);
menuBar.add(tools);
this.frame.setJMenuBar(menuBar);
JPanel root = (JPanel)this.frame.getContentPane();
root.setLayout(new BorderLayout());
JPanel flow = new JPanel();
JPanel center = new JPanel(new GridLayout(1, 2));
JLabel label = new JLabel("Username: ");
center.add(label);
JTextField field = new JTextField("", 10);
center.add(field);
flow.add(center);
root.add(flow);
JPanel buttonPanel = new JPanel();
JButton cancel = new JButton(" Cancel ");
cancel.addActionListener(this);
buttonPanel.add(cancel);
root.add(buttonPanel, BorderLayout.SOUTH);
this.frame.setVisible(true);
}
public static void main ( String[] args )
{
new Test();
}
@Override
public void actionPerformed(ActionEvent e)
{
switch(e.getActionCommand())
{
case " Cancel ":
{
this.frame.dispose();
System.exit(0);
}
case "Settings":
{
if (this.dialog == null)
{
this.dialog = new JDialog(this.frame, true);
this.dialog.setTitle("Settings");
this.dialog.setSize(400, 300);
this.dialog.setLocationRelativeTo(this.frame);
this.dialog.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
JPanel sRoot = (JPanel)this.dialog.getContentPane();
sRoot.setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
JButton close = new JButton(" Close ");
close.addActionListener(this);
buttonPanel.add(close);
sRoot.add(buttonPanel, BorderLayout.SOUTH);
}
this.dialog.setVisible(true);
break;
}
case " Close ":
{
this.dialog.setVisible(false);
break;
}
default:
{
break;
}
}
}
}
只需缩小主帧即可,设置菜单即可超越框架的底部(可能必须两次打开并关闭“设置”对话框)
然后关闭“设置”对话框,扩展主帧并悬停在按钮和按钮上,然后/或Textfield。
它在我身上毫无失败。
When I click on any popup menu that extends outside the bounds of my frame or dialog, whether it's from a combobox or a menu it doesn't matter, my entire gui goes funky. It starts replicating panels but only visually. Everything remains usable just hidden behind a mess. I've reverted my program to bare bones, 1 frame, 2 buttons, a menu, and 1 dialog. I shrink the frame so when I click the menu the popup extends down past the bottom of the frame, click the menuitem which brings up a dialog with a couple of buttons. The first time is fine but after the second time things go very weird. Everything works perfectly fine if the popup stays within the frame. Please tell me other people experience this. Any info would be appreciated. Google knows nothing about this btw :)
I removed all unnecessary imports, I made sure all gui creation/editing is on EDT, I broke down extended classes and use only base classes, I tried hiding and reusing dialogs, I tried disposing, I found this UIManager.put("PopupMenu.consumeEventOnClose", false) which didn't make much sense but I tried it anyway, nothing worked.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Test implements ActionListener
{
JFrame frame;
JDialog dialog;
public Test()
{
int width = 336, height = 225;
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
this.frame = new JFrame();
this.frame.setTitle("Frame");
this.frame.setLocation((dim.width - width) / 2, (dim.height - height) / 2);
this.frame.setSize(width, height);
JMenuBar menuBar = new JMenuBar();
JMenu tools = new JMenu("Tools");
JMenuItem settings = new JMenuItem("Settings");
settings.addActionListener(this);
tools.add(settings);
menuBar.add(tools);
this.frame.setJMenuBar(menuBar);
JPanel root = (JPanel)this.frame.getContentPane();
root.setLayout(new BorderLayout());
JPanel flow = new JPanel();
JPanel center = new JPanel(new GridLayout(1, 2));
JLabel label = new JLabel("Username: ");
center.add(label);
JTextField field = new JTextField("", 10);
center.add(field);
flow.add(center);
root.add(flow);
JPanel buttonPanel = new JPanel();
JButton cancel = new JButton(" Cancel ");
cancel.addActionListener(this);
buttonPanel.add(cancel);
root.add(buttonPanel, BorderLayout.SOUTH);
this.frame.setVisible(true);
}
public static void main ( String[] args )
{
new Test();
}
@Override
public void actionPerformed(ActionEvent e)
{
switch(e.getActionCommand())
{
case " Cancel ":
{
this.frame.dispose();
System.exit(0);
}
case "Settings":
{
if (this.dialog == null)
{
this.dialog = new JDialog(this.frame, true);
this.dialog.setTitle("Settings");
this.dialog.setSize(400, 300);
this.dialog.setLocationRelativeTo(this.frame);
this.dialog.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
JPanel sRoot = (JPanel)this.dialog.getContentPane();
sRoot.setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
JButton close = new JButton(" Close ");
close.addActionListener(this);
buttonPanel.add(close);
sRoot.add(buttonPanel, BorderLayout.SOUTH);
}
this.dialog.setVisible(true);
break;
}
case " Close ":
{
this.dialog.setVisible(false);
break;
}
default:
{
break;
}
}
}
}
Just shrink the main frame so the settings menu expands past the bottom of the frame (might have to open and close the settings dialog twice)
Then close the settings dialog, expand the main frame and hover over the button and/or the textfield.
It happens to me without fail.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论