使用按钮

时间:2016-11-24 03:21:49

标签: java jframe

我想点击一下按钮来改变jFrame的高度。

但我不知道从哪里开始。我只有一个按钮,不知道代码。

*编辑: 我知道的代码可以改变JFrame的大小并将其移动到右上角是这个

    Dimension screenSize = new Dimension(Toolkit.getDefaultToolkit().getScreenSize());
    setPreferredSize(new Dimension(311, 430));
    Dimension windowSize = new Dimension(getPreferredSize());
    int wdwLeft = 530 + screenSize.width / 2 - windowSize.width / 2;
    int wdwTop = 0;
    pack();
    jButton2.setEnabled(false);
    setLocation(wdwLeft, wdwTop);

但我不知道可以改变JFrame

大小的特定代码

* EDIT2:这是我的public static void main(String args[])

public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            final VoucherChecker frame = new VoucherChecker();
            frame.setVisible(true);
        }
    });
}

我不知道如何致电frameframe.setSize

2 个答案:

答案 0 :(得分:2)

要在按下按钮时运行代码,请使用.addActionListener()方法向按钮添加ActionListener。在这里查看此代码,看看它是否适合您:( 阅读评论以查看正在进行的

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main {

    private JFrame frame;

    public static void main(String[] args){

        SwingUtilities.invokeLater(new Runnable(){

            @Override
            public void run() {
                new Main();                                 //Create a Main object, wrapped by SwingUtilities.invokeLater to make it thread safe
            }
        });

    }

    public Main() {                                         //Main's constructor

        frame = new JFrame();                               //Create JFrame
        frame.setTitle("Test Frame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        init((JPanel)frame.getContentPane());               //'init' frame's JPanel
        setFrameSizeAndPos(frame);                          //Set the frame's size

        frame.setVisible(true);
    }

    private void setFrameSizeAndPos(JFrame frame) {

        //Set JFrame size here! Eg:
        frame.pack();                                       //Set the frame size, you could change this to set it in a different way.
        frame.setLocationRelativeTo(null);                  //Place frame in the center of the screen
    }

    private void init(JPanel panel) {
        //Setup your GUI here...
        JButton button1 = new JButton("Click me!");         //Create button
        button1.addActionListener(new ActionListener(){     //add an ActionListener to the button, passing in an Anonymous Class

            @Override
            public void actionPerformed(ActionEvent e) {
                setFrameSizeAndPos(frame);                  //This will be called then the button is pressed
            }
        });

        panel.add(button1);

    }
}

答案 1 :(得分:1)

如果您想在按钮上更改框架尺寸,则可以尝试此操作。

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Run {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setTitle("Stackoverflow");


        JButton button = new JButton("Change Size");
        button.setVisible(true);
        frame.getContentPane().add(button, BorderLayout.SOUTH);

        frame.setSize(200, 200);
        frame.setVisible(true);

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                /* now you can do some calculation that
                how much you want to change
                the frame size by the button click */

                // increasing hight by 50
                frame.setSize(frame.getWidth(), frame.getHeight() + 50); 
            }
        });
    }
}