为什么赢了';这不是我的标签中心吗? 当我按下相应的按钮时,我试图让我的标签在窗口的中心,但是它只是坐在按钮的顶部而不是坐在中间,我不知道为什么 import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.*; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class navigator extends JFrame { Container con; public navigator(){ super("JFrame"); JFrame newFrame = new JFrame("Navigator"); newFrame.setSize(400, 400); newFrame.setVisible(true); newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); con = getContentPane(); BorderLayout newLayout = new BorderLayout(); con.setLayout(newLayout); JButton newButton = new JButton("Up"); newFrame.add(newButton, BorderLayout.NORTH); JButton newButton2 = new JButton("Left"); newFrame.add(newButton2, BorderLayout.WEST); JButton newButton3 = new JButton("Down"); newFrame.add(newButton3, BorderLayout.SOUTH); JButton newButton4 = new JButton("Right"); newFrame.add(newButton4, BorderLayout.EAST); JLabel newLabel = new JLabel("Going up!"); newFrame.add(newLabel, BorderLayout.CENTER); newLabel.setVisible(false); newButton.add(newLabel); JLabel newLabel2 = new JLabel("Going left!"); newFrame.add(newLabel2, BorderLayout.CENTER); newLabel2.setVisible(false); newButton2.add(newLabel2); JLabel newLabel3 = new JLabel("Going down!"); newFrame.add(newLabel3, BorderLayout.CENTER); newLabel3.setVisible(false); newButton3.add(newLabel3); JLabel newLabel4 = new JLabel("Going right!"); newFrame.add(newLabel4, BorderLayout.CENTER); newLabel4.setVisible(false); newButton4.add(newLabel4); newButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { newLabel.setVisible(true); newLabel2.setVisible(false); newLabel3.setVisible(false); newLabel4.setVisible(false); } }); newButton2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { newLabel2.setVisible(true); newLabel.setVisible(false); newLabel3.setVisible(false); newLabel4.setVisible(false); } }); newButton3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { newLabel3.setVisible(true); newLabel2.setVisible(false); newLabel.setVisible(false); newLabel4.setVisible(false); } }); newButton4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { newLabel4.setVisible(true); newLabel2.setVisible(false); newLabel3.setVisible(false); newLabel.setVisible(false); } }); } public static void main(String[] args){ navigator myNavigator = new navigator(); } }

为什么赢了';这不是我的标签中心吗? 当我按下相应的按钮时,我试图让我的标签在窗口的中心,但是它只是坐在按钮的顶部而不是坐在中间,我不知道为什么 import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.*; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class navigator extends JFrame { Container con; public navigator(){ super("JFrame"); JFrame newFrame = new JFrame("Navigator"); newFrame.setSize(400, 400); newFrame.setVisible(true); newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); con = getContentPane(); BorderLayout newLayout = new BorderLayout(); con.setLayout(newLayout); JButton newButton = new JButton("Up"); newFrame.add(newButton, BorderLayout.NORTH); JButton newButton2 = new JButton("Left"); newFrame.add(newButton2, BorderLayout.WEST); JButton newButton3 = new JButton("Down"); newFrame.add(newButton3, BorderLayout.SOUTH); JButton newButton4 = new JButton("Right"); newFrame.add(newButton4, BorderLayout.EAST); JLabel newLabel = new JLabel("Going up!"); newFrame.add(newLabel, BorderLayout.CENTER); newLabel.setVisible(false); newButton.add(newLabel); JLabel newLabel2 = new JLabel("Going left!"); newFrame.add(newLabel2, BorderLayout.CENTER); newLabel2.setVisible(false); newButton2.add(newLabel2); JLabel newLabel3 = new JLabel("Going down!"); newFrame.add(newLabel3, BorderLayout.CENTER); newLabel3.setVisible(false); newButton3.add(newLabel3); JLabel newLabel4 = new JLabel("Going right!"); newFrame.add(newLabel4, BorderLayout.CENTER); newLabel4.setVisible(false); newButton4.add(newLabel4); newButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { newLabel.setVisible(true); newLabel2.setVisible(false); newLabel3.setVisible(false); newLabel4.setVisible(false); } }); newButton2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { newLabel2.setVisible(true); newLabel.setVisible(false); newLabel3.setVisible(false); newLabel4.setVisible(false); } }); newButton3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { newLabel3.setVisible(true); newLabel2.setVisible(false); newLabel.setVisible(false); newLabel4.setVisible(false); } }); newButton4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { newLabel4.setVisible(true); newLabel2.setVisible(false); newLabel3.setVisible(false); newLabel.setVisible(false); } }); } public static void main(String[] args){ navigator myNavigator = new navigator(); } },java,swing,Java,Swing,组件只能有一个父级。因此,无法将标签添加到框架和按钮。我甚至不知道你为什么要尝试将标签添加到按钮 在任何情况下,都不能将四个标签添加到框架的中心。BorderLayout在每个区域中只允许一个组件,因此只有最后添加的组件才会可见。BorderLayout将仅设置最后添加的按钮的大小。所有其他按钮的大小均为(0,0),因此无需绘制任何内容 因此,只需添加一个标签,然后使用ActionListener中的setText(…)方法更改文本 然而,一旦你解决了这个问题,你仍然会有一个问题。默认情况下,标

组件只能有一个父级。因此,无法将标签添加到框架和按钮。我甚至不知道你为什么要尝试将标签添加到按钮

在任何情况下,都不能将四个标签添加到框架的中心。BorderLayout在每个区域中只允许一个组件,因此只有最后添加的组件才会可见。BorderLayout将仅设置最后添加的按钮的大小。所有其他按钮的大小均为(0,0),因此无需绘制任何内容

因此,只需添加一个标签,然后使用ActionListener中的
setText(…)
方法更改文本

然而,一旦你解决了这个问题,你仍然会有一个问题。默认情况下,标签绘制在标签可用空间的左侧

如果希望文本显示在中间,则需要使用:

label.setHorizontalAlignment(JLabel.CENTER);
此外,在使框架可见之前,应将所有组件添加到框架中


最后,类名应该以大写字符开头。查看JDK API的类名并遵循其使用的约定。

使用BorderLayout时,每个部分只能放置一个控件。例如,你们只能在中间放一个按钮


如果你想把更多的东西放在一个区域,那么你需要创建一个新的JPanel,把它放在中间,然后把按钮放在新创建的JPanel上。(当然要遵循相同的布局规则)。您可以递归地添加任意数量的JPanel。

欢迎使用Stack Overflow
BorderLayout.center
不代表您认为它的功能。你读了吗?@Jarrod Roberson它运行良好,但我犯了一个逻辑错误,我只是不确定出了什么问题。这是我得到的输出。我做了一个面板,把标签贴在上面,但现在它们出现在顶部。我还尝试创建一个按钮,将其可见性设置为false,并在其上添加标签,但只会“向右走!”出现。此外,这就是我在顶部出现的意思。这很有效,非常感谢:D。我中途意识到我也没有理由在按钮上添加标签,这是一个愚蠢的错误。谢谢你的帮助!
label.setHorizontalAlignment(JLabel.CENTER);