晚上好!
我在理解方面有问题,如何使用按钮实现(awt)
我如何才能增加每次点击而不是一次的答案之类的字体?
P.s.我如何才能在我的框架(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);


我还将在pack上使用setSie,但是这可能需要一些其他配置,并且由于AWT已有17年没有主流使用了,所以我不在乎尝试记住您可能需要的内容。做纠正。建议,考虑改用Swing或JavaFX,您将获得更好的支持

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

10-12 00:21
查看更多