2013-06-24 2 views
0

내 JTextField의 글꼴을 변경하면 실제 JTextField는 모든 도움말의 크기를 변경합니까?JTextField 크기가 글꼴을 바꿀 때 변경됨

나는 코드가 당신은 내가 어떤 도움을 주시면 감사하겠습니다

멍청한 놈 오전 말할 수 있습니다 얼마나 나쁜지에 따라 분명히 계산기에 노력하고 있어요

CODE :

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

그리고 귀하의 질문은 무엇입니까? –

+0

@camickr 어쨌든 나는 그것을 바꿀 수 있습니까? –

+0

질문은 "왜 하시겠습니까?" – MadProgrammer

답변

4

내 JTextField의 글꼴을 변경하는 경우 실제 JTextField 크기 조정

예 이것은 Swing 구성 요소의 바람직한 동작입니다. 속성이 변경되면 자동으로 크기가 조정됩니다. 이것은 좋은 일이며 기본 동작을 그대로 두어야합니다. 즉, setPreferredSize() 메서드를 사용하여 구성 요소의 크기를 수동으로 설정하지 않아야합니다.

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

너무 큰 열 크기를 지정했기 때문에 문제가 발생했습니다. 열 크기는 픽셀이 아니라 문자 수입니다. 좀 더 합리적으로 만드십시오 :

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

이 좋았습니다. 고맙습니다. –

관련 문제