无法与中心对齐

时间:2014-01-17 21:14:05

标签: java swing

我无法让测试器与屏幕中心对齐,我不明白发生了什么 我已经尝试了我所知道的每种布局格式,但无法使其工作 如果它是面板上唯一的东西并且我使用FlowLayout,则计时器位于屏幕的中心,但我不能简单地添加按钮,因为我希望它位于计时器之下。

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class TestBomb extends JFrame implements ActionListener {
/**
 * 
 */
private static final long serialVersionUID = 1L;
private static final int WIDTH = 1000;
private static final int HEIGHT = 600;
private String armPassword,disarmPassword;
private Timer timer = null;
private int heldTime=0;
private int mins;
private Timer hold=null;
private int secs;
private JPanel timerSpot=null;
private int totalSeconds;
private Container content=null;
private JTextField type;
private JTextField theTimer;
private JButton defuse=null;
@SuppressWarnings("unused")

public TestBomb() {
    setSize(WIDTH, HEIGHT);
    setLocationRelativeTo(null);
    addWindowListener(new WindowDestroyer());
    content = this.getContentPane();
    content.setBackground(Color.black);
    type = new JTextField(25);
    type.setActionCommand("Enter");
    type.setForeground(Color.green);
    type.setBackground(Color.black);
    type.addActionListener(this);
    armPassword = null;//Method implemented later
    disarmPassword=null;//Method implemented later
    JPanel passwordPanel = new JPanel(new FlowLayout());
    JLabel passwordSpot = new JLabel("Arm Password: "+armPassword);
    passwordSpot.setForeground(Color.white);
    passwordSpot.setSize(50, 30);
    JLabel disarmPasswordSpot = new JLabel("Disarm Password: "+disarmPassword);
    disarmPasswordSpot.setForeground(Color.white);
    disarmPasswordSpot.setSize(50, 30);
    passwordPanel.add(passwordSpot);
    passwordPanel.add(type);
    passwordPanel.add(disarmPasswordSpot);
    passwordPanel.setBackground(new Color(0, 51, 0));
    defuse=new JButton("Hold to Defuse");
    defuse.addActionListener(this);
    defuse.setVisible(true);
    defuse.setAlignmentX(CENTER_ALIGNMENT);
    content.add(passwordPanel, "South");
    theTimer = new JTextField();
    theTimer.setText("00" + ":" + "00");
    theTimer.setFont(new Font("Arial Black", Font.PLAIN, 235));
    theTimer.setBackground(Color.black);
    theTimer.setForeground(Color.green);
    theTimer.setBorder(BorderFactory.createEmptyBorder());
    theTimer.setEditable(true);
    theTimer.setAlignmentX(CENTER_ALIGNMENT);
    timerSpot = new JPanel();
    timerSpot.setLayout(new BoxLayout(timerSpot,BoxLayout.Y_AXIS));
    timerSpot.setBackground(Color.black);
    timerSpot.add(theTimer);
    timerSpot.add(defuse);
    content.add(timerSpot, "Center");
}
public static void main(String[] args) {
    TestBomb timer = new TestBomb();
    timer.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub

}
}

以下是用于关闭窗口的类

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class WindowDestroyer extends WindowAdapter 
{
     public void windowClosing(WindowEvent e) 
     {
               System.exit(0); 
     } 
}

2 个答案:

答案 0 :(得分:0)

要解决我的问题,我改为使用FlowLayout()并将Box.createHorizontalGlue(1000)添加到代码中

答案 1 :(得分:0)

这可以解决您的问题,但您必须使用正确的Layout Managers

只需创建另一个JPanel作为主面板,它将添加到contentPane,如:

JPanel timer=new JPanel();

然后将jTextField和JButton添加到timerSpot面板

timerSpot.add(theTimer);
timerSpot.add(defuse);

然后将timerSpot面板添加到计时器面板,如:

timer.add(timerSpot);

现在将计时器面板添加到contentPane,这会将您的组件定位到中心

content.add(timer, "Center");