我在JScrollPane中有一个JTable。我希望最小宽度大约为600宽表。我尝试在表格,滚动窗格和面板上设置最小尺寸。大小根本没有变化,我错过了什么?很难谷歌这一点,因为所有出现的是如何设置列的宽度。
以下是代码:
class SearchResults extends JPanel {
/**
* Create the panel.
*/
public SearchResults() {
setMinimumSize(new Dimension(640, 480));
String[][] data= new String[][] {
{null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, "VIEW BUTTON"},
{null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, "VIEW BUTTON"}};
String[] col = new String[] {
"Last Name", "First Name", "Middle Initial", "Phone Number", "Email", "Project Title", "Project Description", "Amount", "Date Approved", "Date Completed", "College", "Faculty Mentor Name", "Co Grantee", "Major", "Travel Required", "Travel Purpose", "Travel Cost", "Travel Start Date", "Travel End Date", "View"};
JTable table = new JTable(data,col);
table.setMinimumSize(new Dimension(600,200));
JTableHeader header = table.getTableHeader();
JScrollPane pane = new JScrollPane(table);
pane.setMinimumSize(new Dimension(600, 23));
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
add(pane);
}
}
以下是我将其添加到JFrame的地方:
public class Test extends JFrame
{
public static void main(String[] args)
{
Test test = new Test();
test.run();
}
public Test()
{
super("JAVA TEST!");
}
private void run()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SearchResults resultsPanel = new SearchResults();
resultsPanel.setMinimumSize(new Dimension(600,200));
add(resultsPanel);
setSize(800,600);
setVisible(true);
}
}
答案 0 :(得分:8)
有几个问题:
Consequestly,有几个螺丝要调整(删除所有setXXSize调用后:))
在代码中(并使用SwingX项目的JXTable,因为它已经有第二个api :-))
String[][] data= new String[][] {
{null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, "VIEW BUTTON"},
{null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, "VIEW BUTTON"}};
String[] col = new String[] {
"Last Name", "First Name", "Middle Initial", "Phone Number",
"Email", "Project Title", "Project Description", "Amount",
"Date Approved", "Date Completed", "College", "Faculty Mentor Name",
"Co Grantee", "Major", "Travel Required", "Travel Purpose",
"Travel Cost", "Travel Start Date", "Travel End Date", "View"};
JXTable table = new JXTable(data,col);
table.setVisibleColumnCount(10);
table.setHorizontalScrollEnabled(true);
JScrollPane pane = new JScrollPane(table);
JComponent comp = new JPanel(new BorderLayout());
comp.add(pane);
修改强>
解决80%的要求(以及MigLayout的小预告:-))
// 80% with a minimum of 600 logical pixel:
MigLayout layout = new MigLayout("wrap 2, debug",
"[600:pref, fill, grow][20%]");
JComponent comp = new JPanel(layout);
comp.add(pane, "spany");
comp.add(new JLabel("just something"));
答案 1 :(得分:0)
这个东西也有一些问题。也使用setPreferedSize(new Dimension(800,600));
。
这可以解决你的问题
答案 2 :(得分:0)
布局很重要。像Netbeans或Eclipse这样的IDE通过拖放提供免提布局设计。
一旦理解Swing layouts,您甚至可以通过代码实现。我在这里提到的几乎没有最常用的布局
BorderLayout
BoxLayout
FlowLayout
GridLayout
在你的中,如果添加panel.setLayout( new GridLayout(1,1));
,表格将在框架内得到修复。
除非您覆盖某些内容或添加更多内容,否则您无需扩展JPanel
或JFrame
。练习学习。