我一直试图将这种边框布局设置好几个小时。我在Java网站上查了How to Use BorderLayout,但我还没有得到它。
我已将下面的代码包含在内以供审核,以便更轻松地了解我如何尝试使用BorderLayout功能。
如何在JL3上设置边框布局?
class GameStructure {
private String []wordList = {"computer","java","activity","alaska","appearance","article",
"automobile","basket","birthday","canada","central","character","chicken","chosen",
"cutting","daily","darkness","diagram","disappear","driving","effort","establish","exact",
"establishment","fifteen","football","foreign","frequently","frighten","function","gradually",
"hurried","identity","importance","impossible","invented","italian","journey","lincoln",
"london","massage","minerals","outer","paint","particles","personal","physical","progress",
"quarter","recognise","replace","rhythm","situation","slightly","steady","stepped",
"strike","successful","sudden","terrible","traffic","unusual","volume","yesterday"};
private JTextField tf;
private JLabel jl2;
public void window() {
ImageIcon ic = new ImageIcon("hangman.png");
JFrame gameFrame = new JFrame();
JPanel jp = new JPanel();
JPanel jpLets = new JPanel();
JPanel jpBlank = new JPanel();
JPanel imgPane = new JPanel();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
panel2.setLayout(new BorderLayout());
jpLets.setLayout(new BoxLayout(jpLets, BoxLayout.Y_AXIS));
panel1.setLayout(new BorderLayout());
panel1.setOpaque(false);//!!
//jp.setBorder(BorderFactory.createTitledBorder(""));
tf = new JTextField(1);
JLabel img = new JLabel(ic, JLabel.CENTER);
JLabel jl = new JLabel("Enter a letter", JLabel.CENTER);
jl2 = new JLabel("Letters used: ", JLabel.CENTER);
JLabel jl3 = new JLabel("__ ", JLabel.CENTER);
jl.setFont(new Font("Rockwell", Font.PLAIN, 20));
tf.setFont(new Font("Rockwell", Font.PLAIN, 20));
jl2.setFont(new Font("Rockwell", Font.PLAIN, 20));
imgPane.add(img, BorderLayout.CENTER);
jp.add(jl);
jp.add(tf);
jpLets.add(jl2);
jpBlank.add(jl3);
//jpMain.add(imgPane, BorderLayout.CENTER);
panel1.add(jp, BorderLayout.NORTH);
panel1.add(jpLets, BorderLayout.SOUTH);
panel1.add(imgPane);
panel2.add(panel1, BorderLayout.CENTER);
panel2.add(jpBlank, BorderLayout.PAGE_END);
gameFrame.setTitle("Hangman");
gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameFrame.setIconImage(
new ImageIcon("Hangman-Game-grey.png").getImage());
gameFrame.setResizable(false);
gameFrame.add(panel2);
gameFrame.setSize(600, 600);
gameFrame.setLocationRelativeTo(null);
gameFrame.setVisible(true);
int j = 0;
int []length = new int[64];
for(j = 0; j<64; j++) {
length[j] = wordList[j].length();//gets length of words in wordList
}//end for
int l = 0;
String line = "";
//create line first then put into .setText
for(int m = 0; m<length[l]; m++) {
line += "__ ";
l++;
}//end for
jl3.setText(line);
tf.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {//when enter key pressed
JTextField tf = (JTextField)e.getSource();
String letter = tf.getText();
jl2.setText(jl2.getText() + letter + " ");//sets jlabel text to users entered letter
}//end actionPerformed method
});
}//end userInput method
}
public class GameMain {
public static void main(String[] args) {
GameStructure game = new GameStructure();
game.window();
}
}
答案 0 :(得分:2)
首先,让我赞扬你实际上看了我昨天与你联系的教程,并努力学习:)
这是我看到的一些事情。
在这里,您尝试将BorderLayout用于JLabel。通常,您希望将LayoutManagers与容器一起使用(JPanels,JFrames,JDialogs,JApplets)。摆脱下面的代码。
JLabel img = new JLabel(ic, JLabel.CENTER);
img.setLayout( new BorderLayout() );
同样在这里,你所有的jp.add()
。
jp.add(img, BorderLayout.CENTER);
jp.add(jl);
jp.add(tf);
jp.add(jl2);
....
我假设您要使用BorderLayout,但没有为jp
面板指定BorderLayout。默认情况下,如果您未指定LayoutManager,JPanel将为您提供FlowLayout
JPanel jp = new JPanel();
请注意,当您将BorderLayout
分配给容器时,您需要确保添加到其中的每个组件都指定了布局位置。不应该多次使用任何职位。此外,通常应始终使用BorderLayout.CENTER
。 (例如,如果面板中只有两个组件,请使用BorderLayout.SOUTH
,BorderLayout.CENTER
)。
此外,您应该学会使用多个面板嵌套布局以获得所需的结果。这是一个简单的例子。假设您有两个按钮和一个图像标签
JLabel label = new JLabel(new ImageIcon(filePath));
JButton jbt1 = new JButton("jbt1");
JButton jbt2 = new JButton("jbt2");
我希望布局看起来像这样
+--------------------------------+
| |
| |
| Image Label |
| |
+----------------+---------------+
| jbt1 | jbt2 |
+----------------+---------------+
我想要做的第一件事是将按钮添加到单独的JPanel。然后将标签包装在JPanel中(不是必需的,但我喜欢使用面板);
JPanel buttonsPanel = new JPanel(); <-- FlowLayout (none specified)
buttonsPanel.add(jbt1);
buttonsPanel.add(jbt2);
JPanel labelPanel = new JPanle(); <-- FlowLayout
labelPabel.add(label);
然后我将用另一个带有BorderLayout
的面板包裹两个面板JPanel panel1 = new JPanel(new BorderLayout());
panel1.add(buttonPanel, BorderLayout.SOUTH);
panel1.add(labelPanel, BorderLayout.CENTER);
所以我的最后一个小组看起来像这样
panel1
+--------------------------------+
| |
| | BorderLayout.CENTER
| Image Label |
| |
+----------------+---------------+
| jbt1 | jbt2 | BorderLayout.SOUTH
+----------------+---------------+
假设您想出了另一个想要添加的面板。您想将此新面板添加到我们刚创建的面板的左侧。我们现在只需要使用我们创建的panel1
面板,并将该面板和新面板包装在另一个面板中
JPanel newPanel = new JPanel();
JPanel panel2 new JPanel(new BorderLayout());
panel2.add(newPanel, BorderLayout.WEST);
panel2.add(panel1, BorderLayout.CENTER);
panel2
+---------------+--------------------------------+
| | |
| | |
| | Image Label |
| newPanel | |
| +----------------+---------------+
| | jbt1 | jbt2 |
+---------------+----------------+---------------+
了解布局和面板的效果。这是处理布局组件的好方法
答案 1 :(得分:0)
您没有将jp添加到gameFrame和jp2。无论哪种方式你添加许多组件到jp面板,它没有设置为BorderLayout(因此,默认情况下它设置为FlowLayout)然后你只需将它添加到jp2,它设置为BorderLayout,但只有两个组件
因此,尝试在这两条(现有)行之间添加以下行:
gameFrame.setResizable(false);
gameFrame.add(jp);
gameFrame.pack();