gpt4 book ai didi

java - 如何使用 awt/关闭窗口按钮?

转载 作者:行者123 更新时间:2023-12-02 11:59:32 25 4
gpt4 key购买 nike

晚上好!我在理解如何使用按钮的实现(awt)方面遇到问题如何在每次点击时增加字体(例如答案),而不仅仅是一次?附注我怎样才能在我的框架(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();
}
}

最佳答案

How can i increase font like answer on every my click

所以,你的问题就在这里......

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);
}
});
//...

how can i realise textArea just in border of my frame(300x300) with a place for buttonPanel on south?

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);

我还会使用 pack 而不是 setSie,但这可能需要一些额外的配置,并且由于 AWT 已经 17 年没有成为主流使用,所以我不在乎尝试记住您可能需要做什么来纠正。建议,考虑使用 Swing 或 JavaFX,您将获得更好的支持

关于java - 如何使用 awt/关闭窗口按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47369630/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com