设置JPanel中固定的JTextField的大小 [英] Set the size of JTextField fixed in a JPanel
本文介绍了设置JPanel中固定的JTextField的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于学校,我正在使用Java GUI程序来存储一些管理数据.
for school I am working on a Java GUI program to store some administrative data.
现在,我想使用JTextField将来自数据库(mysql)的事件数据显示到JPanel中, 我的问题是我无法固定JTextField的大小,因为它总是占据很多位置(参见图片)
Now I want to display eventdata from my DB (mysql) into a JPanel with use of a JTextField, my problem is I can't get the size of the JTextField fixed as it always takes up a lot of place (see picture)
图片: http://postimg.org/image/5pcklo5n1/
这是我的代码,有人提示吗? (我是Java新手):
Here's my code, anyone some tips? (I am new to java):
public void editEvent() {
JFrame frEventEdit = new JFrame ("Event Edit Menu");
frEventEdit.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frEventEdit.setVisible(true);
frEventEdit.setSize(700, 500);
//JPanel for displaying data
JPanel pnData = new JPanel();
pnData.setLayout(new BoxLayout(pnData, BoxLayout.PAGE_AXIS));
pnData.add(Box.createRigidArea(new Dimension(0,5)));
pnData.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
pnData.setAutoscrolls(true);
Statement stmt;
try {
stmt = mySql.createStatement();
ResultSet rs = stmt.executeQuery("SELECT name, date, time, type, address, representative FROM events " ) ;
while (rs.next() == true){
System.out.println(rs.getString("name")+" "+rs.getString("date")+" "+rs.getString("time")+" "+rs.getString("type")+" "+rs.getString("address")+" "+rs.getString("representative"));
final JTextField txtEventList = new JTextField(rs.getString("name")+" "+rs.getString("date")+" "+rs.getString("time")+" "+rs.getString("type")+" "+rs.getString("address")+" "+rs.getString("representative"));
pnData.add(txtEventList, BorderLayout.CENTER);
}
} catch (SQLException e) {
e.printStackTrace();
}
JScrollPane scroller = new JScrollPane(pnData);
frEventEdit.add(scroller);
frEventEdit.setLocationRelativeTo(null);
}
预先感谢
推荐答案
- 发生这种情况是因为您使用
BoxLayout
,请尝试使用FlowLayout
,这是JPanel
或其他默认值. - 在下一个语句
pnData.add(txtEventList, BorderLayout.CENTER);
中,BorderLayout.CENTER
不起作用,因为您没有在面板上使用BorderLayout
. - 要固定
JTextField
的大小,请使用构造函数JTextField(int cols)
. - 根据您的目的,使用@AndrewThompson建议的
JTable
. 表格指南. -
在构建结束或下一步类似时呼叫
frEventEdit.setVisible(true);
:
- It happens because you use
BoxLayout
,try to useFlowLayout
which is default forJPanel
or another. - In next statement
pnData.add(txtEventList, BorderLayout.CENTER);
,BorderLayout.CENTER
doesn't work, because you doesn't useBorderLayout
for your panel. - For fixing size of
JTextField
, use constructorJTextField(int cols)
. - For your purposes use
JTable
as recommended by @AndrewThompson. Tutorial for table. call
frEventEdit.setVisible(true);
at the end of construction or like next:
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
frEventEdit.setVisible(true);
}
});
这篇关于设置JPanel中固定的JTextField的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文