[英]How to use buttons awt/close window?
晚上好! 我在理解上有問題,如何使用按鈕實現(awt)我如何才能在每次單擊時增加字體,如答案,而不僅僅是一次? PS我如何才能在我的框架(300x300)的邊框中實現textArea,並在南部放置buttonPanel?
public class GUI extends Frame {
public GUI() throws HeadlessException {
Frame frame = new Frame();
frame.setVisible(true);
frame.setTitle("Test window");
frame.setSize(300, 300);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//System.exit(0);
frame.dispose();
}
});
String fontName = "Arial";
int fontStyle = 10;
int fontSize = 12;
Font font = new Font(fontName, fontStyle, fontSize);
Panel mainPanel = new Panel();
Panel textPlacePanel = new Panel();
Panel buttonPlacePanel = new Panel();
Button increaseButton = new Button("Increase");
Button decreaseButton = new Button("Decrease");
Label label = new Label("Font size");
TextArea textArea = new TextArea();
textArea.setFont(font);
frame.add(mainPanel,BorderLayout.CENTER);
frame.add(buttonPlacePanel,BorderLayout.SOUTH);
frame.add(textPlacePanel,BorderLayout.CENTER);
buttonPlacePanel.add(label);
buttonPlacePanel.add(increaseButton);
buttonPlacePanel.add(decreaseButton);
textPlacePanel.add(textArea);
increaseButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int i = font.getSize();
Font font = new Font(fontName,fontStyle,++i);
textArea.setFont(font);
}
});
decreaseButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int i = font.getSize();
Font font = new Font(fontName,fontStyle,--i);
textArea.setFont(font);
}
});
}
public static void main(String[] args) {
GUI gui = new GUI();
}
}
我如何在每次點擊時增加諸如答案之類的字體
所以,您的問題在這里...
int i = font.getSize();
Font font = new Font(fontName, fontStyle, i);
您正在使用在構造函數中創建的font
實例,但是隨后創建了Font
的新本地實例(在ActionListener
的上下文中)並將其應用於TextArea
,這意味着基本大小始終為12。
而是使用Font#deriveFont
public class GUI extends Frame {
private Font font;
// Don't use hard coded values, use the constent names, its easier
// to read
private int fontStyle = Font.PLAIN;
private int fontSize = 12;
public GUI() {
String fontName = "Arial";
font = new Font(fontName, fontStyle, fontSize);
//...
increaseButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
font.deriveFont(++fontSize);
textArea.setFont(font);
}
});
//...
我怎樣才能在我的邊框(300x300)的邊框中實現textArea,並在南部放置buttonPanel?
BorderLayout
將僅管理添加到其管理的五個可用位置之一的最后一個組件。 我不知道mainPanel
在做什么,但這是沒有意義的,所以我會擺脫它。
另外, Panel
的默認布局是Flowlayout
,不確定為什么要將文本區域包裝在其中,但是我將布局管理器更改為更有用的東西
Font font = new Font(fontName, fontStyle, fontSize);
//Panel mainPanel = new Panel();
Panel textPlacePanel = new Panel(new BorderLayout());
Panel buttonPlacePanel = new Panel(new GridLayout(1, 3));
Button increaseButton = new Button("Increase");
Button decreaseButton = new Button("Decrease");
Label label = new Label("Font size");
TextArea textArea = new TextArea();
textArea.setFont(font);
//frame.add(mainPanel, BorderLayout.CENTER);
frame.add(buttonPlacePanel, BorderLayout.SOUTH);
frame.add(textPlacePanel, BorderLayout.CENTER);
我還將在setSie
使用pack
,但是這可能需要一些額外的配置,並且由於AWT已有17年沒有在主流中使用了,所以我不在乎嘗試記住您可能需要做些什么來進行更正。 建議,考慮改用Swing或JavaFX,您將獲得更好的支持
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.