Java 如何向 JFrame Gui 添加按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31245320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 10:50:02  来源:igfitidea点击:

How to add a button to a JFrame Gui

javaswingbuttonjframe

提问by Rssj V

I'm trying to add a button to a frame gui.
i tried making a panel and adding it to that, but it does not work. please help!

我正在尝试向框架 gui 添加一个按钮。
我尝试制作一个面板并将其添加到其中,但它不起作用。请帮忙!

here is my code:

这是我的代码:

import javax.swing.*;

public class Agui extends JFrame {
    public Agui() {
        setTitle("My Gui");
        setSize(400, 400);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        JButton button;
        JPanel panel;

        // my error lines are under the "panel" and  "button"
        // it says i must implement the variables. what does that mean???
        panel.add(button);
    }
    public static void main(String[] args) {
        Agui a = new Agui();
    }
}

采纳答案by almightyGOSU

Example Code:

示例代码:

import javax.swing.*;

public class Agui extends JFrame {

    public Agui() {

        setTitle("My Gui");
        setSize(400, 400);

        // Create JButton and JPanel
        JButton button = new JButton("Click here!");
        JPanel panel = new JPanel();

        // Add button to JPanel
        panel.add(button);
        // And JPanel needs to be added to the JFrame itself!
        this.getContentPane().add(panel);

        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {

        Agui a = new Agui();
    }
}

Output:

输出:

Image

图片

Note:

笔记:

  • Create the JButton and JPanel using new JButton("...");and new JPanel()
  • Add the JPanel to the JFrame's content pane using getContentPane().add(...);
  • 使用new JButton("...");和创建 JButton 和 JPanelnew JPanel()
  • 使用以下命令将 JPanel 添加到 JFrame 的内容窗格 getContentPane().add(...);

回答by CoderNeji

Change:

改变:

JButton button;
JPanel panel;

to:

到:

JButton button = new JButton();
JPanel panel = new JPanel();

You can also pass a Stringvalue in JButton()constructor for that string value to be shown on the JButton.

您还可以StringJButton()构造函数中传递一个值,以便该字符串值显示在JButton.

Example:JButton button = new JButton("I am a JButton");

例子:JButton button = new JButton("I am a JButton");

回答by ChaminduWeerasinghe

If you can Change this Program, You can adjust the button place also

如果您可以更改此程序,您也可以调整按钮位置

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;



public class agui extends JFrame

{

agui()
{
    setTitle("My GUI");
    setSize(400,400);
    setLayout(null);

    JButton button = new JButton("Click Here..!");
    button.setBounds(50,100,100,50); /*Distance from left,           
                      Distance from top,length of button, height of button*/
    add(button);

    setVisible(true);

}

public static void main(String[] args)
{

     JFrame agui = new agui();



}

}

Output

输出