更改/设置BorderLayout中JTextField的高度

时间:2013-02-06 11:42:24

标签: java swing height jtextfield border-layout

我正在尝试设置JTextField的高度。目前显示的是JFrame的完整大小。我怎么设置高度到50?

编辑:这是修改后的代码以及屏幕截图谢谢!

ps.png

public class Display extends JFrame {

    private DrawCanvas canvas;
    private JTextField Altitude;
    private JTextField TASpeed;
    private JLabel altButton;
    private int countA = 0;
    private int countS = 0;
    private int Bcount1 = 0;
    public String Ccount = Integer.toString(Bcount1);

    public Display() {
        canvas = new DrawCanvas();
        canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
        canvas.setLayout(new BorderLayout());
        Container cp = getContentPane();
        cp.setLayout(new BorderLayout());
        cp.add(canvas, BorderLayout.LINE_START);

        //here are the 2 side fields![enter image description here][2]
        Altitude = new JTextField("0", 5);
        Altitude.setHorizontalAlignment(JTextField.CENTER);
        Altitude.setEditable(false);
        Altitude.setOpaque(false);
        Altitude.setFont(Altitude.getFont().deriveFont(25f));

        TASpeed = new JTextField("0", 5);
        TASpeed.setHorizontalAlignment(JTextField.CENTER);
        TASpeed.setEditable(false);
        TASpeed.setOpaque(false);
        TASpeed.setFont(Altitude.getFont().deriveFont(25f));

        altButton = new JLabel();
        altButton.setText(Ccount);

        canvas.add(altButton, BorderLayout.SOUTH);

        canvas.add(Altitude, BorderLayout.LINE_END);
        canvas.add(TASpeed, BorderLayout.LINE_START);

        canvas.add(new JLabel(Ccount), BorderLayout.SOUTH);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("FLIGHT DISPLAY");
        pack();
        setVisible(true);
        requestFocus();
    }

    class DrawCanvas extends JPanel {

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            setBackground(CANVAS_BACKGROUND);

            g.setColor(GROUND_COLOR);
            g.drawString(Ccount, 100, 100);

        }
    }

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

            public void run() {
                new Display();
            }
        });
    }
}

1 个答案:

答案 0 :(得分:1)

正如@kleopatra正确评论,JTextField可以根据平台设计者选择的Font来计算自己的首选大小。此示例更改了大小。

image

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

/** @see http://stackoverflow.com/a/14734937/230513 */
public class Test {

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JTextField tfCount = new JTextField("42", 8);
        tfCount.setHorizontalAlignment(JTextField.CENTER);
        tfCount.setFont(tfCount.getFont().deriveFont(50f));
        f.add(tfCount, BorderLayout.NORTH);
        f.add(new JPanel() { //placeholder

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(320, 240);
            }
        }, BorderLayout.CENTER);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Test().display();
            }
        });
    }
}