简体   繁体   English

Java Swing设计

[英]Java Swing design

I have a question regarding coding a Swing UI. 我有一个关于编码Swing UI的问题。 If I want to make a software with some option eg on the first Frame I have three buttons (New, Option, Exit). 如果我想制作带有某些选项的软件,例如在第一帧上我有三个按钮(New,Option,Exit)。

Now if a user clicks the new button, I want to change the entire content in the Frame to something else. 现在,如果用户单击新按钮,我想将Frame中的整个内容更改为其他内容。 I know I have to use addActionListener to that button. 我知道我必须对该按钮使用addActionListener But my question is how to change the contents in the frame. 但我的问题是如何更改框架中的内容。 Creating new Frames and then use setVisible() isn't an option for me. 创建新框架然后使用setVisible()不是我的选择。

And to use frame.remove() all the objects seems awkward if it is several things that needs to be removed. 并且要使用frame.remove()如果有几件需要删除的东西,所有对象看起来都很尴尬。 Or is it the right way? 还是正确的方式?

Look into the Card Layout . 查看卡片布局 I would also probably use menu items instead of buttons. 我也可能使用菜单项而不是按钮。

CardLayout is indeed the better choice in this case; 在这种情况下, CardLayout确实是更好的选择; but, when the occasion demands, a Component may be removed from a Container using either the remove() or removeAll() method. 但是,当需要时,可以使用remove()removeAll()方法从Container中删除Component Afterward, the essential step is to invoke the [ validate() ]( http://java.sun.com/javase/6/docs/api/java/awt/Container.html#validate()) method to lay out the container's subcomponents again. 之后,必要的步骤是调用[ validate() ]( http://java.sun.com/javase/6/docs/api/java/awt/Container.html#validate())方法来布局容器的子组件再次出现。 Oscar Reyes' example uses the Frame 's [ pack() ]( http://java.sun.com/javase/6/docs/api/java/awt/Window.html#pack()) method, inherited from Window , to achieve this effect. Oscar Reyes的例子使用了Frame的[ pack() ]( http://java.sun.com/javase/6/docs/api/java/awt/Window.html#pack())方法,继承自Window ,达到这个效果。 In this example , the resetGame() method reconstructs the display panel in a similar way. 在此示例中resetGame()方法以类似的方式重建显示面板。

You may use frame.remove() 你可以使用frame.remove()

The difference is that you may remove a whole panel instead of removing "several" things, and you just replace it with a new panel 不同之处在于您可以删除整个面板而不是删除“多个”内容,而只需将其替换为新面板即可

frame.add( mainPanel );
...
// in the action listener 
frame.remove( mainPanel );
frame.add( theNewPage );

The point is, you don't have to be afraid of removing awkwardly things in the main frame, for you have to place all those things into a single panel and then just change panels. 关键是,您不必害怕在主框架中移除笨拙的东西,因为您必须将所有这些东西放在一个面板中然后只需更换面板。

UPDATE UPDATE

I've made the code needed to test what I'm talking about. 我已经制作了测试我正在谈论的内容所需的代码。

Here's a running sample: 这是一个正在运行的示例:

alt text http://img190.imageshack.us/img190/5206/capturadepantalla200912p.png 替代文字http://img190.imageshack.us/img190/5206/capturadepantalla200912p.png

later 后来

alt text http://img301.imageshack.us/img301/1368/capturadepantalla200912n.png 替代文字http://img301.imageshack.us/img301/1368/capturadepantalla200912n.png

Here's the running sample: 这是运行样本:

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

public class ChangePage {
    JComponent mainPage;
    JComponent newPage;
    JFrame     frame;

    public static void main( String [] args ) {
        ChangePage changePageDemo = new ChangePage();
        changePageDemo.show();
    }
    private void show(){
        frame = new JFrame("Demo");
        frame.add( getMainPage() );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
    }
    private JComponent getMainPage() {
        if( this.mainPage != null ) {
            return this.mainPage;
        }
        this.mainPage = new JPanel(new BorderLayout());
        mainPage.add( new JLabel("Choose an option") );
        mainPage.add( new JPanel(){{
            add( new JButton("New"){{
                addActionListener( new ActionListener(){
                    public void actionPerformed( ActionEvent e ){
                        SwingUtilities.invokeLater( new Runnable(){
                            public void run(){
                                frame.remove( getMainPage() );
                                frame.add( getNewPage() );
                                //frame.setContentPane( getNewPage() );
                                frame.pack();

                            }
                        });
                    }
                });
            }});
            add( new JButton("Options"));
            add( new JButton("Exit"));
        }}, BorderLayout.SOUTH );
        return this.mainPage;

    }
    private JComponent getNewPage() {
        if( newPage != null ) {
            return newPage;
        }
        this.newPage = new JPanel();
        this.newPage.add( new JLabel("<html>This is the \"new\" page.<br> Do you like it?<br></html>"));
        this.newPage.add( new JButton("Return"){{
            addActionListener( new ActionListener(){
                public void actionPerformed( ActionEvent e ){
                    SwingUtilities.invokeLater( new Runnable(){
                        public void run(){
                            frame.remove( getNewPage() );
                            frame.add( getMainPage() );
                            //frame.setContentPane( getMainPage() );
                            frame.pack();

                        }
                    });
                }
            });
        }});
        return this.newPage;
    }

}

Alternatively you may use setContentPane :) 或者你可以使用setContentPane :)

最简单的方法是使用选项卡式窗格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM