你好,我是一名业余爱好者,想通过编写一个射箭记分卡来学习/提高对Java的理解。我正在尝试制作一个GUI,到目前为止已经成功地在JPanel上生成了一行18个标签,这些标签大小和颜色各异,适合用于计分。
然后,我尝试将其中的五个'标签面板'添加到另一个面板中,以建立一个网格,并避免创建和添加多达150个标签。但是原始标签面板仍未显示出来。所有面板都显示在JFrame上。
我尝试了许多不同的方法来使代码工作,使用Java教程和在互联网上搜索,并研究了本站上类似的问题,但我一直在打转。我肯定错过了某些东西,希望你能帮忙。
我正在使用Java 6和JGrasp v1.8.8_01作为IDE。
以下是标签面板的代码,因为其中许多内容是重复的,所以进行了简化。
import javax.swing.*;
import java.awt.*;
public class ArrowScoreLabels extends JPanel{
public JPanel createContentPane(){
JPanel panelForLabels = new JPanel();
panelForLabels.setLayout(null);
//Code creates 18 labels, sets the size, position, background colours, border and
//font and adds the labels to the’panelForLabels
JLabel scorelabel1;
scorelabel1 = new JLabel("",JLabel.CENTER);
scorelabel1.setBorder(BorderFactory.createLineBorder(Color.black));
scorelabel1.setFont(new Font("Arial", Font.ITALIC, 26));
scorelabel1.setLocation(0, 0);//first value differs for each label
scorelabel1.setSize(35, 35);
scorelabel1.setOpaque(true);
panelForLabels.add(scorelabel1);
panelForLabels.setOpaque(true);
return panelForLabels;
}
}
运行以下类会在面板上显示18个标签。
import javax.swing.*;
import java.awt.*;
public class TestArrowScoreLabels {
private static void createAndShowArrowLabels() {
//Create and set up the window.
JFrame frame = new JFrame("To score one dozen");
//Create and set up the content pane.
ArrowScoreLabels asl = new ArrowScoreLabels();
frame.setContentPane(asl.createContentPane());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(676, 73);
frame.setVisible(true);
}
//Main method to show the GUI/
public static void main(String[] args) {
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
createAndShowArrowLabels();
}
});
}
}
第二个面板的代码类似,可以编译,但只显示第二个绿色的JPanel,而不是带有标签的面板。
import javax.swing.*;
import java.awt.*;
public class FiveDozenScorePanel{
public JPanel createContentPane(){
//A bottom JPanel on which to place five dozenpanels.
JPanel fivedozenpanel = new JPanel();
fivedozenpanel.setLayout(null); //requires absolute spacing
fivedozenpanel.setSize(676,185);
fivedozenpanel.setBackground(Color.green);
//Label panels for five dozen
ArrowScoreLabels dozenscorepanel1, dozenscorepanel2,
dozenscorepanel3,dozenscorepanel4,dozenscorepanel5;
//Create the 5 dozenscorelabels.
dozenscorepanel1 = new ArrowScoreLabels();
dozenscorepanel1.setLocation(5,5);//y value changes for each panel
fivedozenpanel.add(dozenscorepanel1);//plus the other 4
fivedozenpanel.setOpaque(true);
return fivedozenpanel;
}
private static void createAndShowDozenPanels() {
JFrame frame = new JFrame("To score five dozen");
FiveDozenScorePanel fdsp = new FiveDozenScorePanel();
frame.setContentPane(fdsp.createContentPane());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Display the window
frame.setSize(700, 233);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
createAndShowDozenPanels();
}
});
}
}
我也尝试过frame.getContentPane().add(fdsp); - frame.pack();并阅读了很多关于绘制方法的内容,但我完全感到困惑。我可以让ArrowScoreLabels图片直接显示在JFrame上而不是JPanel上,但只能显示其中一个,而不是五个。
我希望能指出正确的方向。谢谢你的时间。
更新-2010年12月14日 我已经成功地在JFrame的另一个JPanel上显示了panelForLabels Jpanel。这是通过将以下代码添加到ArrowScoreLabels类中完成的。原始的createContentPane()方法被重命名为createRowOne()。panelForLabels被涂成红色,fivedozen面板被涂成黄色以确定哪个正在显示。然而,尽管进行了大量的实验和研究,我仍然只能让程序显示一行标签。
public static JPanel createContentPane(){
//Bottom panelto hold rows of labels
JPanel fivedozenscorepanel = new JPanel();
fivedozenscorepanel.setLayout(null);//requires absolute spacing
fivedozenscorepanel.setSize(660,180);
fivedozenscorepanel.setBackground(Color.yellow);
fivedozenscorepanel.add(createRowOne());
fivedozenscorepanel.setOpaque(true);
return fivedozenscorepanel;
}
我展示5行18个标签的唯一方法是在ArrowScoreLabels类中创建所有90个标签,然后使用绝对间距将它们添加到一个JPanel中,最后添加到JFrame中。
我已经注意到了pstanton的建议 - 谢谢您的建议 - 我正在研究使用MigLayout管理器。
setVisible(true);
吗? - user489041