我在Java Swing屏幕中遇到对齐问题。我在这里有borderLayout。我的要求是基于不同的按钮点击合适的查询将被执行并填充在JTable中。但是这个屏幕存在对齐问题,JTable隐藏了所有其他组件。以下是代码:
public class UI {
static JTextField inputText = new JTextField(20);
static JLabel errMsgLbl = new JLabel();
static PreparedStatement ptsmt;
static ResultSet rs;
static JButton nameButton;
static JButton accnoButton;
static JButton countryButton;
static JButton altaccButton;
static JButton showAllButton;
static JButton clearButton;
static JButton exitButton;
static JButton prevButton;
static JButton nextButton;
static JLabel searchBy;
static String[] columnNames = { "ID", "Name", "Original Name",
"Account Number", "Country", "Alternate Account No",
"Related Party Name", "Select" };
static JTable table;
static JComponent createHorizontalSeparator() {
JSeparator x = new JSeparator(SwingConstants.HORIZONTAL);
x.setForeground(Color.BLACK);
x.setPreferredSize(new Dimension(50, 3));
return x;
}
public static void main(String args[]) {
try {
DBConfig.loadEngineConfiguration();
} catch (Exception ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
final JTable table = new JTable(50, 8);
JPanel topPnl = new JPanel(new BorderLayout());
JPanel cntrPnl = new JPanel(new BorderLayout());
JPanel bottomPnl = new JPanel(new BorderLayout());
JPanel hdrPnl = new JPanel((LayoutManager) new FlowLayout(
FlowLayout.LEFT));
JPanel srchPnl = new JPanel((LayoutManager) new FlowLayout(
FlowLayout.LEADING));
JPanel topBtnPnl = new JPanel((LayoutManager) new FlowLayout(
FlowLayout.LEADING));
JPanel bottombtnPnl = new JPanel((LayoutManager) new FlowLayout(
FlowLayout.LEADING));
JPanel navbtnPnl = new JPanel((LayoutManager) new FlowLayout(
FlowLayout.TRAILING));
JPanel tblPnl = new JPanel((LayoutManager) new FlowLayout(
FlowLayout.CENTER));
hdrPnl.add(new JLabel("Welcome"));
srchPnl.add(new JLabel("Input text"));
srchPnl.add(inputText);
topPnl.add(hdrPnl, BorderLayout.NORTH);
topPnl.add(srchPnl, BorderLayout.SOUTH);
searchBy = new JLabel(DBConfig.searchBy);
nameButton = new JButton(DBConfig.nameBtn);
accnoButton = new JButton(DBConfig.accNoBtn);
countryButton = new JButton(DBConfig.countryBtn);
altaccButton = new JButton(DBConfig.altAccNoBtn);
showAllButton = new JButton(DBConfig.showBtn);
clearButton = new JButton(DBConfig.clearBtn);
exitButton = new JButton(DBConfig.exitBtn);
prevButton = new JButton(DBConfig.prevBtn);
nextButton = new JButton(DBConfig.nextBtn);
clearButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
inputText.setText(null);
errMsgLbl.setText(null);
}
});
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Container frame = exitButton.getParent();
do
frame = frame.getParent();
while (!(frame instanceof JFrame));
((JFrame) frame).dispose();
}
});
topBtnPnl.add(searchBy);
topBtnPnl.add(nameButton);
topBtnPnl.add(accnoButton);
topBtnPnl.add(countryButton);
topBtnPnl.add(altaccButton);
bottombtnPnl.add(showAllButton);
bottombtnPnl.add(clearButton);
bottombtnPnl.add(exitButton);
//cntrPnl.add(topBtnPnl, BorderLayout.NORTH);
//cntrPnl.add(bottombtnPnl, BorderLayout.SOUTH);
cntrPnl.add(topBtnPnl, BorderLayout.NORTH);
cntrPnl.add(bottombtnPnl, BorderLayout.CENTER);
cntrPnl.add(createHorizontalSeparator(), BorderLayout.SOUTH);
navbtnPnl.add(prevButton);
navbtnPnl.add(nextButton);
table.getTableHeader().setReorderingAllowed(false);
tblPnl.add(table, BorderLayout.PAGE_END);
bottomPnl.add(navbtnPnl, BorderLayout.CENTER);
bottomPnl.add(tblPnl, BorderLayout.CENTER);
//table.setTableHeader(columnNames);
frame.add(topPnl, BorderLayout.NORTH);
frame.add(cntrPnl, BorderLayout.CENTER);
frame.add(bottomPnl, BorderLayout.SOUTH);
frame.setTitle(DBConfig.appName);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setMinimumSize(new Dimension(600, 400));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
任何帮助将不胜感激。非常感谢。
答案 0 :(得分:1)
The BorderLayout supports only one component per region and you add navbtnPnl and tblPnl to the SOUTH region of bottomPnl, and tblPnl is the last one added which replaces the previously added navbtnPnl component.
如果您希望显示表头,也不应将JTable直接放在JPanel中。您应该将它添加到JScrollPane,它将正确显示标题。
答案 1 :(得分:0)
您使用的是错误的布局。使用GridBagLayout。我尝试过,这是部分工作示例
public class UI
{
static JTextField inputText = new JTextField(20);
static JLabel errMsgLbl = new JLabel();
static PreparedStatement ptsmt;
static ResultSet rs;
static JButton nameButton;
static JButton accnoButton;
static JButton countryButton;
static JButton altaccButton;
static JButton showAllButton;
static JButton clearButton;
static JButton exitButton;
static JButton prevButton;
static JButton nextButton;
static JLabel searchBy;
static String[] columnNames = {"ID", "Name", "Original Name", "Account Number", "Country", "Alternate Account No",
"Related Party Name", "Select"};
static JTable table;
static JComponent createHorizontalSeparator()
{
JSeparator x = new JSeparator(SwingConstants.HORIZONTAL);
x.setForeground(Color.BLACK);
x.setPreferredSize(new Dimension(50, 3));
return x;
}
public static void main(String args[])
{
try
{
// DBConfig.loadEngineConfiguration();
}
catch (Exception ex)
{
ex.printStackTrace();
}
JFrame frame = new JFrame();
frame.setLayout(new GridBagLayout());
final JTable table = new JTable(50, 8);
JPanel topPnl = new JPanel(new BorderLayout());
JPanel cntrPnl = new JPanel(new GridLayout(4, 1, 0, 0));
JPanel bottomPnl = new JPanel(new BorderLayout());
JPanel hdrPnl = new JPanel((LayoutManager) new FlowLayout(FlowLayout.LEFT));
JPanel srchPnl = new JPanel((LayoutManager) new FlowLayout(FlowLayout.LEADING));
JPanel topBtnPnl = new JPanel((LayoutManager) new FlowLayout(FlowLayout.LEADING));
JPanel bottombtnPnl = new JPanel((LayoutManager) new FlowLayout(FlowLayout.LEADING));
JPanel navbtnPnl = new JPanel((LayoutManager) new FlowLayout(FlowLayout.TRAILING));
JPanel tblPnl = new JPanel((LayoutManager) new FlowLayout(FlowLayout.CENTER));
hdrPnl.add(new JLabel("Welcome"));
srchPnl.add(new JLabel("Input text"));
srchPnl.add(inputText);
topPnl.add(hdrPnl, BorderLayout.NORTH);
topPnl.add(srchPnl, BorderLayout.SOUTH);
searchBy = new JLabel("searchBy");
nameButton = new JButton("nameBtn");
accnoButton = new JButton("accNoBtn");
countryButton = new JButton("countryBtn");
altaccButton = new JButton("altAccNoBtn");
showAllButton = new JButton("showBtn");
clearButton = new JButton("clearBtn");
exitButton = new JButton("exitBtn");
prevButton = new JButton("prevBtn");
nextButton = new JButton("nextBtn");
clearButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
inputText.setText(null);
errMsgLbl.setText(null);
}
});
exitButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Container frame = exitButton.getParent();
do
frame = frame.getParent();
while (!(frame instanceof JFrame));
((JFrame) frame).dispose();
}
});
topBtnPnl.add(searchBy);
topBtnPnl.add(nameButton);
topBtnPnl.add(accnoButton);
topBtnPnl.add(countryButton);
topBtnPnl.add(altaccButton);
bottombtnPnl.add(showAllButton);
bottombtnPnl.add(clearButton);
bottombtnPnl.add(exitButton);
cntrPnl.add(topBtnPnl);
cntrPnl.add(bottombtnPnl);
cntrPnl.add(createHorizontalSeparator(), BorderLayout.SOUTH);
navbtnPnl.add(prevButton);
navbtnPnl.add(nextButton);
table.getTableHeader().setReorderingAllowed(false);
cntrPnl.add(table, BorderLayout.PAGE_END);
bottomPnl.add(navbtnPnl, BorderLayout.CENTER);
bottomPnl.add(tblPnl, BorderLayout.CENTER);
// table.setTableHeader(columnNames);
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1;
// c.weighty = 10;
c.gridx = 0;
c.gridy = 0;
frame.add(topPnl, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40; // make this component tall
c.weightx = 0.2;
c.weighty = 80;
c.weightx = 0.0;
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 1;
frame.add(cntrPnl, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.weighty = 0;
c.gridx = 0;
c.gridy = 2;
frame.add(bottomPnl, c);
frame.setTitle("appName");
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setMinimumSize(new Dimension(600, 400));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}