我有得到這個代碼正常運行的問題,增加字體大小顯示短語。它編譯並且最初框架顯示正確。問題是,當我通過最大化或拖動框架的一側來手動重新調整框架大小時,文本消失。我正在使用jGRASP,不確定這是否是問題。代碼似乎對我有意義,就像我說的那樣,它編譯(我知道並不一定是正確的)。我仍然是一個新手,所以如果任何人都能指出我正確的方向,我會非常感激。JAVA,在一幀
import javax.swing.*;
import java.awt.*;
public class JFontSizes extends JFrame {
int x = 5;
int y = 50;
String homework = "This is the first homework assignment";
public JFontSizes() {
super("Increasing Font Sizes");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics brush) {
super.paint(brush);
// This works sometimes. I am not sure if it is a jGRASP issue or something else.
// If I resize the frame, the text disappears, and I cannot get the text to start at the top of the frame
for(int n = 6; n<= 20; ++n) {
brush.setFont(new Font("Serif", Font.PLAIN, n));
brush.drawString(homework, x, y);
y += 15;
}
}
public static void main(String[] args) {
JFontSizes frame = new JFontSizes();
frame.setSize(400, 500);
frame.setVisible(true);
}
}
它一般不提倡直接畫到框架,而是使用了'JPanel'並使用它通過覆蓋其'paintComponent'方法進行自定義的繪畫,見[演出風俗繪畫(http://docs.oracle .COM/JavaSE的/教程/ uiswing /繪畫/)瞭解更多詳情 – MadProgrammer