我是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);
}
答案 0 :(得分:3)
您的代码有点混乱,但是,基本问题是您将GridLayout
设置为多行,例如
frame.setLayout(new GridLayout(3, 0));
应该是
frame.setLayout(new GridLayout(2, 0));
因为你只是在框架中添加了两个组件
同样地,
panel2.setLayout(new GridLayout(10, 0));
应该是
panel2.setLayout(new GridLayout(6, 0));
请记住,即使行/列中没有任何内容,GridLayout
也会将容器划分为相等的部分。
您可能还需要考虑使用其他布局管理器,例如GridBagLayout
或布局组合,具体取决于您的基本需求。
有关详细信息,请参阅How to use GridBagLayout。