我被允许在JFrame
放置一个手表和一个按钮。当全屏显示时,手表没有来到中心。
怎么能把时钟放在中心呢?
我的来源
package clock;
import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;
import javax.swing.*;
public class FullScreenExample
{
static class GameFrame extends JFrame //
{
private static final GraphicsDevice gd = (GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices())[0];
private static final boolean FULLSCREEN_SUPPORTED = gd.isFullScreenSupported();
private static final String MAKE_FULLSCREEN_TEXT = "Full Screen";
private static final String MAKE_WINDOWED_TEXT = "Window";
private static final int WINDOWED_WIDTH = 400;
private static final int WINDOWED_HEIGHT = 300;
JPanel clock = new Clock();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
private final JButton fsButton = new JButton(MAKE_FULLSCREEN_TEXT);
private boolean isFullscreen = false;
public GameFrame(String title)
{
super(title);
setSize(WINDOWED_WIDTH, WINDOWED_HEIGHT);
setLocation((screenSize.width - 300) / 2, (screenSize.height -300)/2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initComponents();
}
public void initComponents()
{
if(FULLSCREEN_SUPPORTED)
{
fsButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
toggleFullscreen();
}
});
add(fsButton,BorderLayout.NORTH);
add(clock);
}
else
{
add(new JLabel("full screen X"));
}
}
public void toggleFullscreen()
{
isFullscreen = !isFullscreen;
setVisible(false);
dispose();
setUndecorated(isFullscreen);
if(isFullscreen)
{
fsButton.setText(MAKE_WINDOWED_TEXT);
gd.setFullScreenWindow(this);
clock.setLocation((screenSize.width) / 2, (screenSize.height )/2);
validate();
}
else
{
fsButton.setText(MAKE_FULLSCREEN_TEXT);
gd.setFullScreenWindow(null);
setVisible(true);
}
}
}
static class Game
{
private GameFrame gameFrame; //
public Game(String title)
{
gameFrame = new GameFrame(title); //
}
public void start()
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
getGameFrame().setVisible(true);
}
});
}
public GameFrame getGameFrame() //
{
return gameFrame;
}
}
public static void main(String[] args)
{
Game g = new Game("Clock");
g.start();
}
}
class Clock extends JPanel{
private JTextField timeField;
private Timer t;
public Clock() {
timeField = new JTextField(9);
timeField.setEditable(false);
timeField.setFont(new Font("sansserif", Font.PLAIN,50));
add(timeField);
t= new Timer(1000, new ClockListener());
t.start();
}
private class ClockListener implements ActionListener{
public void actionPerformed(ActionEvent arg0){
Calendar time = Calendar.getInstance();
int hour = time.get(Calendar.HOUR);
int amPm = time.get(Calendar.AM_PM);
String strAmPm = null;
if(amPm == Calendar.AM)
strAmPm = " AM";
else
strAmPm = " PM";
int min = time.get(Calendar.MINUTE);
int sec = time.get(Calendar.SECOND);
timeField.setText(strAmPm + " " + hour + " : " + min + " : " + sec);
}
}
}
答案 0 :(得分:0)
默认情况下,JPanel将使用FlowLayout。只要它们适合,这种布局基本上只是并排堆叠组件。您想将布局设置为BorderLayout。这就是你实现的方法:
setLayout(new BorderLayout());
您尝试将fsButton设置为BorderLayout.NORTH,但您尚未设置布局。添加时钟时需要使用BorderLayout.CENTER。这显然会使它居中。这就是它的样子:
add(clock, BorderLayout.CENTER);
感谢您的阅读!希望这会有所帮助。