java - 更改字体时 JTextField 大小发生变化

标签 java swing fonts jtextfield flowlayout

当我更改 JTextField 的字体时,实际的 JTextField 会调整大小,有什么帮助吗?

我正在开发一个计算器,显然根据代码的糟糕程度,你可以看出我是一个菜鸟

任何帮助将不胜感激

代码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;

import javax.swing.JButton;
import javax.swing.JFrame;
javax.swing.JTextField;



  public class Ken {

static JTextField text = new JTextField("0",37);



public static void frame(){
    JFrame frame = new JFrame("MyCalc");
    frame.setSize(460, 600);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setResizable(false);
    FlowLayout fl = new FlowLayout(0, 30, 20);
    setLayout(fl);

    frame.getContentPane().setLayout(fl);
    Container con = frame.getContentPane();
    con.setBackground(Color.LIGHT_GRAY);
    Font f = new Font("MS UI Gothic", Font.BOLD, 40);
    Font f2 = new Font("Engravers MT", Font.PLAIN, 40);
    int bHeight = 80;
    int bWidth = 70;
    text.setPreferredSize(new Dimension(bHeight, bWidth));
    text.setBackground(Color.GREEN);
    text.setFont(f2);


    frame.add(text, BorderLayout.NORTH);

    JButton oneButton = new JButton("1");
    oneButton.setPreferredSize(new Dimension(bHeight, bWidth));
    oneButton.setBackground(Color.DARK_GRAY);
    oneButton.setForeground(Color.GREEN);
    oneButton.setFont(f);
    frame.add(oneButton);

    JButton twoButton = new JButton("2");
    twoButton.setPreferredSize(new Dimension(bHeight, bWidth));
    twoButton.setBackground(Color.DARK_GRAY);
    twoButton.setForeground(Color.GREEN);
    twoButton.setFont(f);
    frame.add(twoButton);

    JButton threeButton = new JButton("3");
    threeButton.setPreferredSize(new Dimension(bHeight, bWidth));
    threeButton.setBackground(Color.DARK_GRAY);
    threeButton.setForeground(Color.GREEN);
    threeButton.setFont(f);
    frame.add(threeButton);

    JButton plusButton = new JButton("+");
    plusButton.setPreferredSize(new Dimension(bHeight, bWidth));
    plusButton.setBackground(Color.DARK_GRAY);
    plusButton.setForeground(Color.GREEN);
    plusButton.setFont(f);
    frame.add(plusButton);

    JButton fourButton = new JButton("4");
    fourButton.setPreferredSize(new Dimension(bHeight, bWidth));
    fourButton.setBackground(Color.DARK_GRAY);
    fourButton.setForeground(Color.GREEN);
    fourButton.setFont(f);
    frame.add(fourButton);

    JButton fiveButton = new JButton("5");
    fiveButton.setPreferredSize(new Dimension(bHeight, bWidth));
    fiveButton.setBackground(Color.DARK_GRAY);
    fiveButton.setForeground(Color.GREEN);
    fiveButton.setFont(f);
    frame.add(fiveButton);

    JButton sixButton = new JButton("6");
    sixButton.setPreferredSize(new Dimension(bHeight, bWidth));
    sixButton.setBackground(Color.DARK_GRAY);
    sixButton.setForeground(Color.GREEN);
    sixButton.setFont(f);
    frame.add(sixButton);

    JButton minusButton = new JButton("-");
    minusButton.setPreferredSize(new Dimension(bHeight, bWidth));
    minusButton.setBackground(Color.DARK_GRAY);
    minusButton.setForeground(Color.GREEN);
    minusButton.setFont(f);
    frame.add(minusButton);

    JButton sevenButton = new JButton("7");
    sevenButton.setPreferredSize(new Dimension(bHeight, bWidth));
    sevenButton.setBackground(Color.DARK_GRAY);
    sevenButton.setForeground(Color.GREEN);
    sevenButton.setFont(f);
    frame.add(sevenButton);

    JButton eightButton = new JButton("8");
    eightButton.setPreferredSize(new Dimension(bHeight, bWidth));
    eightButton.setBackground(Color.DARK_GRAY);
    eightButton.setForeground(Color.GREEN);
    eightButton.setFont(f);
    frame.add(eightButton);

    JButton nineButton = new JButton("9");
    nineButton.setPreferredSize(new Dimension(bHeight,bWidth));
    nineButton.setBackground(Color.DARK_GRAY);
    nineButton.setForeground(Color.GREEN);
    nineButton.setFont(f);
    frame.add(nineButton);

    JButton timesButton = new JButton("*");
    timesButton.setPreferredSize(new Dimension(bHeight, bWidth));
    timesButton.setBackground(Color.DARK_GRAY);
    timesButton.setForeground(Color.GREEN);
    timesButton.setFont(f);
    frame.add(timesButton);

    JButton zeroButton = new JButton("0");
    zeroButton.setPreferredSize(new Dimension(bHeight, bWidth));
    zeroButton.setBackground(Color.DARK_GRAY);
    zeroButton.setForeground(Color.GREEN);
    zeroButton.setFont(f);
    frame.add(zeroButton);

    JButton enterButton = new JButton("=");
    enterButton.setPreferredSize(new Dimension(190, bWidth));
    enterButton.setBackground(Color.DARK_GRAY);
    enterButton.setForeground(Color.GREEN);
    enterButton.setFont(f);
    frame.add(enterButton);

    JButton dividButton = new JButton("/");
    dividButton.setPreferredSize(new Dimension(bHeight, bWidth));
    dividButton.setBackground(Color.DARK_GRAY);
    dividButton.setForeground(Color.GREEN);
    dividButton.setFont(f);
    frame.add(dividButton);

    frame.setComponentOrientation(
            ComponentOrientation.LEFT_TO_RIGHT);
}



private static Dimension Dimension(int bHeight, int bWidth) {
    // TODO Auto-generated method stub
    return null;
}



private static void setLayout(FlowLayout fl) {
    // TODO Auto-generated method stub

}



public static void main(String[] args){
    frame();
}
}

最佳答案

When I Change The Font Of My JTextField The Actual JTextField Resizes

是的,这是 Swing 组件所期望的行为。当属性更改时,它们会自动调整自身大小。这是一件好事,您应该保留默认行为。也就是说,您不应该尝试使用 setPreferredSize() 方法手动设置组件的大小。

static JTextField text = new JTextField("0",37);

问题是您指定的列大小太大。列大小是字符数,而不是像素数。让它变得更合理:

static JTextField text = new JTextField("0",10);

关于java - 更改字体时 JTextField 大小发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17266461/

相关文章:

Java - 使用 iBatis 从数据库检索大量数据

java - 在 android 中膨胀类 textview 时出错

ios - 为 UITextView 调整FontSizeToFitWidth

java - 如何让 jslider 在滑动时真正影响事物?

多个字符集中的 CSS 字体堆栈名称

java - 无法读取 *.ttf 文件

java - YARN(Hadoop)中的容器分配代码

java - 如何使用默认访问(或包默认)访问构造函数

java - JFrame 在 Thread.sleep(int) 期间处于非 Activity 状态

java - 在Swing中,如何将计算后的结果自动加载到框架中?