gpt4 book ai didi

java - 想要将 JPanel 与 Java 中框架的左侧对齐

转载 作者:行者123 更新时间:2023-12-01 19:05:00 25 4
gpt4 key购买 nike

事实证明这比我想象的要困难得多。我想做的就是将这个 JPanel“粘”到框架的左侧。这是我到目前为止所拥有的。 (如果可能的话,我不想使用 GridBagLayout (我是一个 java 新手)。

labels = new JPanel();
JLabel currentWordLA = new JLabel("Current word:");
JLabel triedLettersLA = new JLabel("Tried letters:");
JLabel triesLeftLA = new JLabel("Tries remaining:");
JButton restart = new JButton("Reset");

labels.setLayout(new BoxLayout(labels, BoxLayout.Y_AXIS));
labels.add(currentWordLA);
labels.add(triedLettersLA);
labels.add(triesLeftLA);
scorePanel.add(labels, BorderLayout.WEST);

编辑:抱歉大家,这是完整的代码。它有点像框架中的面板中的面板。只是想让框架向左对齐。谢谢!

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class Hangman {

JFrame frame;
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 String mysteryWord;
private boolean finished = false;
private boolean won = false;
private Button a[];

public static void main(String[] args) {
Hangman gui = new Hangman();
gui.go();
}

class myDrawPanel extends JPanel {

public void paintComponent(Graphics g) {
setBackground(Color.white);
g.setColor(Color.gray);
g.fillRect(50, 200, 150, 20);
g.fillRect(90, 20, 10, 200);
g.fillRect(90, 20, 60, 10);
g.setColor(Color.black);
g.fillRect(145, 20, 5, 25);
}
}

public void go() {
frame = new JFrame("Hangman");
JPanel topPanel = new JPanel();
myDrawPanel noosePanel = new myDrawPanel();
JPanel bottomPanel = new JPanel();
JPanel scorePanel = new JPanel();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(2, 0));
bottomPanel.setLayout(new GridLayout(0, 2));
scorePanel.setSize(20, 100);

noosePanel.setBorder(BorderFactory.createTitledBorder("Your progress."));
topPanel.setBorder(BorderFactory.createTitledBorder("Your arsenal."));
scorePanel.setBorder(BorderFactory.createTitledBorder("Your score."));
frame.add(topPanel);
frame.add(bottomPanel);
bottomPanel.add(scorePanel);
bottomPanel.add(noosePanel);

//The Stats section.
JPanel labels = new JPanel();
JLabel currentWordLA = new JLabel("Current word:");
JLabel triedLettersLA = new JLabel("Tried letters:");
JLabel triesLeftLA = new JLabel("Tries remaining:");
JButton restart = new JButton("Reset");

labels.setLayout(new BoxLayout(labels, BoxLayout.Y_AXIS));
labels.add(currentWordLA);
labels.add(triedLettersLA);
labels.add(triesLeftLA);
scorePanel.add(labels, BorderLayout.WEST);

int i;
StringBuffer buffer;
a = new Button[26];
topPanel.setLayout(new GridLayout(4, 0, 10, 10));

// create all 26 buttons
for (i = 0; i < 26; i++) {
buffer = new StringBuffer();
buffer.append((char) (i + 65));
a[i] = new Button(buffer.toString());
a[i].setSize(100, 100);
//a[i].addActionListener( this );
topPanel.add(a[i]);
}

frame.setSize(500, 500);
frame.setResizable(false);
frame.setVisible(true);
}
}

最佳答案

尝试向 BorderLayout.CENTER 添加一些内容,如下所示:

    scorepanel.add(new JPanel(), BorderLayout.CENTER); //I guess scorepanel is your frame
//your code here

关于java - 想要将 JPanel 与 Java 中框架的左侧对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10467696/

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