如何在Jframe Swing中居JLabel? [英] how to center JLabel in Jframe Swing?

查看:139
本文介绍了如何在Jframe Swing中居JLabel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个用于Swing秒表的工作代码。我希望标签 Time Remaining 300秒以第二行为中心。这是我的代码。

I have this working code for a stop watch in Swing. I want to have the label Time Remaining 300 seconds centered on the second line`. here is my code.

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

    }

}

编辑:
更改为此。

changing to this.

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

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

gives different ouput, kindly see the image

推荐答案

更改JLabel的水平对齐方式。构造函数可以通过更改来帮助您完成此操作:

Change your JLabel's horizontal alignment. The constructor can help you do this by changing:

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

to:

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

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

Also nest your JPanels each using its own layout. e.g.,

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

这篇关于如何在Jframe Swing中居JLabel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆