我試圖設置JTextField
的高度。目前它的顯示尺寸爲JFrame
。任何想法如何將高度設置爲50?在BorderLayout中更改/設置JTextField的高度
編輯:這裏是修改後的代碼以及截圖謝謝!
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();
}
});
}
}
不要使用邊界佈局 – MadProgrammer
爲更好地幫助直接把文本字段到一個容器越早發佈的[SSCCE(http://sscce.org/),請有沒有想法'1)'什麼DrawCanvas(); '2)'爲什麼你在這裏設置'BorderLayout'的原因'3)'沒有閱讀關於'LayoutManagers'的錯誤教程 – mKorbel
錯誤的問題 - 你不設置組件的尺寸([甚至沒有它的尺寸提示, PREF/MAXSIZE](http://stackoverflow.com/a/7229519/203657))。相反,請使用合適的LayoutManager,f.i.尊重組件的prefSize總是像FlowLayout一樣。或換句話說:你需要了解所有關於LayoutManagers :-) – kleopatra