【问题标题】:My whole code is complete but I am not able to add a second button我的整个代码已完成,但我无法添加第二个按钮
【发布时间】:2012-04-15 07:03:07
【问题描述】:

请查看我的代码,它按我想要的方式正常工作,但唯一的问题是我想在当前按钮对面添加另一个按钮,但我无法这样做,任何人都可以帮助我。

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

public class Example2 extends JFrame {

public Example2() {
        initUI();
    }

public final void initUI() {
        JPanel panel = new JPanel();
        getContentPane().add(panel);
        panel.setLayout(null);
        panel.setToolTipText("A Panel container");

        JButton button = new JButton("Even");
        button.setBounds(100, 60, 100, 30);
        button.setToolTipText("A button component");

        JButton button2 = new JButton("Odd");
        button2.setBounds(100, 60, 100, 30);
        button2.setToolTipText("A button component");

        //Add action listener to button
                button.addActionListener(new ActionListener () {
                    public void actionPerformed(ActionEvent  e)
                    {
                        //Execute when button is pressed
                        System .out.println("You clicked the button");
                        int sum=0;
                                for(int i=1;i<=100;i++){
                                    if(i%2==0){
                                        System.out.println(i);
                                        sum+=i;
                                    }
                                }
        System.out.println("Sum of even numbers: "+sum);
                    }
        });

        panel.add(button);
        panel.add(button2);

        setTitle("Tooltip");
        setSize(500, 400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

    }
    public static void main(String[] args) {
                Example2 ex = new Example2();
                ex.setVisible(true);
    }
}

【问题讨论】:

标签: java swing user-interface awt


【解决方案1】:
panel.setLayout(null);

这就是它开始出错的地方。

  1. 使用布局。请参阅Laying Out Components Within a ContainerEffective Layout Management: Short Course 了解更多详情。
  2. 使用:

顺便说一句。

    ...
    button.setBounds(100, 60, 100, 30);
    button.setToolTipText("A button component");

    JButton button2 = new JButton("Odd");
    button2.setBounds(100, 60, 100, 30);
    ...

您是否注意到两个按钮的边界是相同的?当您将两个大小相同的组件放在同一个位置时,您认为会发生什么?

【讨论】:

  • 我做不到你能帮忙吗
  • @user1054393 有关有用的链接,请参阅我的回答。
  • fkr 提供了 2 个单独的“单行修复”,但也通过 @Sanjay 和我的答案中的链接。
  • 多么讽刺。这正是his other question with the same snippet 中推荐的内容
  • @Robin(叹气)你可以把马牵到水边,但你不能让它喝水。
【解决方案2】:

您必须将panel.setLayout(null) 更改为您需要的布局。例如:

    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));

    panel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER));

【讨论】:

    【解决方案3】:

    安德鲁·汤普森 +1,

    这里有一些有用的链接:

    1. A Visual Guide to Layout Managers
    2. Using Layout Managers
    3. Adding space between components

    【讨论】:

    • 最后一个是一个很好的答案,但错过了一种添加空白的技术,布局填充。例如。 new GridLayout(0,3,10,10); // Use 10px padding 因为我不能(再次)投票赞成另一个答案,所以我只会投票赞成这个答案。顺便说一句 - 感谢评论链接,我知道有一个我应该完成的编辑打开。 ;)
    猜你喜欢
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    • 2020-07-18
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多