2010-09-17 48 views
1

我非常卡住。希望這裏有一個善良的人可以幫助我用一行代碼來使我的平板顯示器進一步穿過它的面板。目前它在屏幕中間保持非常小的狀態!我嘗試了很多方法,最近在嘗試改變列的寬度時失敗了(因爲反正頁面/ gui的大小是固定的)。無論如何,JTable的代碼位於底部。我希望你能有所幫助!它只有一個方法和類,所以它很容易複製和粘貼和運行(如果你嫌麻煩的是,如果你不能我明白了!)jtable列太小

import javax.swing.*; 
import javax.swing.table.TableColumn; 

import java.awt.*; 

public class second extends JFrame 
{ 
/** 
    * 
    */ 
private static final long serialVersionUID = 1L; 

public second() 
{ 

} 

public static void main(String[] args) 
{ 
    second sec = new second(); 
    sec.createFrame(); 
} 

@SuppressWarnings("deprecation") 
private void createFrame() 
{ 
    JFrame frame = new JFrame("Second attempt"); 
    JPanel content = new JPanel(new BorderLayout(5,10)); 
    frame.getContentPane().add(content); 
    frame.setResizable(false); 

    JPanel requestor = new JPanel(new GridLayout(1, 4, 15, 20)); 
    content.add("North", requestor); 
    requestor.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.gray), "Requestor")); 

    JPanel col1 = new JPanel(); 
     BoxLayout boxLayout1 = new BoxLayout(col1, BoxLayout.Y_AXIS); 
     col1.setLayout(boxLayout1); 


     JPanel col2 = new JPanel(); 
     BoxLayout boxLayout2 = new BoxLayout(col2, BoxLayout.Y_AXIS); 
     col2.setLayout(boxLayout2); 


     JPanel col3 = new JPanel(); 
     BoxLayout boxLayout3 = new BoxLayout(col3, BoxLayout.Y_AXIS); 
     col3.setLayout(boxLayout3); 

     JPanel col4 = new JPanel(); 
     BoxLayout boxLayout4 = new BoxLayout(col4, BoxLayout.Y_AXIS); 
     col4.setLayout(boxLayout4); 

     requestor.add(col1); 
     requestor.add(col2); 
     requestor.add(col3); 
     requestor.add(col4);  

     //create JLabels 
     JLabel requestorId = new JLabel("RequestorID");  
     JLabel org = new JLabel("Organisation"); 
     JLabel orgAddress = new JLabel("Organisation Address"); 
     JLabel otherAdd = new JLabel("Other Address"); 
     JLabel title = new JLabel("Title"); 
     JLabel phoneNo = new JLabel("Phone Number"); 
     JLabel firstName = new JLabel("First Name"); 
     JLabel surname = new JLabel("Surname"); 
     JLabel emailAdd = new JLabel("E Mail Address"); 
     JLabel dept = new JLabel("Department"); 
     JLabel fax = new JLabel("Fax Number"); 
     JLabel spacer = new JLabel("Spacer"); 


     //create text areas 
     JTextField reqIDTxt = new JTextField(); 


     JTextField orgTxt = new JTextField(); 
     JTextField orgAddTxt = new JTextField(); 
     JTextField otherAddTxt = new JTextField(); 
     JTextField titleTxt = new JTextField(); 
     JTextField phoneNoTxt = new JTextField(); 
     JTextField firstNameTxt = new JTextField(); 
     JTextField faxNoTxt = new JTextField(); 
     JTextField surnameTxt = new JTextField(); 
     JTextField emailTxt = new JTextField(); 
     JTextField deptTxt = new JTextField(); 
     JTextField spacerTxt = new JTextField(); 


     col1.add(requestorId); 
     col1.add(reqIDTxt); 
     col1.add(title); 
     col1.add(titleTxt); 
     col1.add(firstName); 
     col1.add(firstNameTxt); 
     col1.add(surname); 
     col1.add(surnameTxt); 
     col1.add(dept); 
     col1.add(deptTxt); 

     col2.add(org); 
     col2.add(orgTxt); 
     col2.add(phoneNo); 
     col2.add(phoneNoTxt); 
     col2.add(fax); 
     col2.add(faxNoTxt); 
     col2.add(emailAdd); 
     col2.add(emailTxt); 
     col2.add(spacer); 
     col2.add(spacerTxt); 

     col3.add(orgAddress); 
     col3.add(orgAddTxt); 

     col4.add(otherAdd); 
     col4.add(otherAddTxt); 

    JPanel buttonsPanel = new JPanel(new BorderLayout()); 
    content.add("Center", buttonsPanel); 

    JPanel buttons = new JPanel(new FlowLayout()); 
    buttonsPanel.add("North", buttons); 

    buttons.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.gray))); 

     JButton addBtn = new JButton("Add"); 
     JButton deleteBtn = new JButton("Delete"); 
     JButton clearAllBbtn = new JButton("Clear All"); 
     JButton copyReqBtn = new JButton("Copy Requestor"); 
     buttons.add(addBtn); 
     buttons.add(deleteBtn); 
     buttons.add(clearAllBbtn); 
     buttons.add(copyReqBtn); 

    JPanel checkBoxPanel = new JPanel(new BorderLayout()); 
    buttonsPanel.add("Center", checkBoxPanel); 

    JPanel checkBoxHolder = new JPanel(new BorderLayout()); 
    checkBoxPanel.add("North", checkBoxHolder); 


    JCheckBox chkBox = new JCheckBox("Search"); 
     checkBoxHolder.add(chkBox); 

     JPanel bottomHalf = new JPanel(new BorderLayout()); 
     checkBoxPanel.add("Center",bottomHalf); 


    //--------------------------------------------------------------------------------------------------------------------- 
     Dimension d = new Dimension(100, 200); 

     JPanel splitForTable = new JPanel(); 
     BoxLayout lay = new BoxLayout(splitForTable, BoxLayout.X_AXIS); 
     splitForTable.setLayout(lay); 
     bottomHalf.add("North",splitForTable); 

     JPanel rbtn = new JPanel(); 
     BoxLayout layoutRbtn = new BoxLayout(rbtn, BoxLayout.Y_AXIS); 
     rbtn.setLayout(layoutRbtn); 
     rbtn.setPreferredSize(d); 
     splitForTable.add(rbtn); 
     rbtn.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.gray), "Search Criteria")); 

     JTextField btnBox = new JTextField(); 
     btnBox.setMaximumSize(new Dimension(10500, 20)); 
     ButtonGroup group = new ButtonGroup(); 
     btnBox.setColumns(15); 
     JRadioButton firstNameRbtn = new JRadioButton("First Name"); 
     JRadioButton surnameRbtn = new JRadioButton("Surname"); 
     JRadioButton orgNameRbtn = new JRadioButton("Organisation Name"); 
     JRadioButton deptRbtn = new JRadioButton("Department"); 
     JRadioButton addresRbtn = new JRadioButton("Address"); 
     JRadioButton addresOtherRbtn = new JRadioButton("Address Other"); 

     group.add(firstNameRbtn); 
     group.add(surnameRbtn); 
     group.add(orgNameRbtn); 
     group.add(deptRbtn); 
     group.add(addresRbtn); 
     group.add(addresOtherRbtn); 

     rbtn.add(btnBox); 
     rbtn.add(firstNameRbtn); 
     rbtn.add(surnameRbtn); 
     rbtn.add(orgNameRbtn); 
     rbtn.add(deptRbtn); 
     rbtn.add(addresRbtn); 
     rbtn.add(addresOtherRbtn); 

     rbtn.add(Box.createVerticalGlue()); 
     JPanel jtablePanel = new JPanel(new FlowLayout()); 

     splitForTable.add(jtablePanel); 
     jtablePanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.gray), "Results")); 


     //construct dummy data for table at the moment 
     Object[][] data = { 
      {"Kathy", "Smith", 
       "Snowboarding", new Integer(5), new Boolean(false)}, 
      {"John", "Doe", 
       "Rowing", new Integer(3), new Boolean(true)}, 
      {"Sue", "Black", 
       "Knitting", new Integer(2), new Boolean(false)}, 
      {"Jane", "White", 
       "Speed reading", new Integer(20), new Boolean(true)}, 
      {"Joe", "Brown", 
       "Pool", new Integer(10), new Boolean(false)}, 
       {"Ton", "Parker", 
        "Pool", new Integer(10), new Boolean(false)}, 
       {"Sky", "Sports", 
        "Pool", new Integer(12), new Boolean(true)}, 
       {"Ricki", "Lambert", 
        "Football", new Integer(10), new Boolean(true)}, 
       {"Simon", "Gayton", 
        "Chemistry", new Integer(15), new Boolean(false)}, 
       {"Tom", "Andrews", 
        "Golf", new Integer(45), new Boolean(false)}, 
       {"Lionel", "Messi", 
        "Football", new Integer(10), new Boolean(true)}, 
       {"Tom", "Andrews", 
         "Golf", new Integer(45), new Boolean(false)}, 
       {"Tom", "Andrews", 
         "Golf", new Integer(45), new Boolean(false)}, 
       {"Tom", "Andrews", 
          "Golf", new Integer(45), new Boolean(false)}, 
     }; 

     String[] columnNames = {"First Name", 
       "Last Name", 
       "Sport", 
       "# of Years", 
       "Vegetarian"}; 





     //THIS IS WHERE ALL TABLE INFORMATION IS USED 
     JTable table = new JTable(data, columnNames); 
     JScrollPane scrollPane = new JScrollPane(table); 
     scrollPane.setPreferredSize(new Dimension(250, 200)); 
     table.setFillsViewportHeight(true); 

     //scrollPane.add(table); 
     jtablePanel.add(scrollPane); 

     JPanel southBtn = new JPanel(new FlowLayout()); 
     content.add("South", southBtn); 

     JButton bottomOkBtn = new JButton("Ok"); 
     JButton bottomCancelBtn = new JButton("Cancel"); 
     JButton bottomApplyBtn = new JButton("Apply"); 

     southBtn.add(bottomOkBtn); 
     southBtn.add(bottomCancelBtn); 
     southBtn.add(bottomApplyBtn); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.pack(); 
    frame.show(); 
} 

} 

回答

1

問題是因爲該行的 scrollPane.setPreferredSize(new Dimension(250, 200)); 你是在此設置滾動窗格的寬度。

我註釋掉了這一行,表格填充了「結果」面板。

但是:
代碼中有很多錯誤。
例如最後一行中的frame.show();:不推薦使用show方法。使用setVisible方法
您正在使用地址爲JTextField,這意味着沒有新行,整個地址將在一行中。嘗試使用JTextAreaJScrollPane

有可能是其他人,但我沒有完全看代碼。
如果你想快速開發UI,我建議使用具有拖放Swing開發(eclipsevisual editor,或netbeans

1

JTable中有一個列模型

您可以設置所需的列寬的IDE在它上面

TableColumnModel colModel=table_.getColumnModel(); 
TableColumn col=colModel.getColumn(i); 
col.setPreferredWidth(w);