Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用按钮更改Jframe的高度_Java_Jframe - Fatal编程技术网

Java 使用按钮更改Jframe的高度

Java 使用按钮更改Jframe的高度,java,jframe,Java,Jframe,我想通过单击按钮来更改jFrame的高度 但我不知道从哪里开始。我只有一个按钮,不知道密码 *编辑: 我知道可以更改JFrame大小并将其位置移动到右上角的代码如下 Dimension screenSize = new Dimension(Toolkit.getDefaultToolkit().getScreenSize()); setPreferredSize(new Dimension(311, 430)); Dimension windowSize = new Di

我想通过单击按钮来更改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:这是我的
公共静态void main(字符串args[])


我不知道如何调用
frame
以在
frame中使用。设置大小

如果您想在单击按钮时更改帧大小,则可以尝试此操作

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

如果要更改按钮单击时的帧大小,可以尝试此操作

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

要在按下按钮时运行代码,请使用
.addActionListener()
方法向按钮添加一个。在这里看一下这段代码,看看它是否适合您:(阅读注释以了解发生了什么)


要在按下按钮时运行代码,请使用
.addActionListener()
方法向按钮添加一个。在这里看一下这段代码,看看它是否适合您:(阅读注释以了解发生了什么)



你的测试代码在哪里?我没有任何代码要测试。但我只知道我可以在初始化部分更改jframe的高度。。。我会更新这个问题,我知道这个代码可以改变JFrame的大小。但这只是init上的工作@Sachith@MArgus,什么是
init
?是否在构造函数中调用了方法?如果你发了一个帖子,帮助你会容易得多。对不起@Gulllie。是的,我的意思是
init
。在我的构造函数中调用的。。很抱歉,这个问题没有提供任何代码信息,因为我不知道我需要提供什么代码。。我知道这个问题上的代码来自另一个问题,它用于移动JFrame的位置和调整JFrame的大小。您测试的代码在哪里?我没有任何代码要测试。但我只知道我可以在初始化部分更改jframe的高度。。。我会更新这个问题,我知道这个代码可以改变JFrame的大小。但这只是init上的工作@Sachith@MArgus,什么是
init
?是否在构造函数中调用了方法?如果你发了一个帖子,帮助你会容易得多。对不起@Gulllie。是的,我的意思是
init
。在我的构造函数中调用的。。很抱歉,这个问题没有提供任何代码信息,因为我不知道我需要提供什么代码。。我知道这个问题的代码来自另一个问题,它用于移动JFrame的位置和调整JFrame的大小..我遇到了一个小问题。。可能是我的代码有问题。。请查看更新的问题。我将包括我的
public void run()
为什么你建议新手在JButton中添加鼠标听筒?如果JButton被禁用,按下它会导致动作吗?应该吗-感谢您给出了错误的示例和建议。无论如何,感谢您提供
setSize
信息。我正在使用NetBeans,因此我认为NetBeans将为我提供一个函数
private void jbutton3aactionperformed(java.awt.event.ActionEvent evt)
,我将
setSize
代码放在it@HovercraftFullOfEels编辑了答案,删除了鼠标侦听器。@MArgusChopinGyver抱歉,我之前没有注意到编辑。我遇到了一个小问题。。可能是我的代码有问题。。请查看更新的问题。我将包括我的
public void run()
为什么你建议新手在JButton中添加鼠标听筒?如果JButton被禁用,按下它会导致动作吗?应该吗-感谢您给出了错误的示例和建议。无论如何,感谢您提供
setSize
信息。我正在使用NetBeans,因此我认为NetBeans将为我提供一个函数
private void jbutton3aactionperformed(java.awt.event.ActionEvent evt)
,我将
setSize
代码放在it@HovercraftFullOfEels编辑答案,删除鼠标侦听器。@MArgusChopinGyver抱歉,我没有注意到之前的编辑。感谢您提供此代码
private static JFrame。。我正在使用NetBeans,我认为它们为我提供了
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)
,因此我不必在它上面添加
addActionListener
(NetBeans已经提供了它)。因为有了这段代码,我可以在我的
jbutton3aactionperformed
@MArgus上使用
frame
变量(很乐意帮忙。)当您的问题得到解决时,不要忘记将问题标记为已回答。感谢您提供此代码
private static JFrame。。我正在使用NetBeans,我认为它们为我提供了
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)
,因此我不必在它上面添加
addActionListener
(NetBeans已经提供了它)。因为有了这段代码,我可以在我的
jbutton3aactionperformed
@MArgus上使用
frame
变量(很乐意帮忙。)当你的问题得到解决时,别忘了将问题标记为已回答。
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);

    }
}