JFrame窗口拒绝打开

时间:2017-01-30 06:32:28

标签: java swing

我目前正在开发一个Java程序,并且该程序没有显示任何内容。

在main方法中有以下代码:

WindowClient client = new WindowClient();
    client.pack();
    client.setVisible(true);

和WindowClient:

import javax.swing.JFrame;
import java.awt.GridBagLayout;
import javax.swing.SpringLayout;

import org.datacontract.schemas._2004._07.NaturalDisasterService.NaturalDisaster;
import org.tempuri.INaturalDisasterServiceProxy;

import java.awt.BorderLayout;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Font;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.awt.event.ActionEvent;

public class WindowClient extends JFrame {
    private INaturalDisasterServiceProxy ndsp;
    private JTextField textDisasterName; // text box on insertPanel for disaster names
    private JTextField textDisasterDescription; // text box on insertPanel for disaster descriptions
    public JPanel insertPanel, detailPanel; 
    public JList list; // the list
    public JLabel lblDisasterNameStaticInsert, //displays text "Disaster Name:" on insertPanel
        lblDisasterDescStaticInsert, // displays text "Disaster Description:" on insertPanel
        lblDisasterDescDataDetail, //  displays disaster description from database on detailPanel
        lblDisasterDescStaticDetail;  // label displays text "Disaster Desc:" on detailPanel
    public WindowClient() {
    buildWindow();
    ndsp = new INaturalDisasterServiceProxy();
    ndsp.setEndpoint("url redacted");
    updateList();

    }

    /**
     * @param args
     */

    public void getSelection() {
    showDetailPanel();
    }

    public void insertLoad() {
    if (detailPanel.isVisible()) {
        hideDetailPanel();
    }
    showInsertPanel();
    }

    public void saveToDB() {
    hideInsertPanel();

    }

    public void updateList() {

     NaturalDisaster nds[];
    try {
        nds = ndsp.getData();

        DefaultListModel model = new DefaultListModel();
        for(NaturalDisaster disaster : nds){
        model.addElement(disaster.getDisasterName());
        }
        getContentPane().remove(list);
        list = new JList(model);
        list.setBounds(81, 11, 247, 132);
        getContentPane().add(list);
        getContentPane().repaint();
        getContentPane().revalidate();


    } catch (RemoteException e) {
        // TODO Auto-generated catch block
        System.out.println(e.getMessage());
        //e.printStackTrace();
    }

    }

    public void buildWindow() {
    getContentPane().setLayout(null);

    list = new JList();
    list.setBounds(81, 11, 247, 132);
    getContentPane().add(list);

    insertPanel = new JPanel();
    insertPanel.setBounds(10, 150, 414, 67);
    getContentPane().add(insertPanel);
    insertPanel.setLayout(null);

    detailPanel = new JPanel();
    detailPanel.setBounds(10, 150, 414, 67);
    detailPanel.setLayout(null);

    // Label for disaster names on insertPanel
    lblDisasterNameStaticInsert = new JLabel("Disaster Name:");
    lblDisasterNameStaticInsert.setFont(new Font("Tahoma", Font.BOLD, 11));
    lblDisasterNameStaticInsert.setBounds(10, 5, 105, 14);
    insertPanel.add(lblDisasterNameStaticInsert);

    // label for disaster descripitons on the insertPanel
    lblDisasterDescStaticInsert = new JLabel("Disaster Description:");
    lblDisasterDescStaticInsert.setFont(new Font("Tahoma", Font.BOLD, 11));
    lblDisasterDescStaticInsert.setBounds(10, 30, 120, 14);
    insertPanel.add(lblDisasterDescStaticInsert);

    // text boxes on the insertPanel
    textDisasterName = new JTextField();
    textDisasterName.setBounds(138, 2, 266, 20);
    insertPanel.add(textDisasterName);
    textDisasterName.setColumns(10);

    textDisasterDescription = new JTextField();
    textDisasterDescription.setBounds(138, 27, 266, 20);
    insertPanel.add(textDisasterDescription);
    textDisasterDescription.setColumns(10);
    getContentPane().add(detailPanel);

    // label for the disaster description from database on display panel
    lblDisasterDescDataDetail = new JLabel("New label");
    lblDisasterDescDataDetail.setBounds(10, 30, 120, 14);
    detailPanel.add(lblDisasterDescDataDetail);

    // label displays text "Disaster Desc:" on display panel
    lblDisasterDescStaticDetail = new JLabel("Disaster Desc:");
    lblDisasterDescStaticDetail.setBounds(10, 5, 105, 14);
    detailPanel.add(lblDisasterDescStaticDetail);

    JButton btnGetSelection = new JButton("Get Selection");
    btnGetSelection.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        getSelection();
        }
    });
    btnGetSelection.setBounds(10, 228, 105, 23);
    getContentPane().add(btnGetSelection);

    JButton btnInsert = new JButton("Insert");
    btnInsert.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        insertLoad();
        }
    });
    btnInsert.setBounds(125, 228, 124, 23);
    getContentPane().add(btnInsert);

    JButton btnSave = new JButton("Save Info to DB");
    btnSave.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        saveToDB();
        }
    });

    btnSave.setBounds(259, 228, 165, 23);
    getContentPane().add(btnSave);
    //getContentPane().setVisible(true);
    }

    public void hideInsertPanel() {
    getContentPane().remove(insertPanel);
    insertPanel.setVisible(false);
    }

    public void showInsertPanel() {
    getContentPane().add(insertPanel);
    insertPanel.setVisible(true);
    insertPanel.getParent().revalidate();
    }

    public void hideDetailPanel() {
    getContentPane().remove(detailPanel);
    detailPanel.setVisible(false);
    }

    public void showDetailPanel() {
    getContentPane().add(detailPanel);
    detailPanel.setVisible(true);
    detailPanel.getParent().revalidate();
    }

}

关于JFrame故障问题的其他一些问题提到了对setVisible()pack()的需求,但我已经完成了这两个调用。唯一的结果是程序运行,并且程序在Windows任务栏中打开,但没有相应的窗口(预览显示一个薄的白色条,如果程序被选为活动状态则不会显示)。

虽然不是主要问题,但这是我几年来第一次使用Swing,所以请随意指出其他错误。

1 个答案:

答案 0 :(得分:1)

getContentPane().setLayout(null);

此行表示您已禁用布局管理器。由于您不使用任何产品,因此您的容器不会自动调整组件大小,这就是您没有看到任何内容的原因。

如果您将此行更改为:

getContentPane().setLayout(new FlowLayout());

删除其他调用null JPanel对象布局的调用,您将看到正在运行的组件。

以下是guide,简要介绍了每个布局管理器的工作原理。