繁体   English   中英

利用GridLayout,JPanel,BorderLayout

[英]Utilizing GridLayout, JPanel, BorderLayout

GUI的新增功能,我试图创建一个简单的JFrame其中两个JTextArea实例彼此相邻放置,而JPanel在底部。

import java.awt.*; 
import java.awt.event.ActionListener;

import javax.swing.*; 

public class Demo extends JFrame 
{
private JPanel panel; 
private JTextArea JTextArea1; 
private JTextArea JTextArea2; 
private DecisionPanel decisionPanel; 
private GridLayout gridLayout;
private Container container;

public Demo()
{ 
    super( "Demo" ); 

    Container myContainer = new Container(); 

    JTextArea1 = new JTextArea(); 
    JTextArea2 = new JTextArea(); 

    GridLayout gridLayout = new GridLayout( 1, 2 );
    myContainer.setLayout( gridLayout ); 

    myContainer.add( new JScrollPane( JTextArea1 ) ); 
    myContainer.add( new JScrollPane( JTextArea2 ) );

    JFrame f = new JFrame(); 
    f.add( myContainer, BorderLayout.CENTER); 
    f.add( decisionPanel, BorderLayout.PAGE_END ); 
    f.setSize( 400, 400 ); 
    f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 
    f.setVisible( true ); 
}
}

JFrame不会出现。 这是将JTextArea对象添加到GridLayout正确方法吗,是否正确使用了Container

首先不扩展JFrame ,这会引起您的困惑。 基本上,您的示例代码有两个JFrame实例,那么哪个实例实际显示在屏幕上?

你也必须产生一个NullPointerExceptiondecisionPanel永远不会初始化。

public class Demo { //extends JFrame {

    private JPanel panel;
    private JTextArea JTextArea1;
    private JTextArea JTextArea2;
    private DecisionPanel decisionPanel;
    private GridLayout gridLayout;
    private Container container;

    public Demo() {

        Container myContainer = new Container();

        decisionPanel = new DecisionPanel();
        JTextArea1 = new JTextArea();
        JTextArea2 = new JTextArea();

        GridLayout gridLayout = new GridLayout(1, 2);
        myContainer.setLayout(gridLayout);

        myContainer.add(new JScrollPane(JTextArea1));
        myContainer.add(new JScrollPane(JTextArea2));

        JFrame f = new JFrame("Demo");
        f.add(myContainer, BorderLayout.CENTER);
        f.add(decisionPanel, BorderLayout.PAGE_END);
        f.setSize(400, 400);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}

或者,从JPanel扩展并单独将“ Demo面板添加到JFrame ,这可能更可取,这取决于您要实现的目标...

public class Demo extends JPanel {

    private JPanel panel;
    private JTextArea JTextArea1;
    private JTextArea JTextArea2;
    private DecisionPanel decisionPanel;
    private GridLayout gridLayout;
    private Container container;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new Demo());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public Demo() {
        setLayout(new BorderLayout());
        Container myContainer = new Container();

        decisionPanel = new DecisionPanel();
        JTextArea1 = new JTextArea();
        JTextArea2 = new JTextArea();

        GridLayout gridLayout = new GridLayout(1, 2);
        myContainer.setLayout(gridLayout);

        myContainer.add(new JScrollPane(JTextArea1));
        myContainer.add(new JScrollPane(JTextArea2));
        add(myContainer, BorderLayout.CENTER);
        add(decisionPanel, BorderLayout.PAGE_END);
    }
}

暂无
暂无

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

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