java 如何向 JFrame 添加多个按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29062013/
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-11-02 14:36:58  来源:igfitidea点击:

How do I add multiple buttons to JFrame?

javabuttonjframe

提问by Anon E. Muss

I need something really basic. I tried this:

我需要一些非常基本的东西。我试过这个:

import java.*;
import javax.swing.*;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.event.ActionEvent;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class thisthing
{
    public static void main(String[]args)
    {
        boolean done = false;
        while (!done)
        {
            JFrame frame = new JFrame();

            JButton button= new JButton("Add Interest");
            frame.add(button);
            JButton button1 = new JButton("Other Button");
            frame.add(button1);

            class AddInterestListener implements ActionListener
            {
                public void actionPerformed(ActionEvent event)
                {
                    System.out.println("hello, I was pressed");
                }
            }
            class OtherButtonListener implements ActionListener
            {
                public void actionPerformed(ActionEvent event)
                {
                    System.out.println("The Other button was pressed");
                }
            }

            ActionListener listener = new AddInterestListener();
            button.addActionListener(listener);
            ActionListener listener1 = new OtherButtonListener();
            button1.addActionListener(listener1);

            frame.setSize(100, 100);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    }
}

But then my computer had trouble making two buttons on one window when I ran it, so it tried to make two windows. I really don't know how to describe it except to say that it was a mess and I did something wrong. In the end I need to add 15 buttons in a triangle shape (the famous AP peg game project)..so is there any way I can also re-position the buttons and mess with their sizes?

但是当我运行它时,我的计算机在一个窗口上制作两个按钮时遇到了麻烦,所以它试图制作两个窗口。我真的不知道如何描述它,只能说这是一团糟,我做错了什么。最后我需要在三角形中添加 15 个按钮(著名的 AP 挂钩游戏项目)..那么有什么办法可以重新定位按钮并弄乱它们的大小?

回答by Paul

You could either (recommended) use another layoutmanager (or define one on your own). Or (really NOT recommended and extremely ugly solution, but it works):

您可以(推荐)使用另一个布局管理器(或自己定义一个)。或者(真的不推荐并且非常丑陋的解决方案,但它有效):

frame.setLayout(null);

JButton button = new JButton("Hello world");
button.setBounds(20 , 20 , 100 , 30);
frame.add(button);

this will position the button at the given location and with the given size. Alternatively you could use button.setSize(100 , 30); button.setLocation(20 , 20);

这会将按钮定位在给定的位置和给定的大小。或者你可以使用 button.setSize(100 , 30); button.setLocation(20 , 20);

Note: without a layoutmanager, you can position your components freely, but they will by default have the bounds (0 , 0 , 0 , 0), so you will always have to set the position and size to get anything visible. I only provide this solution because you asked for it, you shouldn't use it though.

注意:如果没有 layoutmanager,你可以自由地定位你的组件,但默认情况下它们会有边界 (0 , 0 , 0 , 0),所以你总是必须设置位置和大小才能看到任何可见的东西。我只提供这个解决方案是因为你要求它,但你不应该使用它。