gpt4 book ai didi

java - JFrame 窗口拒绝打开

转载 作者:行者123 更新时间:2023-12-01 08:57:44 26 4
gpt4 key购买 nike

我目前正在开发 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 任务栏中打开,但没有相应的窗口(预览显示一个细白色条,如果程序被选择为 Activity 状态,则不会显示)。

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

最佳答案

getContentPane().setLayout(null);

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

如果将此行更改为:

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

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

这是guide这简要解释了每个布局管理器的工作原理。

关于java - JFrame 窗口拒绝打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41930156/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com