【发布时间】:2014-08-19 17:52:31
【问题描述】:
我正在尝试创建一个简单的 JFrame 菜单,其中有一个漂亮的背景,它可以与一些文本一起使用。文字有点乱。这是它的外观 (http://imgur.com/nRpzA30)
如您所见,文本只出现了一行,没有出现第二行。
随之而来。运行程序时出现两个 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();
}
}
}
再次感谢! :)
【问题讨论】:
-
您正在从 JFrame 类内部创建一个 JFrame。
cselectorButtons = new JFrame();在class characterSelector extends JFrame内。 -
那么我将如何修复它?我不能让它为空。
-
我不确定您到底想达到什么目标,因此不可能给出具体答案。从您在 JFrame 中有一个 JFrame 的事实来看,您最好的选择是考虑为您想要的内容创建单独的 JPanel,并将它们全部组合到一个 JFrame 中。
标签: java swing user-interface jframe jlabel