2

我正在尝试设置 a 的高度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();
            }
        });
    }
}
4

1 回答 1

1

正如@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();
            }
        });
    }
}
于 2013-02-06T17:15:38.963 回答