JFrame添加文本无法正常工作.出现两个GUI [英] JFrame adding Text not working. Two GUI appear

查看:69
本文介绍了JFrame添加文本无法正常工作.出现两个GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的JFrame菜单,其中有一个很好的背景,并且带有一些文本.文字有点马车.外观如下( http://imgur.com/nRpzA30 )

I am trying to create a simple JFrame menu where there is a nice background, which works, along with some text. The text is a bit buggy. Here is how it looks (http://imgur.com/nRpzA30)

如您所见,文本的仅一行显示而不是第二行.

As you can see, only one line of the text appears not the second line.

与此相关.当我运行该程序时,将出现两个GUI.一个是完全空的,一个看起来像上面的图片.

Along with that. Two GUI's appear when I run the program. One is completley empty, and one looks like the picture above.

最后我不能使用logo.setHorizo​​ntalAlignment(JLabel.NORTH);方法.不会出错,其他几个也一样.我测试和工作的两个只是.CENTER和.LEFT.

Finally I can't use the method logo.setHorizontalAlignment(JLabel.NORTH); without getting an error, same with a few others. The two I tested and worked were only .CENTER, and .LEFT.

任何帮助都会很棒!

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

Oh and almost forgot, here's my code :)

    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();
        }
    }
}

再次感谢! :)

推荐答案

如您所见,文本的仅一行显示而不是第二行.

As you can see, only one line of the text appears not the second line.

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

You set the layout of cselectorButtons to BorderLayout. Then you add JLabel("<Character Name>");, which is fine. But then you add JLabel("SoccerKidz [REPLACE W/ COOL LOGO]");. Here's what's happening. With BorderLayout, when you just add(component), generally you want to specify a BorderLayout.[POSITION] as the second argument to the add. If you don't, every component that you add, without the position specified, will be added the the BorderLayout.CENTER implicitly by default. The problem with this is each position can only have one component. So in your case, the first label gets kicked out the center, only showing the second one you added.

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

Two GUI's appear when I run the program. One is completley empty, and one looks like the picture above.

查看代码时.您的班级是JFrame,您在其中没有添加任何内容,而this.setVisible(true).还有一个JFrame cselectorButtons;,您可以在其中添加 以及cselectorButtons.setVisible(true);.您能猜出哪一个不是空的吗?不要扩展JFrame.只需使用您当前正在使用的实例即可.

When look at your code. Your class is a JFrame in which you add nothing to, and this.setVisible(true). And also you a JFrame cselectorButtons; in which you do add components and also cselectorButtons.setVisible(true);. Can you guess which one is the one you're seeing that's not empty. Don't extends JFrame. Just use the instance one you currently are using.

最后我不能在没有错误的情况下使用方法logo.setHorizontalAlignment(JLabel.NORTH);.

看看

It doesn't take much to take a look at the API for JLabel. That being said, what makes you think aligning something horizontally, should include an option to position to the north. Doesn't make sense

  • public void setHorizontalAlignment(int alignment)

设置标签内容在X轴上的对齐方式.

Sets the alignment of the label's contents along the X axis.

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

Parameters: alignment - One of the following constants defined in SwingConstants: LEFT, CENTER (the default for image-only labels), RIGHT, LEADING (the default for text-only labels) or TRAILING.


我建议您看看在容器内布置组件布局管理器的可视指南如果BorderLayout不适合您,则可以使用布局管理器.另外请记住,您可以将不同的面板与不同的布局管理器嵌套在一起以获得所需的结果.但是首先,您需要学习它们的工作原理.


I suggest you take a look at Layout out Components Within a Container and A Visual Guide to Layout Managers for other possibly layout managers you can use, if BorderLayout doesn't suit you. Also keep in mind you can nest different panels with different layout managers to get your desired result. But first you need to learn how they work.

更新

我进行了一些更改,这是可行的.我也在查看您的pastebin代码,看不到任何区别.您可能需要调查我的代码,以尝试查找差异

I made some changes, which works. I'm also looking at your pastebin code, and can't really see any difference. You may want to investigate my code to try and look for differences

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();
            }
        });
    }
}

这篇关于JFrame添加文本无法正常工作.出现两个GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆