GUI,JComboBox并打开一个新窗口 [英] GUI, JComboBox and opening a new window

查看:103
本文介绍了GUI,JComboBox并打开一个新窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的新手,尤其是GUI的新手,这现在让我感到非常困惑。

I am new to Java and especially new to GUI and it's super confusing to me right now.

我正在为班级制作一个程序,该程序应该具有一个菜单(我假设是JComboBox),当选择一个选项时,该菜单会打开一个新窗口。我只是在第一个选项上工作,在其中单击矩阵,然后会弹出一个新窗口,其中包含两个名为红色药丸的按钮。 蓝色药丸,这就是我碰壁的地方。

I'm making a program for class that is supposed to have a menu (JComboBox I'm assuming) that opens a new window when an option is selected. I am just working on the first option where you click "The Matrix" and a new window pops up with two buttons called "Red Pill" & "Blue Pill" and thats where I've hit a wall.

我到了可以创建新窗口的地步(不确定是否这甚至是打开新窗口的正确方法),但是,当我尝试将Buttons添加到新的弹出窗口时,没有任何显示...

感谢您提供的正确帮助或指示!

Thanks for any help or pointers in the right direction!

public class MultiForm extends JFrame{

    private JComboBox menu;
    private JButton bluePill;
    private JButton redPill;

private static String[] fileName = {"", "The Matrix", "Another Option"};

public MultiForm() {
    super("Multi Form Program");        
    setLayout(new FlowLayout());
    menu = new JComboBox(fileName);
    add(menu);

    TheHandler handler = new TheHandler();
    menu.addActionListener(handler);        
}

private class TheHandler implements ActionListener{
    public void actionPerformed(ActionEvent event) {        
      ********************************************************************  
            //Create a new window when "The Matrix" is clicked in the JCB
            JFrame newFrame = new JFrame();
            JPanel panel = new JPanel();
            newFrame.setLayout(new FlowLayout());
            newFrame.setSize(500, 300);
            newFrame.setDefaultCloseOperation(newFrame.EXIT_ON_CLOSE);

            Icon bp = new ImageIcon(getClass().getResource("Blue Pill.png"));
            bluePill = new JButton("Blue Pill", bp);
            newFrame.add(bluePill); 

            Icon rp = new ImageIcon(getClass().getResource("Red Pill.png"));
            redPill = new JButton("Red Pill", rp);
            newFrame.add(redPill);  

            add(panel);
            newFrame.setVisible(true);

    }
}

public static void main(String[] args) {
    MultiForm go = new MultiForm();
    go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    go.setSize(400, 200);
    go.setVisible(true);
}
}


推荐答案


我尝试做newFrame.add(BluePill),它创建了一个与整个窗口大小相同的按钮,并且不允许我那样添加两个按钮

I tried doing newFrame.add(BluePill) and it created a button that was the size of the entire window and it would not allow me to add both buttons that way

这是因为默认情况下框架使用 BorderLayout 。除非另行指定,否则组件的位置将添加到 CENTER 位置,但BUT, BorderLayout 仅允许单个组件可以在五个可用位置的每个位置进行管理,因此您只会看到最后添加的组件。

That's because the frame uses a BorderLayout by default. Unless you specify otherwise, the component's will be added to the CENTER position, BUT, BorderLayout will only allow a single component to be managed at each of the it's five available positions, so you are only seeing the last component you added.

请参见如何使用BorderLayout 了解更多详情


所以我认为这不是正确的方法

so I figured that wasn't the correct way

这是正确的方法,您只需要使用布局即可可以容纳更多组件或更改要添加按钮的位置的管理器

It's the right approach, you just need to use a layout manager which can accommodate more components or change the position which you are adding the buttons

在这个小示例中,我只使用了 FlowLayout ,但是您可以使用将为您带来所需效果的任何东西

In this little example, I've just use a FlowLayout, but you can use what ever is going to give you the effect you desire

JFrame newFrame = new JFrame();
newFrame.setLayout(new FlowLayout());
newFrame.setDefaultCloseOperation(newFrame.EXIT_ON_CLOSE);

bluePill = new JButton("Blue Pill");
newFrame.add(bluePill);
redPill = new JButton("Red Pill");
newFrame.add(redPill);

newFrame.pack();
newFrame.setVisible(true);

一般而言,我不喜欢将这样的组件直接添加到顶层容器,我更喜欢使用中间容器,例如 JPanel ,这为我提供了更多的重用可能性,但这就是我。

As a general rule of thumb, I don't like adding components like this directly to a top level container, I prefer to use a intermediate container, like a JPanel, this gives me more possibilities for re-use, but that's me.

您还应该仅在框架准备就绪时使其可见,否则您可能会发现某些时候组件不会显示

You should also only make the frame visible when it's actually ready, otherwise you may find that some times, the components won't show up

请参阅在容器内布置组件以获取更多详细信息

See Laying Out Components Within a Container for more details

这篇关于GUI,JComboBox并打开一个新窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆