gpt4 book ai didi

java - 边框布局不起作用

转载 作者:行者123 更新时间:2023-11-30 09:13:57 25 4
gpt4 key购买 nike

几个小时以来,我一直在尝试设置此边框布局。我查过How to Use BorderLayout在 Java 网站上,但我还没有得到它。

我在下面包含了我的代码以供审查,以便更容易地了解我是如何尝试使用 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();
}
}

最佳答案

首先,让我表扬你真的看了我昨天链接给你的那个教程并努力学习:)

这是我看到的几件事。

在这里,您尝试对 JLabel 使用 BorderLayout。通常,您希望将 LayoutManagers 与容器(JPanel、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.SOUTHBorderLayout.CENTER)。

此外,您应该学习将 Layout 与多个 Panel 嵌套以获得您想要的结果。这是一个简单的例子。假设您有两个按钮和一个图像标签

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 |
+---------------+----------------+---------------+

查看布局和面板的情况。这是处理布局组件的好方法

关于java - 边框布局不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20735601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com