gpt4 book ai didi

java - 如何在 Jframe Swing 中将 JLabel 居中?

转载 作者:搜寻专家 更新时间:2023-10-30 21:15:48 24 4
gpt4 key购买 nike

我有这个 Swing 秒表的工作代码。我想让标签 Time Remaining 300 seconds 居中在第二行`。这是我的代码。 enter image description here

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

public class TimerGUI extends JPanel {
JLabel promptLabel, timerLabel;
final int count = 30;
JButton start;
JButton end;
Timer timer;

public TimerGUI() {
setLayout(new GridLayout(0,2));

start = new JButton("Start");
add(start);
Event e = new Event();
start.addActionListener(e);

end = new JButton("End");
end.setEnabled(false);
add(end);
end.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
timer.stop();
start.setEnabled(true);
end.setEnabled(false);
timerLabel.setText("Time Remaining " + count + " seconds");

}

});

timerLabel = new JLabel("Time Remaining 300 seconds");
add(timerLabel);

}

private class Event implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {
start.setEnabled(false);
end.setEnabled(true);
timerLabel.setText("Time Remaining " + count + " seconds");

TimeClass tc = new TimeClass(count);
timer = new Timer(1000, tc);
timer.start();

}
}

private class TimeClass implements ActionListener {
int counter;

public TimeClass(int count) {
this.counter = count;

}

@Override
public void actionPerformed(ActionEvent e) {
counter--;
if (counter <= 5) {
Toolkit.getDefaultToolkit().beep();
}

if (counter >= 1) {
timerLabel.setText("Time Remaining " + counter + " seconds");
} else {
timer.stop();
timerLabel.setText("game over");
}

}
}

public static void main(String[] args) {
JFrame myframe = new JFrame();
TimerGUI timer = new TimerGUI();
// myframe.getContentPane().add(content,BorderLayout.CENTER);
myframe.getContentPane().add(timer, BorderLayout.CENTER);
myframe.setTitle("Hangman Game");
myframe.pack();
myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myframe.setLocationRelativeTo(null);
myframe.setVisible(true);

}

}

编辑:改成这个。

timerLabel = new JLabel("Time Remaining 300 seconds");
add(timerLabel,SwingConstants.CENTER);

给出不同的输出,请看图片

enter image description here

最佳答案

更改 JLabel 的水平对齐方式。构造函数可以通过更改帮助您做到这一点:

timerLabel = new JLabel("Time Remaining 300 seconds");

到:

timerLabel = new JLabel("Time Remaining 300 seconds", SwingConstants.CENTER);

还可以嵌套您的 JPanel,每个 JPanel 使用自己的布局。例如,

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.*;

public class TimerFoo extends JPanel {
public TimerFoo() {
JPanel centerPanel = new JPanel(new GridLayout(0, 2));
centerPanel.add(new JButton("Foo"));
centerPanel.add(new JButton("Bar"));

JLabel bottomLabel = new JLabel("Bottom Label", SwingConstants.CENTER);

int gap = 5;
setLayout(new BorderLayout(gap, gap));
setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
add(centerPanel, BorderLayout.CENTER);
add(bottomLabel, BorderLayout.PAGE_END);
}

private static void createAndShowGui() {
JFrame frame = new JFrame("TimerFoo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new TimerFoo());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

关于java - 如何在 Jframe Swing 中将 JLabel 居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19506769/

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