Javax Swing不会显示所有组件

时间:2016-09-02 01:29:39

标签: java swing awt grouplayout

这是一个小型的java程序,我正在努力工作 我的问题是,SearchTab类中的组件不会显示!在initFrame()中,我有一个JTabbedPlane,它只显示我在SearchTab类中注释掉了很多组件,但是当我从SearchTab类中取消注释一些组件时它们就不显示了。

我在做错了什么?我不知道有任何其他更简单的方式来问这个,对不起,如果我不清楚这个问题。

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


public class View{

    public static JFrame        mainFrame;
    public static JTabbedPane   tabs;

    public static void initFrame(){
        mainFrame = new JFrame("Address Book Proggie");
        mainFrame.setSize(600,300);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setVisible(true);

        tabs = new JTabbedPane();
        tabs.addTab("Search", new SearchTab());
        tabs.addTab("Insert", new InsertTab("This is the insert tab!"));
        tabs.addTab("Delete", new DeleteTab("This is the tab used to delete items from the database"));
        mainFrame.add(tabs);

    }

//  public static void main(String args[]){
//      initFrame();
//  }
}

class DeleteTab extends JPanel{
    public DeleteTab(String str){
        JLabel deleteLabel = new JLabel(str);
        add(deleteLabel);
    }
}


class InsertTab extends JPanel{
    public InsertTab(String str){
        JLabel insertLabel = new JLabel(str);
        add(insertLabel);
    }
}


class SearchTab extends JPanel{
    public SearchTab(){
        //give this panel a border)
        this.setBorder(BorderFactory.createMatteBorder(10,10,10,10, Color.BLUE));

        //set the group layout
        GroupLayout gl = new GroupLayout(this);
        this.setLayout(gl);
        gl.setAutoCreateGaps(true);

        //create labels..       
        JLabel searchLabel = new JLabel("Search by...");
        JLabel nameLabel = new JLabel("Name: ");
        JLabel addressLabel = new JLabel("Address: ");
        JLabel phoneLabel = new JLabel("Phone Number: ");

        //create text fields...
        JTextField nameField = new JTextField(20);
        JTextField addressField = new JTextField(20);
        JTextField phoneField = new JTextField(20);


        gl.setHorizontalGroup(gl.createSequentialGroup()
            .addGroup(gl.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(searchLabel))
            .addGroup(gl.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(nameLabel)
                .addComponent(addressLabel)
                .addComponent(phoneLabel))      

);

        gl.setVerticalGroup(gl.createSequentialGroup()
            .addGroup(gl.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(searchLabel))
            .addGroup(gl.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(nameLabel)
                .addComponent(nameField))
            .addGroup(gl.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(addressLabel)
                .addComponent(addressField))
            .addGroup(gl.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(phoneLabel)
                .addComponent(phoneField))

);
System.out.println("Hello there");      



    }
}

0 个答案:

没有答案