繁体   English   中英

在BorderLayout中更改/设置JTextField的高度

[英]Changing/setting the height of a JTextField in a BorderLayout

我正在尝试设置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();
            }
        });
    }
}

当@kleopatra正确注释时, JTextField可以根据平台设计人员对Font的选择来计算其自身的首选大小。 本示例更改大小。

图片

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM