JFrame添加文本不工作.出现两个GUI



我正在尝试创建一个简单的JFrame菜单,其中有一个很好的背景,可以工作,以及一些文本。这篇文章有一点错误。下面是它的样子(https://i.stack.imgur.com/yZHUe.jpg)

如您所见,只有一行文本出现,而不是第二行。

与此同时。当我运行程序时,出现了两个GUI。一个是完全空的,一个看起来像上面的图片。

最后我不能使用方法logo.setHorizontalAlignment(JLabel.NORTH);没有得到一个错误,和其他一些一样。我测试和工作的两个只有。center和。left。

任何帮助将是伟大的!

哦,差点忘了,这是我的代码:)

    package character;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
/**
 * Created by Niknea on 6/28/14.
 */
public class characterSelector extends JFrame{

    JPanel cselectorText;
    JFrame  cselectorButtons;
    JLabel logo, characterName, label;
    JButton previous, next;

    public characterSelector(String title){
        super(title);
        this.createCharacterSelector();
        this.setSize(1920, 1033);
        this.setResizable(true);
        this.setVisible(true);
    }

    public void createCharacterSelector(){
      try {
            label = new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("/resources/Grass_Background.jpg"))));

            cselectorButtons = new JFrame();
            logo = new JLabel("SoccerKidz [REPLACE W/ COOL LOGO]");
            characterName = new JLabel("<Character Name>");
            logo.setPreferredSize(new Dimension(50, 50));

            logo.setFont(new Font(logo.getFont().getName(), Font.HANGING_BASELINE, 50));
            characterName.setFont(new Font(characterName.getFont().getName(), Font.HANGING_BASELINE, 50));
            cselectorButtons.add(logo);
            cselectorButtons.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            cselectorButtons.setContentPane(label);
            cselectorButtons.setLayout(new BorderLayout());
            characterName.setForeground(Color.CYAN);
            characterName.setHorizontalAlignment(JLabel.CENTER);
            cselectorButtons.add(characterName);
          logo.setForeground(Color.CYAN);
          logo.setHorizontalAlignment(JLabel.LEFT);
            cselectorButtons.add(logo);
            cselectorButtons.pack();
            cselectorButtons.setLocationRelativeTo(null);
            cselectorButtons.setVisible(true);
        } catch (IOException exp) {
            exp.printStackTrace();
        }
    }
}

再次感谢!:)

如您所见,只有一行文本出现,而不是第二行。

设置cselectorButtons布局为BorderLayout。然后加入JLabel("<Character Name>");,没问题。然后加入JLabel("SoccerKidz [REPLACE W/ COOL LOGO]");。事情是这样的。对于BorderLayout,当您只使用add(component)时,通常您希望指定BorderLayout.[POSITION]作为add的第二个参数。如果不这样做,则默认情况下,添加的每个组件(没有指定位置)都将隐式添加到BorderLayout.CENTER中。这样做的问题是每个位置只能有一个组件。因此,在您的例子中,第一个标签被踢出中心,只显示您添加的第二个标签。

当我运行程序时,出现了两个GUI。一个是完全空的,一个看起来像上面的图片。

看看你的代码。你的类是一个JFrame,其中你没有添加任何东西,this.setVisible(true)。还有一个JFrame cselectorButtons;,你可以在其中添加组件,还有cselectorButtons.setVisible(true);。你能猜出你看到的那个不是空的吗?不要扩展JFrame。请使用当前使用的实例。

最后我不能使用方法logo.setHorizontalAlignment(JLabel.NORTH);没有得到一个错误。

看一下JLabel的API并不需要花太多的时间。话虽如此,让你想到水平对齐的东西,应该包括一个向北定位的选项。没有意义
  • public void setHorizontalAlignment(int alignment)

    设置标签内容沿X轴的对齐方式。

    参数:alignment - SwingConstants中定义的以下常量之一: LEFT CENTER (纯图像标签的默认值),RIGHT, LEADING(纯文本标签的默认值)或TRAILING


我建议你看看布局组件在一个容器和视觉指南布局管理器的其他可能的布局管理器,你可以使用,如果BorderLayout不适合你。还要记住,你可以用不同的布局管理器嵌套不同的面板来获得你想要的结果。但首先你需要了解它们是如何工作的。


我做了一些改变,这是有效的。我也在看你的粘贴代码,并不能真正看到任何差异。您可能想调查我的代码,尝试寻找差异

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
/**
 * Created by Niknea on 6/28/14.
 */
public class CharacterSelector {
    JPanel cselectorText;
    JFrame cselectorButtons;
    JLabel logo, characterName, label;
    JButton previous, next;
    public CharacterSelector() {
        createCharacterSelector();
    }
    public void createCharacterSelector() {
        try {
            label = new JLabel(new ImageIcon(ImageIO.read(getClass()
                    .getResource("/resources/Grass_Background.jpg"))));
            cselectorButtons = new JFrame();
            logo = new JLabel("SoccerKidz [REPLACE W/ COOL LOGO]");
            characterName = new JLabel("<Character Name>");
            logo.setPreferredSize(new Dimension(50, 50));
            logo.setFont(new Font(logo.getFont().getName(),
                    Font.HANGING_BASELINE, 50));
            characterName.setFont(new Font(characterName.getFont().getName(),
                    Font.HANGING_BASELINE, 50));
            cselectorButtons.add(logo);
            cselectorButtons.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            cselectorButtons.setContentPane(label);
            cselectorButtons.setLayout(new BorderLayout());
            characterName.setForeground(Color.CYAN);
            characterName.setHorizontalAlignment(JLabel.CENTER);
            cselectorButtons.add(characterName);
            logo.setForeground(Color.CYAN);
            logo.setHorizontalAlignment(JLabel.LEFT);
            cselectorButtons.add(logo, BorderLayout.NORTH);
            cselectorButtons.pack();
            cselectorButtons.setLocationRelativeTo(null);
            cselectorButtons.setVisible(true);
        } catch (IOException exp) {
            exp.printStackTrace();
        }
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new CharacterSelector();
            }
        });
    }
}

最新更新