2017-02-28 92 views
-2

我遇到了問題,JFrame未顯示添加到它的JTable。我試過getContentPane().add(..),我已經切換到只添加以保持代碼更短一些。任何幫助都不勝感激!JTable沒有顯示在JFrame上(Java)

package com.embah.Accgui; 

import java.awt.*; 
import javax.swing.*; 

public class accCreator extends JFrame { 
private String[] columnNames = {"Username", "Password", "Members", "World"}; 
private Object[][] data = {{"b", "b", "b", "b"}, 
          { "e", "e", "e", "e"}}; 
    private JTable tbl_Accounts; 
    private JScrollPane scrollPane; 
    private JLabel lbl_Account = new JLabel(); 
    private JLabel lbl_Username = new JLabel(); 
    private JLabel lbl_Password = new JLabel(); 
    private JLabel lbl_Homeworld = new JLabel(); 
    private JButton btn_Select = new JButton(); 
    private JButton btn_Addacc = new JButton(); 
    private JButton btn_Delacc = new JButton(); 
    private JTextArea txt_Username = new JTextArea(); 
    private JTextArea txt_Password = new JTextArea(); 
    private JTextArea txt_Homeworld = new JTextArea(); 
    private JCheckBox cbox_Members = new JCheckBox(); 
    private JCheckBox cbox_RanWrld = new JCheckBox(); 


public accCreator() { 
    setLayout(null); 
    setupGUI(); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
} 

void setupGUI() { 
    tbl_Accounts = new JTable(data, columnNames); 
    tbl_Accounts.setLocation(5, 30); 
    tbl_Accounts.setPreferredScrollableViewportSize(new Dimension(420, 250)); 
    tbl_Accounts.setFillsViewportHeight(true); 
    tbl_Accounts.setVisible(true); 
    add(tbl_Accounts); 
    scrollPane = new JScrollPane(tbl_Accounts); 
    add(scrollPane); 

    lbl_Account.setLocation(4, 5); 
    lbl_Account.setSize(100, 20); 
    lbl_Account.setText("Select Account:"); 
    add(lbl_Account); 

    lbl_Username.setLocation(5, 285); 
    lbl_Username.setSize(70, 20); 
    lbl_Username.setText("Username:"); 
    add(lbl_Username); 

    lbl_Password.setLocation(5, 310); 
    lbl_Password.setSize(70, 20); 
    lbl_Password.setText("Password:"); 
    add(lbl_Password); 

    lbl_Homeworld.setLocation(310, 310); 
    lbl_Homeworld.setSize(80, 20); 
    lbl_Homeworld.setText("Home World:"); 
    add(lbl_Homeworld); 

    btn_Select.setLocation(305, 5); 
    btn_Select.setSize(120, 20); 
    btn_Select.setText("Select Account"); 
    add(btn_Select); 

    btn_Addacc.setLocation(300, 285); 
    btn_Addacc.setSize(60, 20); 
    btn_Addacc.setText("Add"); 
    btn_Addacc.addActionListener(new ActionListener(){ 
     @Override 
     public void actionPerformed(ActionEvent arg0) { 
      String worldSel = ""; 
      if(cbox_RanWrld.isSelected()){ 
       worldSel = "Random"; 
      } else { 
       worldSel = txt_Homeworld.getText(); 
      } 
      Object[] row = {txt_Username.getText(), txt_Password.getText(), cbox_Members.isSelected(), worldSel}; 
      DefaultTableModel model = (DefaultTableModel) tbl_Accounts.getModel(); 
      model.addRow(row); 
     } 
    }); 
    add(btn_Addacc); 

    btn_Delacc.setLocation(365, 285); 
    btn_Delacc.setSize(60, 20); 
    btn_Delacc.setText("Del"); 
    btn_Delacc.addActionListener(new ActionListener(){ 
     @Override 
     public void actionPerformed(ActionEvent arg0) { 
      DefaultTableModel model = (DefaultTableModel) tbl_Accounts.getModel(); 

     } 
    }); 
    add(btn_Delacc); 

    txt_Username.setLocation(80, 285); 
    txt_Username.setSize(100, 20); 
    txt_Username.setText(""); 
    txt_Username.setRows(5); 
    txt_Username.setColumns(5); 
    add(txt_Username); 

    txt_Password.setLocation(80, 310); 
    txt_Password.setSize(100, 20); 
    txt_Password.setText(""); 
    txt_Password.setRows(5); 
    txt_Password.setColumns(5); 
    txt_Password.setTabSize(0); 
    add(txt_Password); 

    txt_Homeworld.setLocation(395, 310); 
    txt_Homeworld.setSize(30, 20); 
    txt_Homeworld.setText("82"); 
    txt_Homeworld.setRows(5); 
    txt_Homeworld.setColumns(5); 
    txt_Homeworld.setTabSize(0); 
    add(txt_Homeworld); 

    cbox_Members.setLocation(185, 285); 
    cbox_Members.setSize(80, 20); 
    cbox_Members.setText("Members"); 
    cbox_Members.setSelected(false); 
    add(cbox_Members); 

    cbox_RanWrld.setLocation(185, 310); 
    cbox_RanWrld.setSize(115, 20); 
    cbox_RanWrld.setText("Random World"); 
    cbox_RanWrld.setSelected(false); 
    add(cbox_RanWrld); 

    setTitle("Account Manager"); 
    setSize(440, 370); 
    setVisible(true); 
    setResizable(false); 

} 

public static void main(String args[]) { 
    new accCreator(); 
} 
} 
+0

好,'的setLayout(NULL);'是你的問題的開始 - 表也往往喜歡被包裹在'JScrollPane's – MadProgrammer

+0

你忘了調用'scrollPane.setLocation(...)'和'scrollPane.setSize(...)'。這是必要的,因爲你決定使用'setLayout(null)'。我希望你明白避免LayoutManagers是一個糟糕的決定。 –

+0

@ MadProgrammer - 我知道這不是問題,因爲一切都顯示得很好。 @托馬斯得到你生病嘗試,謝謝你! – Kushroom

回答

4

我知道那不是因爲壽一切顯示出來就好了

呵呵問題......真的嗎?不是在我的電腦...

讓我們在我的電腦上顯示您的實際GUI的畫面:

enter image description here

是否GUI看起來一樣在你的電腦?我敢打賭不。

但是...爲什麼它看起來像我的電腦?

那麼,正如@MadProgrammer評論中所述,這是因爲setLayout(null);行。您可能想要了解更多信息,請參閱Null layout is evilWhy is it frowned upon to use a null layout in Java Swing?

現在,這就是說,你還應該閱讀和學習如何使用各種layout managers,這將允許你創建複雜的圖形用戶界面。

在你的代碼永遠不會設置的位置/界scrollPane,和它的大小,使組件具有默認值爲0的大小,0

但是...我認爲這是更好地展示你你如何得到一個非常類似的GUI(我很匆忙,所以我沒有做出更類似的GUI)。您可以複製粘貼我的代碼並查看相同的輸出(由於操作系統的不同,可能會略有差異),但文本不會被裁剪。

enter image description here

產生上述圖像的代碼是這樣一個:

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.FlowLayout; 
import java.awt.GridBagConstraints; 
import java.awt.GridLayout; 

import javax.swing.BoxLayout; 
import javax.swing.JButton; 
import javax.swing.JCheckBox; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JPasswordField; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 
import javax.swing.JTextField; 
import javax.swing.SwingUtilities; 

public class AccountCreator { 

    private JFrame frame; 
    private JPanel mainPane; 
    private JPanel topPane; 
    private JPanel tablePane; 
    private JPanel bottomPane; 

    private JLabel selectAccountLabel; 
    private JLabel userNameLabel; 
    private JLabel passwordLabel; 
    private JLabel homeWorldLabel; 

    private JTextField userNameField; 
    private JTextField homeWorldField; 
    private JPasswordField passwordField; 

    private JCheckBox membersBox; 
    private JCheckBox randomBox; 

    private JButton selectAccountButton; 
    private JButton addButton; 
    private JButton deleteButton; 

    private JTable table; 

    private JScrollPane scroll; 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new AccountCreator().createAndShowGui(); 
      } 
     }); 
    } 

    public void createAndShowGui() { 
     frame = new JFrame(getClass().getSimpleName()); 

     int rows = 30; 
     int cols = 3; 

     String[][] data = new String[rows][cols]; 

     for (int i = 0; i < rows; i++) { 
      for (int j = 0; j < cols; j++) { 
       data[i][j] = i + "-" + j; 
      } 
     } 

     String[] columnNames = { "Column1", "Column2", "Column3" }; 

     table = new JTable(data, columnNames); 

     scroll = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 

     table.setPreferredScrollableViewportSize(new Dimension(420, 250)); 
     table.setFillsViewportHeight(true); 

     selectAccountLabel = new JLabel("Select Account"); 
     userNameLabel = new JLabel("Username: "); 
     passwordLabel = new JLabel("Password: "); 
     homeWorldLabel = new JLabel("Home world"); 

     selectAccountButton = new JButton("Select Account"); 
     addButton = new JButton("Add"); 
     deleteButton = new JButton("Del"); 

     userNameField = new JTextField(10); 
     passwordField = new JPasswordField(10); 
     homeWorldField = new JTextField(3); 

     membersBox = new JCheckBox("Members"); 
     randomBox = new JCheckBox("Random world"); 

     topPane = new JPanel(); 
     topPane.setLayout(new BorderLayout()); 

     topPane.add(selectAccountLabel, BorderLayout.WEST); 
     topPane.add(selectAccountButton, BorderLayout.EAST); 

     tablePane = new JPanel(); 
     tablePane.add(scroll); 

     bottomPane = new JPanel(); 
     bottomPane.setLayout(new GridLayout(0, 5, 3, 3)); 

     bottomPane.add(userNameLabel); 
     bottomPane.add(userNameField); 
     bottomPane.add(membersBox); 
     bottomPane.add(addButton); 
     bottomPane.add(deleteButton); 
     bottomPane.add(passwordLabel); 
     bottomPane.add(passwordField); 
     bottomPane.add(randomBox); 
     bottomPane.add(homeWorldLabel); 
     bottomPane.add(homeWorldField); 

     mainPane = new JPanel(); 
     mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.PAGE_AXIS)); 

     frame.add(topPane, BorderLayout.NORTH); 
     frame.add(tablePane, BorderLayout.CENTER); 
     frame.add(bottomPane, BorderLayout.SOUTH); 

     frame.pack(); 
     frame.setVisible(true); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 
} 

此外,可能已經注意到,main()方法是不同的,那麼,它裏面的代碼被放置程序在Event Dispatch Thread (EDT)

所以,一定把它列入你的未來計劃

+0

*「所以組件的preferredSize爲0,0)。」* - 實際上它可能有一個首選大小,它只是沒有物理大小(或默認大小爲0x0 ') – MadProgrammer

+0

@MadProgrammer Woops!感謝您的反饋。修正它:) – Frakcool

+0

不能說謝謝足夠的人,對不起,我是如此固執地聽你們早些時候。我只是想創造一個不錯的圖形用戶界面,而我整天都在試圖讓它工作,我非常沮喪。你已經向我展示了setLayout(null)非常可怕,不應該這樣做。我無法感謝你的投入和幫助!我非常感謝你們的帖子! – Kushroom