简体   繁体   English

在JMenuBar中调整JTextField的大小

[英]Resizing JTextField in JMenuBar

I'm trying to add a JTextField as a search bar to a JMenuBar at the top of my JFrame . 我试图将JTextField作为搜索栏添加到JFrame顶部的JMenuBar My problem is that the JTextField keeps getting resized to take up all available space in the JMenuBar , and I don't want it to. 我的问题是JTextField不断调整大小以占据JMenuBar中的所有可用空间,而我不希望这样做。 I've tried setPreferredSize() and setMaximum Size() , but these didnt work, presumably because the LayoutManager used in the JMenuBar doesn't respect these sizes. 我已经尝试过setPreferredSize()setMaximum Size() ,但是它们没有用,大概是因为JMenuBar中使用的LayoutManager不考虑这些大小。 I also tried adding the JTextField to a JPanel with a FlowLayout and adding the panel to the JMenuBar , but I get something that looks like this: 我还尝试将JTextField添加到具有FlowLayoutJPanel并将面板添加到JMenuBar ,但是我得到的东西看起来像这样:

The panel is on the right side of the JMenuBar , and the size seems to be correct, but I can't see anything in it other than this weird blue bar. 该面板位于JMenuBar的右侧,其大小似乎正确,但是除了这个奇怪的蓝色条形之外,我看不到其他任何内容。

Here's the code that (I think) is relevant. 这是(我认为)相关的代码。 Let me know if more is needed: 让我知道是否需要更多:

       JPanel searchPanel = new JPanel();
    searchPanel.setPreferredSize(new Dimension(100, 25));


    JTextField searchBar = new JTextField(50);

    String[] fields = {"title", "author", "subject", "publisher", "year", "circulating", "catalog" };


    JComboBox searchFields = new JComboBox(fields);

    JButton searchBtn = new JButton("search");

    searchPanel.add(searchBar);
    searchPanel.add(searchFields);
    searchPanel.add(searchBtn);
    searchPanel.setVisible(true);

    fileMenu.add(open);
    fileMenu.add(save);
    fileMenu.add(exit);

    libMenu.add(viewLib);
    libMenu.addSeparator();
    libMenu.add(newBook);
    libMenu.add(search);

    this.setJMenuBar(topBar);
       topBar.add(fileMenu);
    topBar.add(libMenu);
    topBar.add(Box.createHorizontalGlue());
   topBar.add(searchPanel);

This works for me. 这对我有用。

menuBar.add(Box.createHorizontalGlue());
JTextField textField = new JTextField(10);
textField.setMaximumSize( textField.getPreferredSize() );
menuBar.add(textField);

Post an SSCCE if you need more help. 如果需要更多帮助,请发布SSCCE。

Edit: 编辑:

Again, the code is posted was just to show that the problem is in containing the maximum size of the text field. 同样,发布该代码只是为了表明问题在于包含文本字段的最大大小。 How you choose to do this is up to you. 您如何选择执行此操作取决于您。

My solution is similar to camickr's, but without the need for setMaximumSize(). 我的解决方案类似于camickr的解决方案,但不需要setMaximumSize()。 Not that I'm against it, but I know there are zealots on SO who swear by "never ever ever ever call setXxxsize() EVER!!!" 并不是说我反对它,但是我知道SO上有一些狂热者,他们发誓“永远不要调用setXxxsize()EVER !!!”! I'm not one of them, but they're out there. 我不是其中之一,但他们在那里。

Anyway, I'd make a JPanel with GridBagLayout, and put the JTextField in it with a fill of NONE, and a Box.createHorizontalGlue() with a fill of HORIZONTAL. 无论如何,我将使用GridBagLayout制作一个JPanel,然后将JTextField填充为NONE,然后将Box.createHorizo​​ntalGlue()填充为HORIZONTAL。 Then, put this panel in your menubar. 然后,将此面板放在菜单栏中。

EDIT: 编辑:

For completeness, here's a solution using a JPanel w/o having to call setMaximumSize(...) 为了完整起见,这是一个使用JPanel的解决方案,无需调用setMaximumSize(...)
(and thus avoid burning in hell for all eternity... according to some): (因此,避免永远在地狱中燃烧……根据某些人的说法):

GridBagConstraints gbc = new GridBagConstraints();
JPanel gbPanel = new JPanel(new GridBagLayout());
gbc.gridx = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
gbPanel.add(Box.createHorizontalGlue(), gbc);
gbc.gridx = 1;
gbc.fill = GridBagConstraints.NONE;
gbc.weightx = 0;
gbPanel.add(new JTextField(10), gbc);
menuBar.add(gbPanel);

Some comments: 一些评论:
1) Man, I forgot how verbose GBL is. 1)老兄,我忘记了GBL的详细程度。 Normally I use a helper class that reduces the code greatly, but I didn't feel like posting it for such a small example. 通常,我使用一个帮助程序类来极大地减少代码,但是我不想在这么小的示例中发布它。

2) If going the panel route, camickr's suggestion (see comment below) of using BorderLayout not only works, but is far simpler code. 2)如果走面板路线,camickr的使用BorderLayout的建议(请参阅下面的注释)不仅有效,而且代码简单得多。

3) Also, as camickr pointed out, using a panel affects the appearance of the "glue" area. 3)同样,正如camickr所指出的,使用面板会影响“胶水”区域的外观。 It gets painted like a JPanel instead of a JMenuBar. 它被绘制为一个JPanel而不是JMenuBar。 Honestly, I didn't even notice it on my machine (using Metal L&F) until he mentioned it, but it IS different and may be an undesireable side-effect. 老实说,直到他提到它之前,我什至没有在我的机器上注意到它(使用Metal L&F),但这是不同的,并且可能是不希望的副作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM