Java Netbeans IDE |在自定义标题栏上居中标题文本
我正在创建一个自定义标题栏,但问题是我无法使文本集中在JFrame上,因为我在Pntile中有3个按钮。 我试图将按钮拖放到JLabel内部,但它仍然在JLabel外部。如何做到这一点Java Netbeans IDE |在自定义标题栏上居中标题文本,java,netbeans,ide,titlebar,Java,Netbeans,Ide,Titlebar,我正在创建一个自定义标题栏,但问题是我无法使文本集中在JFrame上,因为我在Pntile中有3个按钮。 我试图将按钮拖放到JLabel内部,但它仍然在JLabel外部。如何做到这一点 尝试使用边框布局,按钮位于东部,标题位于中部 希望有帮助 --更新---- 以下是我试图分享的代码片段: import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayo
尝试使用边框布局,按钮位于东部,标题位于中部 希望有帮助 --更新---- 以下是我试图分享的代码片段:
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);
}
}
输出屏幕截图:
免责声明:这是一个巨大的欺骗
因此,本例基本上利用了GridBagLayout
,这将允许“title”标签占据容器中可用空间的整个宽度,并允许按钮同时沿右边缘布置
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);
}
}
}
文本仍然会被右侧按钮的宽度偏移确保东西方向具有相同的宽度,这将确保文本是中心的否,它不会,文本将“显示”偏离中心,因为它被限制在中心面板的可用空间内,这将是父容器的宽度减去east组件的宽度,OP最终将没有更多的内容。我尝试了这个方法(east上有3个按钮,中间有标签),但现在它只显示1个按钮。是的,标签宽度仍然没有完全调整到JPANELA使用上述布局后,您可以使用JLabel作为JLabel title=new JLabel(title“”,SwingConstants.CENTER);然后是title.setVerticalAlignment(SwingConstants.CENTER);这有点棘手,因为您需要允许标签占据整个宽度,但仍然要让按钮显示在右侧-我个人的方法可能是使用
GridBagLayout
先生,您能告诉我更多细节吗?我试图将Pntile布局更改为GridBagLayout,但没有任何更改,仍然是一样的