【问题标题】:How to use buttons awt/close window?如何使用按钮 awt/关闭窗口?
【发布时间】:2018-05-02 08:25:21
【问题描述】:

晚上好! 我有一个理解问题,如何使用按钮的实现(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();
    }
}

【问题讨论】:

    标签: java button fonts awt textarea


    【解决方案1】:

    如何在每次点击时增加类似答案的字体

    那么,你的问题就在这里……

    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,你会得到更好的支持

    【讨论】:

    • “考虑使用 Swing 或 JavaFX,你会得到更好的支持” +++(虽然这里提供的支持非常好 - OP 很幸运 次)
    • 非常感谢!这是一个非常有帮助和有用的答案!是的,我知道 Swing 和 JavaFX...awt- 这就像我在老师的教育中的一个点,在 awt ofc 之后,我们将使用 Swing 和 JavaFX。但是老师是如何谈论所有这些的——AWT 的附加组件。顺便说一句,主面板应该包含另外两个面板 p.s.你知道吗,为什么我在改变窗口大小时会在我的框架上看到 reuslt??
    • @Montego 致电setVisible 最后
    猜你喜欢
    • 2012-09-26
    • 1970-01-01
    • 2022-01-22
    • 2013-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-14
    • 2014-07-16
    相关资源
    最近更新 更多