简体   繁体   English

将多个JPanel添加到JFrame

[英]Add Multiple JPanels to JFrame

Been having a hard time adding JPanels to JFrame. 很难将JPanels添加到JFrame中。 Am pretty much new on java, always used C++ I need to do 4 Panels inside one Frame. 我是java上的新手,总是使用C ++我需要在一个Frame内做4个Panel。

Here is my Code, just started today.. 这是我的代码,刚开始今天..

package project2;
import javax.swing.JOptionPane;    
import java.awt.FlowLayout;   
import javax.swing.JFrame;    
import javax.swing.JLabel;   
import javax.swing.JPanel;   
import javax.swing.SwingConstants; 
import java.awt.Color;   
import java.awt.GridLayout;   
import java.awt.BorderLayout;   
import javax.swing.*;  
import java.awt.Container;  
import java.awt.Dimension;

public class GUI extends JFrame 
{
    private JPanel Checks; //Panel to Hold Checks
    private JPanel Transactions;
    private JPanel History;
    private JPanel Graphics;
    private JLabel CLabel;


    public GUI()
    {
        super ( "UTB Check-In");
        JPanel Checks = new JPanel(); //set up panel
        CLabel = new JLabel("Label with text");
        Checks.setBackground(Color.red);
        Checks.setLayout( new BoxLayout(Checks,BoxLayout.LINE_AXIS)); 
        add(Checks);


      // JPanel Transactions = new JPanel();
       // Transactions.setToolTipText("Electronic Transactions");
        //Transactions.setBackground(Color.blue);
       // add(Transactions);

    }

}

I was trying to put Transaction and Checks one side from the other with different colors,in this case blue and red it doesnt stay in the middle it those one or the other. 我试图将交易和支票的一面放在另一边用不同的颜色,在这种情况下蓝色和红色它不会留在中间的那些或那一个。 One of my Colleagues told me that the BoxLayout(or any layout) needed to be implemented with the size..something to that extend. 我的一位同事告诉我,BoxLayout(或任何布局)需要使用size..something来实现。 am not really sure I been reading http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html 我不确定我一直在阅读http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html

But I still do not get it completely. 但我仍然没有完全理解。 If somebody can help me out thanks! 如果有人能帮助我,谢谢!

Your code fail cause you are adding directly to the JFrame which have by default BorderLayout . 您的代码失败,因为您直接添加到默认BorderLayoutJFrame You are setting BoxLayout to the wrong panel. 您正在将BoxLayout设置为错误的面板。 You have to setLayout() to the top component(jframe) that you are adding or as i prefer adding to a jpanel rather than directly to the jframe to acomplish what you want to do. 您必须将setLayout()添加到要添加的顶部组件(jframe),或者我更喜欢添加到jpanel而不是直接添加到jframe以完成您想要执行的操作。

Example: 例:

public GUI()
{
    super ( "UTB Check-In");

    JPanel parent = new JPanel();
    parent.setLayout(new BoxLayout(parent,BoxLayout.LINE_AXIS));
    add(parent);

    JPanel Checks = new JPanel(); //set up panel
    CLabel = new JLabel("Label with text");
    Checks.setBackground(Color.red);
    parent.add(Checks);


   JPanel Transactions = new JPanel();
   Transactions.setToolTipText("Electronic Transactions");
   Transactions.setBackground(Color.blue);
   parent.add(Transactions);

}

By the way, in Java variables starts with lowerCase as a code convention. 顺便说一句,在Java中,变量以lowerCase作为代码约定开始。

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

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