适当的JFrame组件,用于查看输入的数据,并在加载时正确查看布局

时间:2014-03-14 02:01:52

标签: java css swing

截至目前,我已将所有标签和输入字段叠加在JFrame的中心。

当我运行程序程序时,标签Business Name显示在顶部中心,在我调整窗口大小之前没有其他内容出现......

任何人都可以告诉我为什么会这样做以及如何解决它?

另外,我想在JFrame的左侧添加一个区域,当您单击“保存”时,该区域将显示该联系人,我将使用哪种类型的组件来执行此操作?

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.*;
import javax.swing.*;

public class Driver extends JFrame {
    private JFrame f;
    private JPanel p;
    private JTextField fieldBN;
    private JTextField fieldFN;
    private JTextField fieldLN;
    private JTextField fieldP;
    private JTextField fieldE;
    private JTextField fieldA;
    private JTextField aLine2;
    private JTextField fieldW;
    private JLabel labelBN;
    private JLabel labelFN;
    private JLabel labelLN;
    private JLabel labelP;
    private JLabel labelE;
    private JLabel labelA;
    private JLabel labelW;
    private JButton buttonS;

    // Constructor:
    public Driver() {       
        gui();      
    }

    public void gui() {
        f = new JFrame("Contact Book");
        f.setVisible(true);
        f.setSize(900,800); // default size is 0,0
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        GridBagConstraints c = new GridBagConstraints();  

        c.insets = new Insets(10, 10, 10, 10);

        p = new JPanel(new GridBagLayout());
        f.getContentPane().add(p, BorderLayout.NORTH);

    c.gridx = 100;
    c.gridy = 0;
    labelBN = new JLabel ("Business Name");
    p.add(labelBN, c);  

    c.gridx = 110;
    c.gridy = 0;
    fieldBN = new JTextField(10);
    p.add(fieldBN, c);

    c.gridx = 100;
    c.gridy = 10;
    labelFN= new JLabel ("First Name");
    p.add(labelFN, c);      

    c.gridx = 110;
    c.gridy = 10;
    fieldFN = new JTextField (10);
    p.add(fieldFN, c);

    c.gridx = 100;
    c.gridy = 20;
    labelLN= new JLabel ("Last Name");
    p.add(labelLN, c);      

    c.gridx = 110;
    c.gridy = 20;
    fieldLN = new JTextField (10);
    p.add(fieldLN, c);

    c.gridx = 100;
    c.gridy = 30;
    labelP = new JLabel ("Phone Number");
    p.add(labelP, c);

    c.gridx = 110;
    c.gridy = 30;
    fieldP = new JTextField (10);
    p.add(fieldP, c);

    c.gridx = 100;
    c.gridy = 40;
    labelE = new JLabel ("Email");
    p.add(labelE, c);

    c.gridx = 110;
    c.gridy = 40;
    fieldE = new JTextField (10);
    p.add(fieldE, c);

    c.gridx = 100;
    c.gridy = 50;                           
    labelA = new JLabel ("Address");
    p.add(labelA, c);

    c.gridx = 110;
    c.gridy = 50;
    fieldA = new JTextField (10);
    p.add(fieldA, c);
    c.gridx = 110;
    c.gridy = 60;
    aLine2 = new JTextField (10);
    p.add(aLine2, c);

    c.gridx = 100;
    c.gridy = 70;
    labelW = new JLabel ("Website");
    p.add(labelW, c);

    c.gridx = 110;
    c.gridy = 70;
    fieldW = new JTextField (10);
    p.add(fieldW, c);

    c.gridx = 110;
    c.gridy = 80;
    buttonS = new JButton("Save");
    p.add(buttonS, c);

      //    Window Listeners
      addWindowListener(new WindowAdapter() {

      }
        ); 
           } // End Gui

      public static void main(String[] args) {

          new Driver();
       } // End main Method

       } // End class Driver

1 个答案:

答案 0 :(得分:3)

可能的原因就在这里......

public void gui() {
    f = new JFrame("Contact Book");
    f.setVisible(true);
    f.setSize(900,800); // default size is 0,0

您提前致电setVisible

简单的解决方案是在您按照自己想要的方式设置框架后调用setVisible ...

public void gui() {
    f = new JFrame("Contact Book");
    // Add everything to the frame...
    // pack the frame for better cross platform support
    f.pack();
    // Make it visible
    f.setVisible(true);
}

此外,从JFrame延伸然后创建JFrame的实例非常令人困惑。通常,您不需要从JFrame扩展,只需根据需要创建实例。

您还应该查看Initial Threads

例如......

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
       public void run() {
            new Driver();
       }
    });
} // End main Method