【问题标题】:Netbeans IDE | Centered TitleText on Custom TitleBarNetbeans IDE |自定义标题栏上的居中 TitleText
【发布时间】:2018-03-23 03:33:47
【问题描述】:

我正在创建一个自定义标题栏,但问题是我无法使文本以 JFrame 为中心,因为我在 pnlTitle 中有 3 个按钮。 我试图将按钮拖放到 JLabel 内部,但它仍在 JLabel 外部。如何做到这一点?

【问题讨论】:

  • 这有点棘手,因为您需要让标签占据整个宽度,但仍然让按钮出现在右侧 - 我个人的方法可能是使用GridBagLayout
  • 您能告诉我更多关于它的细节吗?我试图将 pnlTitle Layout 更改为 GridBagLayout 但没有任何改变,它仍然是一样的

标签: java netbeans ide titlebar


【解决方案1】:

免责声明:这是一个巨大的作弊......

所以,这个例子基本上利用了GridBagLayout,这将允许“标题”标签占据容器中可用空间的整个宽度,并允许按钮同时沿右边缘布置

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Test {

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

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 1;
            gbc.weighty = 1;
            gbc.fill = GridBagConstraints.BOTH;
            // This is a space to force the buttons
            // to the right edge.
            // You might be able to play around with the constraints
            // for b1 to basically get the same affect
            add(new JLabel(), gbc);
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            JLabel label = new JLabel("This is the title");
            label.setHorizontalAlignment(JLabel.CENTER);
            add(label, gbc);

            gbc = new GridBagConstraints();
            gbc.gridx = 1;
            gbc.gridy = 0;
            add(new JButton("B1"), gbc);
            gbc.gridx++;
            add(new JButton("B2"), gbc);
            gbc.gridx++;
            add(new JButton("B3"), gbc);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(600, 44);
        }

    }

}

【讨论】:

    【解决方案2】:

    尝试使用按钮在东边,标题在中心的边框布局。

    希望对您有所帮助。

    --更新了----

    这是我试图分享的片段代码:

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingConstants;
    
    public class TestSwing {
    
    public static void main(String...s) {
    
        JFrame jframe = new JFrame();
        JPanel heading = new JPanel();
        JPanel buttons = new JPanel();
        JButton button1 = new JButton("Button1");
        JButton button2 = new JButton("Button2");
        JButton button3 = new JButton("Button3");
        button1.setPreferredSize(new Dimension(40, 40));
        button2.setPreferredSize(new Dimension(40, 40));
        button3.setPreferredSize(new Dimension(40, 40));
        buttons.setLayout(new GridLayout(1,3));
        buttons.add(button1);
        buttons.add(button2);
        buttons.add(button3);
        heading.setLayout(new BorderLayout());
        JLabel title = new JLabel("Title",SwingConstants.CENTER);
        title.setVerticalAlignment(SwingConstants.CENTER);
        heading.add(buttons,BorderLayout.EAST);
        jframe.setLayout(new BorderLayout());
        jframe.add(heading,BorderLayout.NORTH);
        JPanel blankPanel = new JPanel();
        blankPanel.setPreferredSize(new Dimension(120,40));
        heading.add(blankPanel,BorderLayout.WEST);
        heading.add(title,BorderLayout.CENTER);
        jframe.setVisible(true);
    
    }
    }
    

    输出截图:

    【讨论】:

    • 而且文字仍然会被右边按钮的宽度偏移
    • 保证东西宽度一致,这样可以保证文字居中
    • 不,它不会,文本将“出现”偏离中心,因为它受限于中心面板的可用空间,即父容器的宽度减去东部的宽度组件,OP 最终将只剩下他们已经拥有的东西
    • 我试过这个(东边 3 个按钮,中心标签),但现在它只显示 1 个按钮。是的,标签宽度仍然没有完全调整到 JPanel
    • 使用上述布局后,可以使用JLabel作为JLabel title = new JLabel(TITLE"", SwingConstants.CENTER);然后是 title.setVerticalAlignment(SwingConstants.CENTER);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-16
    • 1970-01-01
    • 2020-09-24
    • 2019-01-05
    • 2011-03-10
    相关资源
    最近更新 更多