Java JFrame中没有显示任何内容
我想创建一个gui,它在顶部有两个水平组件(一个组合框和一个按钮),在底部我想添加几个组件。我创造了这样的一切:Java JFrame中没有显示任何内容,java,swing,user-interface,layout,border-layout,Java,Swing,User Interface,Layout,Border Layout,我想创建一个gui,它在顶部有两个水平组件(一个组合框和一个按钮),在底部我想添加几个组件。我创造了这样的一切: import java.awt.BorderLayout; import java.awt.Button; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSeparator;
public class minimumExample extends JFrame {
private JButton addItem;
private JComboBox itemBox;
private String[] itemSelect = { "test1", "test2" };
private JPanel addUpperPane;
private JPanel addLowerPane;
public void createControlPane() {
setLayout(new BorderLayout());
addUpperPane = new JPanel(new BorderLayout(5, 5));
addLowerPane = new JPanel(new GridLayout(0, 1));
addItem = new JButton("Add item");
itemBox = new JComboBox(itemSelect);
addItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
getContentPane().setLayout(new GridLayout(0, 1));
if(itemBox.getSelectedItem().toString().equals("test1")) {
addLowerPane.add(new Button("Lolonator"));
validate();
repaint();
}
}
});;
addUpperPane.add(itemBox);
addUpperPane.add(addItem);
addUpperPane.add(new JSeparator(JSeparator.HORIZONTAL));
//put everything together
add(addUpperPane);
add(addLowerPane);
repaint();
}
private void makeLayout() {
setTitle("Test App");
setLayout(new BorderLayout());
setPreferredSize(new Dimension(1000, 500));
createControlPane();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
/**
* starts the GUI
*/
public void start() {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
makeLayout();
}
});
}
public static void main(String[] args) throws IOException {
minimumExample ex = new minimumExample();
ex.start();
}
}
我的问题是没有显示任何内容,而且我还发现布局不正确。有什么建议我应该改变什么来解决我的问题吗
谢谢你的回答
更新
以下是我的gui的简单线框:
更新2
将所有内容更改为:
addUpperPane.add(itemBox, BorderLayout.EAST);
addUpperPane.add(addItem, BorderLayout.WEST);
addUpperPane.add(new JSeparator(JSeparator.HORIZONTAL));
//put everything together
add(addUpperPane, BorderLayout.NORTH);
add(addLowerPane, BorderLayout.SOUTH);
告诉我:
有没有关于如何消除间隙的建议?因为您使用了
BorderLayout
,所以您需要指定布局中每个组件的位置。您要做的是,您只需将所有组件添加到布局的同一位置,默认情况下,该位置为中心
解决方案:
addUpperPane.add(itemBox,BorderLayout.EAST);
addUpperPane.add(addItem,BorderLayout.WEST);
addUpperPane.add(new JSeparator(JSeparator.HORIZONTAL));
//put everything together
add(addUpperPane,BorderLayout.NORTH);
add(addLowerPane,BorderLayout.SOUTH);
getContentPane().setLayout(new GridLayout(0, 1)); //remove it
addItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(itemBox.getSelectedItem().toString().equals("test1")) {
addLowerPane.add(new Button("Lolonator"));
revalidate();
}
}
});;
另外,这也不会产生错误,因为setLayout(newborderlayout())
JFrame的默认布局已经是BorderLayout
,因此无需再次设置布局
编辑:
addUpperPane.add(itemBox,BorderLayout.EAST);
addUpperPane.add(addItem,BorderLayout.WEST);
addUpperPane.add(new JSeparator(JSeparator.HORIZONTAL));
//put everything together
add(addUpperPane,BorderLayout.NORTH);
add(addLowerPane,BorderLayout.SOUTH);
getContentPane().setLayout(new GridLayout(0, 1)); //remove it
addItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(itemBox.getSelectedItem().toString().equals("test1")) {
addLowerPane.add(new Button("Lolonator"));
revalidate();
}
}
});;
如果您希望您的组件并排放置,那么FlowLayout
是一种方法:
addUpperPane = new JPanel(); //default component of JPanel is FlowLayout
addUpperPane.add(itemBox);
addUpperPane.add(addItemT);
编辑编号2:
addUpperPane.add(itemBox,BorderLayout.EAST);
addUpperPane.add(addItem,BorderLayout.WEST);
addUpperPane.add(new JSeparator(JSeparator.HORIZONTAL));
//put everything together
add(addUpperPane,BorderLayout.NORTH);
add(addLowerPane,BorderLayout.SOUTH);
getContentPane().setLayout(new GridLayout(0, 1)); //remove it
addItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(itemBox.getSelectedItem().toString().equals("test1")) {
addLowerPane.add(new Button("Lolonator"));
revalidate();
}
}
});;
问题:
addUpperPane.add(itemBox,BorderLayout.EAST);
addUpperPane.add(addItem,BorderLayout.WEST);
addUpperPane.add(new JSeparator(JSeparator.HORIZONTAL));
//put everything together
add(addUpperPane,BorderLayout.NORTH);
add(addLowerPane,BorderLayout.SOUTH);
getContentPane().setLayout(new GridLayout(0, 1)); //remove it
addItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(itemBox.getSelectedItem().toString().equals("test1")) {
addLowerPane.add(new Button("Lolonator"));
revalidate();
}
}
});;
示例:
addUpperPane.add(itemBox,BorderLayout.EAST);
addUpperPane.add(addItem,BorderLayout.WEST);
addUpperPane.add(new JSeparator(JSeparator.HORIZONTAL));
//put everything together
add(addUpperPane,BorderLayout.NORTH);
add(addLowerPane,BorderLayout.SOUTH);
getContentPane().setLayout(new GridLayout(0, 1)); //remove it
addItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(itemBox.getSelectedItem().toString().equals("test1")) {
addLowerPane.add(new Button("Lolonator"));
revalidate();
}
}
});;
由于您使用了BorderLayout
,因此需要指定布局中每个组件的位置。您所做的是仅将所有组件添加到布局的同一位置,默认情况下,该位置为CENTER
解决方案:
addUpperPane.add(itemBox,BorderLayout.EAST);
addUpperPane.add(addItem,BorderLayout.WEST);
addUpperPane.add(new JSeparator(JSeparator.HORIZONTAL));
//put everything together
add(addUpperPane,BorderLayout.NORTH);
add(addLowerPane,BorderLayout.SOUTH);
getContentPane().setLayout(new GridLayout(0, 1)); //remove it
addItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(itemBox.getSelectedItem().toString().equals("test1")) {
addLowerPane.add(new Button("Lolonator"));
revalidate();
}
}
});;
另外,这也不会产生错误,因为setLayout(newborderlayout())
JFrame的默认布局已经是BorderLayout
,因此无需再次设置布局
编辑:
addUpperPane.add(itemBox,BorderLayout.EAST);
addUpperPane.add(addItem,BorderLayout.WEST);
addUpperPane.add(new JSeparator(JSeparator.HORIZONTAL));
//put everything together
add(addUpperPane,BorderLayout.NORTH);
add(addLowerPane,BorderLayout.SOUTH);
getContentPane().setLayout(new GridLayout(0, 1)); //remove it
addItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(itemBox.getSelectedItem().toString().equals("test1")) {
addLowerPane.add(new Button("Lolonator"));
revalidate();
}
}
});;
如果您希望您的组件并排放置,那么FlowLayout
是一种方法:
addUpperPane = new JPanel(); //default component of JPanel is FlowLayout
addUpperPane.add(itemBox);
addUpperPane.add(addItemT);
编辑编号2:
addUpperPane.add(itemBox,BorderLayout.EAST);
addUpperPane.add(addItem,BorderLayout.WEST);
addUpperPane.add(new JSeparator(JSeparator.HORIZONTAL));
//put everything together
add(addUpperPane,BorderLayout.NORTH);
add(addLowerPane,BorderLayout.SOUTH);
getContentPane().setLayout(new GridLayout(0, 1)); //remove it
addItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(itemBox.getSelectedItem().toString().equals("test1")) {
addLowerPane.add(new Button("Lolonator"));
revalidate();
}
}
});;
问题:
addUpperPane.add(itemBox,BorderLayout.EAST);
addUpperPane.add(addItem,BorderLayout.WEST);
addUpperPane.add(new JSeparator(JSeparator.HORIZONTAL));
//put everything together
add(addUpperPane,BorderLayout.NORTH);
add(addLowerPane,BorderLayout.SOUTH);
getContentPane().setLayout(new GridLayout(0, 1)); //remove it
addItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(itemBox.getSelectedItem().toString().equals("test1")) {
addLowerPane.add(new Button("Lolonator"));
revalidate();
}
}
});;
示例:
addUpperPane.add(itemBox,BorderLayout.EAST);
addUpperPane.add(addItem,BorderLayout.WEST);
addUpperPane.add(new JSeparator(JSeparator.HORIZONTAL));
//put everything together
add(addUpperPane,BorderLayout.NORTH);
add(addLowerPane,BorderLayout.SOUTH);
getContentPane().setLayout(new GridLayout(0, 1)); //remove it
addItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(itemBox.getSelectedItem().toString().equals("test1")) {
addLowerPane.add(new Button("Lolonator"));
revalidate();
}
}
});;
Rod_Algonquin的解决方案是正确的。
如果您打算构建更多GUI,我可以向您推荐MigLayout。它可以帮助您编写漂亮的GUI,从而减少可读代码的行数
他们的主页:罗杜•阿尔冈金的解决方案是正确的。
如果您打算构建更多GUI,我可以向您推荐MigLayout。它可以帮助您编写漂亮的GUI,从而减少可读代码的行数
他们的主页:只能向边框布局的每个区域添加一个组件。AFAIU上面所有显示的唯一组件是分隔符。提供ASCII艺术或简单绘图,说明GUI应如何以默认大小显示,以及(如果可调整大小)如何以额外的宽度/高度显示。@AndrewThompson Thx供您回答!更改此选项:`setLayout(newflowlayout());addUpperPane=newJPanel(newFlowLayout());`不会改变我程序中的任何内容。任何我应该更改的建议?提供ASCII艺术或简单的图形,说明GUI如何以默认大小显示,以及(如果可以调整大小)如何以额外的宽度/高度显示。@AndrewThompson请查看我的更新<代码>设置首选尺寸(新尺寸(1000500))代码>请参见(是)。在添加所有组件后,我们应该调用pack()
。只能将一个组件添加到边界布局的每个区域。AFAIU上面所有显示的唯一组件是分隔符。提供ASCII艺术或简单绘图,说明GUI应如何以默认大小显示,以及(如果可调整大小)如何以额外的宽度/高度显示。@AndrewThompson Thx供您回答!更改此选项:`setLayout(newflowlayout());addUpperPane=newJPanel(newFlowLayout());`不会改变我程序中的任何内容。任何我应该更改的建议?提供ASCII艺术或简单的图形,说明GUI如何以默认大小显示,以及(如果可以调整大小)如何以额外的宽度/高度显示。@AndrewThompson请查看我的更新<代码>设置首选尺寸(新尺寸(1000500))代码>请参见(是。)在添加所有组件后,我们应该在此处调用pack()
“…JFrame的默认布局已经是BorderLayout
,因此无需再次设置布局。JFrame
的默认布局过去是FlowLayout
。所以我说“改变默认值并设置一个”。另外请注意,最好在基于指南针的常量上使用区分区域设置的PAGE\u START
,LINE\u START
等。@AndrewThompson我记得你以前对我说过,:)。@Rod\u Algonquin Thx作为你的答案!在我的gui中使用您的布局时,为什么会有这么大的差距?对于上面的窗格,我使用了默认的流布局。Thx作为您的答案!我还更新了我的图像。为什么上下窗格之间有这么大的间隙?有什么建议吗?谢谢!刚刚更改为JScrollable,它不支持GridBack,任何建议我应该在这里选择哪个布局?…JFrame的默认布局已经是BorderLayout
,因此无需再次设置布局。JFrame
的默认布局以前是FlowLayout
。所以我说“改变默认值并设置一个”。另外请注意,最好在基于指南针的常量上使用区分区域设置的PAGE\u START
,LINE\u START
等。@AndrewThompson我记得你以前对我说过,:)。@Rod\u Algonquin Thx作为你的答案!在我的gui中使用您的布局时,为什么会有这么大的差距?对于上面的窗格,我使用了默认的流布局。Thx作为您的答案!我还更新了我的图像。为什么上下窗格之间有这么大的间隙?有什么建议吗?谢谢!刚刚改为JScrollable,它不支持GridBack,有什么建议我应该在这里选择哪种布局吗?