利用GridLayout,JPanel,BorderLayout

时间:2015-03-16 23:36:09

标签: java swing jpanel layout-manager grid-layout

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正确使用的正确方法吗?

1 个答案:

答案 0 :(得分:1)

从不延长JFrame开始,这会让您感到困惑。基本上,您的示例代码有两个JFrame实例,那么实际上哪一个实际显示在屏幕上?

您还必须生成NullPointerException,因为decisionPanel从未初始化。

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);
    }
}