如何使用 BorderLayout 在 JPanel 中定位对象?
我有以下实现 3 个 JPanel 的类。 1 个面板有一个标签,接下来是按钮,第三个是表格,如我的代码中所述:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
import java.util.*;
import javax.swing.event.*;
class netTable implements ActionListener, TableModelListener
{
JFrame frame;
JTable table;
Vector rows,columns;
DefaultTableModel tabModel;
JScrollPane scrollPane;
JLabel lblMessage;
JButton cmdLookup, cmdUpdatePlan;
JPanel topPanel,mainPanel,buttonPanel;
public static void main(String[] args)
{
netTable t=new netTable();
}
netTable()
{
rows=new Vector();
columns= new Vector();
String[] columnNames =
{
"ID",
"Client",
"Plan",
"Amount"
};
addColumns(columnNames);
tabModel=new DefaultTableModel();
tabModel.setDataVector(rows,columns);
table = new JTable(tabModel);
scrollPane= new JScrollPane(table);//ScrollPane
table.setRowSelectionAllowed(false);
table.getModel().addTableModelListener(this);
topPanel = new JPanel();
lblMessage=new JLabel("Invoices to Update");
topPanel.add(lblMessage);
buttonPanel=new JPanel();
cmdLookup=new JButton("Lookup");
cmdUpdatePlan = new JButton("Update Plan");
buttonPanel.add(cmdLookup);
buttonPanel.add(cmdUpdatePlan);
cmdLookup.addActionListener(this);
cmdUpdatePlan.addActionListener(this);
mainPanel=new JPanel();
frame=new JFrame("Update Table");
frame.setSize(500,500);
frame.setExtendedState(JFrame.ICONIFIED);
mainPanel.setLayout(new BorderLayout());
mainPanel.add(topPanel,BorderLayout.NORTH);
mainPanel.add(buttonPanel,BorderLayout.CENTER);
mainPanel.add(scrollPane,BorderLayout.SOUTH);
topPanel.setBackground(Color.gray);
mainPanel.setBackground(Color.white);
buttonPanel.setBackground(Color.white);
table.getParent().setBackground(Color.black);
frame.getContentPane().add(mainPanel);
frame.addWindowListener(new WindowCloser());
frame.setVisible(true);
}
}
当我编译此面板时,它会在顶部显示buttonPanel,一个空格,然后在其下面显示scrollPane,忽略topPanel所在的标签应该首先显示在顶部。有什么想法吗?我认为 BorderLayout 位置错误。
I have the following class which implements 3 JPanels. 1 Panel has a label, next are the buttons and the third is a table as described in my code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
import java.util.*;
import javax.swing.event.*;
class netTable implements ActionListener, TableModelListener
{
JFrame frame;
JTable table;
Vector rows,columns;
DefaultTableModel tabModel;
JScrollPane scrollPane;
JLabel lblMessage;
JButton cmdLookup, cmdUpdatePlan;
JPanel topPanel,mainPanel,buttonPanel;
public static void main(String[] args)
{
netTable t=new netTable();
}
netTable()
{
rows=new Vector();
columns= new Vector();
String[] columnNames =
{
"ID",
"Client",
"Plan",
"Amount"
};
addColumns(columnNames);
tabModel=new DefaultTableModel();
tabModel.setDataVector(rows,columns);
table = new JTable(tabModel);
scrollPane= new JScrollPane(table);//ScrollPane
table.setRowSelectionAllowed(false);
table.getModel().addTableModelListener(this);
topPanel = new JPanel();
lblMessage=new JLabel("Invoices to Update");
topPanel.add(lblMessage);
buttonPanel=new JPanel();
cmdLookup=new JButton("Lookup");
cmdUpdatePlan = new JButton("Update Plan");
buttonPanel.add(cmdLookup);
buttonPanel.add(cmdUpdatePlan);
cmdLookup.addActionListener(this);
cmdUpdatePlan.addActionListener(this);
mainPanel=new JPanel();
frame=new JFrame("Update Table");
frame.setSize(500,500);
frame.setExtendedState(JFrame.ICONIFIED);
mainPanel.setLayout(new BorderLayout());
mainPanel.add(topPanel,BorderLayout.NORTH);
mainPanel.add(buttonPanel,BorderLayout.CENTER);
mainPanel.add(scrollPane,BorderLayout.SOUTH);
topPanel.setBackground(Color.gray);
mainPanel.setBackground(Color.white);
buttonPanel.setBackground(Color.white);
table.getParent().setBackground(Color.black);
frame.getContentPane().add(mainPanel);
frame.addWindowListener(new WindowCloser());
frame.setVisible(true);
}
}
When I compile this, it displays the buttonPanel on top, a space, and then the scrollPane below it, leaving out the label where topPanel is supposed to display first at the top. Any ideas? I'm thinking the BorderLayout position is wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这些面板没有空间,因此顶部面板被压缩成大约 3 个像素。尝试
一下。
There isn't room for those panels so the top panel is squished into about 3 pixels. Try
instead.
问题似乎是这一行:
出于某种原因,如果在将组件添加到框架之前运行此操作,topPanel 将消失。
您可以执行以下操作之一:
将整个构造函数放入 try 块中,并在 finally 中设置扩展状态。
(推荐)在构建完所有内容后设置扩展状态(使用其他方法或直接在 main 中)。您还应该将诸如
frame.setVisible(true);
之类的内容移至此类方法。The problem seems to be this line:
For some reason, if this is run before the components are added to your frame, topPanel will disappear.
You can do one of these:
Chuck your entire constructor in a try block, with the extended state being set in finally.
(recommended) Set the extended state after everything has been constructed (using another method or directly in main). You should also move things like
frame.setVisible(true);
to such a method as well.中心面板将尽可能多地占用可用空间。如果您希望滚动窗格“最大化”,并在顶部标签和滚动窗格之间添加按钮,请创建另一个具有边框布局的 JPanel,将按钮添加到北部,将滚动窗格添加到中心,然后将整个面板添加到你的
mainPanel
Center panel will eat as much of the available space as possible. If you want to have a scroll pane "maximized" with buttons between the top label and the scroll pane, create another JPanel with border layout, add buttons to the north and scroll pane to the center, and add the whole panel to the center of your
mainPanel