2017-02-28 2 views
-2

JTable이 표시되지 않는 JFrame에 문제가 있습니다. 내가 getContentPane().add(..)을 시도했지만 코드를 조금 더 짧게 유지하기 위해 전환했습니다. 어떤 도움이라도 인정받을 만합니다!JFrame (Java)에 JTable이 표시되지 않습니다.

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의 (널)에 포함해야는, '문제의 시작이 될 것입니다 - 테이블도'JScrollPane's – MadProgrammer

+0

당신을 감싸 좋아하는 경향이 'scrollPane.setLocation (...)'과'scrollPane.setSize (...)'를 호출하는 것을 잊었습니다. 이것은'setLayout (null)'을 사용하기로 결정했기 때문에 필요합니다. 이제 LayoutManagers를 피하는 것이 나쁜 결정이라는 것을 이해하시기 바랍니다. –

+0

@ MadProgrammer - 그 밖의 모든 것들이 잘 나타나기 때문에 나는 그게 문제가 아니라는 것을 안다. @ Thomas 나중에 고마워! – Kushroom

답변

4

는 정말 ... 문제가 아니라 다른 모든

아 잘 나타나 있기 때문에 상점 그게 전부 알아? 아니 내 컴퓨터에 ...

이의 내 PC에 표시된 실제 GUI의 사진을 보자 :

enter image description here

는 GUI가 컴퓨터에 동일한 보이는합니까? 나는 안한다.

하지만 ... 내 PC에서 그렇게 보이는 이유는 무엇입니까?

글쎄, 위의 @MadProgrammer에 의한 설명은 setLayout(null); 라인 때문입니다. 자세한 내용은 Null layout is evilWhy is it frowned upon to use a null layout in Java Swing?을 읽어보십시오.

복잡한 GUI를 만들 수있는 다양한 layout managers을 사용하는 방법을 읽고 배우는 것이 좋습니다. 코드에서

당신은 scrollPane의 위치/경계를 설정하지 않고, 그것의 크기 때문에 구성 요소는 0

0의 기본 크기는하지만 ... 난 당신을 보여주기 위해 더 나은 것 같아요 결코 정말 비슷한 GUI를 얻을 수있는 방법 (나는 서둘러 그래서 좀 더 비슷한 GUI를 만들지 않았다). 내 코드를 복사하여 붙여 넣거나 동일한 출력을 볼 수는 있지만 (OS 때문에 약간의 차이가있을 수 있음) 텍스트는 잘리지 않습니다. 당신이 main() 메소드가 내부의 코드가 프로그램을 배치합니다, 음, 다른 것을 눈치 챘을 수도, 또한

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); 
    } 
} 

:

enter image description here

위의 이미지를 생성하는 코드는 다음 중 하나입니다 Event Dispatch Thread (EDT)에 있습니다.

그래서, 당신의 미래 프로그램

+0

* "컴포넌트의 preferredSize 값은 0, 0이다."* 실제로는 원하는 크기를 가졌을 뿐이며 실제 크기가 없거나 기본 크기는 0x0이다. ') – MadProgrammer

+0

@MadProgrammer Woops! 의견을 보내 주셔서 감사합니다. 그것을 고쳐주었습니다 :) – Frakcool

+0

Cant는 충분한 사람들에게 감사하다고 말합니다, 미안 나는 당신들을 일찍 듣기에는 너무 완고했습니다. 임씨는 멋진 GUI와 ive를 만들려고 노력하면서 하루 종일 일하려고 애썼지 만 매우 화가났습니다. Frak 당신은 setLayout (null)이 매우 끔찍해서 수행하지 말아야한다는 것을 보여 주셨습니다. 귀하의 의견과 도움을 위해 충분히 감사드립니다! 당신 포스트들에게 매우 감사드립니다. – Kushroom

관련 문제