簡體   English   中英

我的JTable沒有顯示

[英]My JTable doesn't show up

我是JTable或GUI的新手,但已獲得分配在GUI內構建收據程序的任務。 我設法使基本功能正常工作,但是我的桌子看上去糟透了。 我需要有關如何正確顯示表格的幫助

public static void main (String[] args)
{       
    ArrayList <item> lol= new ArrayList <item>();
    item ayam = new item("ayam",5678);
    item kambing= new item("kambing",5014);
    item buaya= new item("buaya",3000);
    item bocoranquiz= new item("bocoranquiz",5000);
    lol.add(ayam);
    lol.add(kambing);
    lol.add(buaya);
    lol.add(bocoranquiz);
    JFrame frame= new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new GridLayout(3, 0));
    frame.setSize(1000, 1000);
    SimpleDateFormat sdf = new SimpleDateFormat("dd/M/yyyy");
    String date = sdf.format(new Date()); 

    JComboBox combo1= new JComboBox();
    combo1.setPreferredSize(new Dimension(10, 10));
    combo1.addItem(ayam.getname());
    combo1.addItem(kambing.getname());
    combo1.addItem(buaya.getname());
    combo1.addItem(bocoranquiz.getname());
    JLabel label1= new JLabel("Invoice no: ");
    JLabel label2= new JLabel("Invoice Date : " + date);
    JLabel label3= new JLabel("Item name " );
    JLabel label4= new JLabel("Item Price ");
    JLabel label5= new JLabel("Item Quantity : ");
    JPanel panel1= new JPanel ();
    panel1.setLayout(new GridLayout(3,0));
    panel1.add(label1);
    panel1.add(label2);

    class inputListener implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {   
            JComboBox<String> combo= (JComboBox<String>) event.getSource();
            selected= (String) combo.getSelectedItem();             
            alpha=10;        
            if (selected.equals("ayam"))
            {           
                alpha=5678; 
            }       
            else if (selected.equals("kambing"))
            {           
                alpha=5014; 
            }       
            else if (selected.equals("buaya"))
            {           
                alpha=3000; 
            }       
            else if (selected.equals("bocoranquiz"))
            {           
                alpha=5000; 
            }   
            label3.setText("Item name " + selected);
            label4.setText("Item quantity " + alpha);
        }           
    }
    ActionListener inputAct = new inputListener();
    combo1.addActionListener(inputAct);
    panel1.add(combo1);     
    JTextField tf = new JTextField();
    JButton adda = new JButton("Add");
    String[] columnNames= {"Name","Price","Quantity","Total"};
    DefaultTableModel tablemodel= new DefaultTableModel(columnNames,0);
    JTable table = new JTable(tablemodel);      
    JScrollPane scrollPanel = new JScrollPane(table);
    class inputListener2 implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            quantity= Integer.parseInt(tf.getText());
            double total= alpha * quantity;
            Object[] data= {selected,alpha,quantity,total};
            tablemodel.addRow(data);
        }
    ActionListener inputAct2 = new inputListener2();
    adda.addActionListener(inputAct2);
    JPanel panel2= new JPanel();
    panel2.setLayout(new GridLayout(10,0));
    panel2.add(label3);
    panel2.add(label4);
    panel2.add(label5);
    panel2.add(tf);
    panel2.add(adda);
    panel2.add(scrollPanel);
    frame.add(panel1);
    frame.add(panel2);
    frame.setVisible(true);
}

您的代碼有些混亂,但是基本問題是您將GridLayout設置為多行,例如

frame.setLayout(new GridLayout(3, 0));

應該

frame.setLayout(new GridLayout(2, 0));

因為您只向框架添加了2個組件

相等,

panel2.setLayout(new GridLayout(10, 0));

應該是

panel2.setLayout(new GridLayout(6, 0));

請記住,即使行/列中沒有任何內容, GridLayout也會將容器分為相等的部分。

您可能還需要根據基本需求考慮使用其他布局管理器(例如GridBagLayout )或布局組合。

有關更多詳細信息,請參見如何使用GridBagLayout

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM