2013-10-21 11 views
7

Tôi có mã làm việc này cho đồng hồ bấm giờ trong Swing. Tôi muốn có nhãn Time Remaining 300 seconds tập trung vào dòng thứ hai`. đây là mã của tôi. enter image description herelàm thế nào để trung tâm JLabel trong Jframe Swing?

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); 

    } 

} 

EDIT: thay đổi thành điều này.

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

cho ouput khác nhau, vui lòng xem hình ảnh

enter image description here

Trả lời

12

Thay đổi liên kết ngang của JLabel của bạn. Các nhà xây dựng có thể giúp bạn thực hiện điều này bằng cách thay đổi:

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

tới:

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

Cũng tổ JPanels của bạn mỗi sử dụng bố trí riêng của mình. ví dụ:

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(); 
     } 
     }); 
    } 
} 
+0

@ user1988876: sau đó sử dụng kết hợp các trình quản lý bố cục tốt hơn. GridLayout của bạn sẽ ngăn chặn việc định tâm. Thay vào đó, JPanel của bạn sử dụng BorderLayout. Xem xét làm tổ JPanels. –

+0

Tôi bắt đầu với 'BorderLayout', nhưng điều đó có vẻ giống như một spoiler tổng số –

+0

@ user1988876:' "total spoiler" '??? Điều đó cho tôi ít manh mối về những gì sai với nó. Một lần nữa, bạn có thể không làm tổ của bạn JPanels và do đó làm tổ bố trí. –

2

Đọc JavaDoc of JLabel. Nó có mô tả cho lớp và tất cả các API, bao gồm cả căn chỉnh văn bản.

5

bạn có thể sử dụng để tập trung theo chiều ngang

labelVariable.setHorizontalAlignment(JLabel.CENTER); 
+0

Đây là câu trả lời hay, vì nó cũng hoạt động nếu 'JLabel' là riêng tư. +1 cho điều đó :) – CodingNinja

2

Bạn có thể sử dụng sau đây

jLabelvariable.setLocation((this.getWidth()-jLabelvariable.getWidth())/2,50); 

đây 50 là chiều cao của nhãn từ trên cùng của khung.

Các vấn đề liên quan