我对 Java GUI 很陌生,在对齐我创建的这个程序的 GUI 时遇到了麻烦。代码如下:
编码 :
import javax.swing.*;
import java.awt.*;
import javax.swing.table.DefaultTableModel;
public class SalesScreen{
private JPanel mainPanel = new JPanel();
//Top Member Panel
private JPanel memberPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
private JLabel memberID = new JLabel();
private JTextField memberIDField = new JTextField(10);
private JLabel memberName = new JLabel();
private JTextField memberNameField = new JTextField(30);
private JButton memberButton = new JButton("OK");
//Center Table Panel
private JPanel tablePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
private JTable itemList;
String [] columnNames = {"Barcode" , "Name" , "Price (RM)" , "Quantity"};
int numRows = 30;
DefaultTableModel model = new DefaultTableModel(numRows , columnNames.length);
JScrollPane scroll;
//Bottom Right
private JPanel infoPanel = new JPanel(new BorderLayout());
private JPanel infoGridPanel = new JPanel( new GridLayout(4,2));
private JPanel infoFlowPanel = new JPanel(new FlowLayout());
private JLabel total = new JLabel(" Total (RM) :");
private JLabel discount = new JLabel(" Discount Rate (%) : ");
private JLabel overall = new JLabel(" Overall (RM) :");
private JLabel payment = new JLabel(" Payment (RM) :");
private JButton process = new JButton("PROCESS");
private JTextField totalPrice = new JTextField(5);
private JTextField discountRate = new JTextField(5);
private JTextField overallPrice = new JTextField(5);
private JTextField paymentMade = new JTextField(5);
//Bottom
private JPanel itemPanel = new JPanel(new BorderLayout());
private JPanel testPanel = new JPanel(new GridLayout(2 , 4 , 4 , 20));
private JLabel emptyLabel = new JLabel("");
private JLabel emptyLabel2 = new JLabel("");
private JLabel barcode = new JLabel("Enter Barcode :");
private JTextField barcodeScanner = new JTextField(5);
private JLabel quantity = new JLabel("Quantity :");
private JTextField itemQuantity = new JTextField(5);
private JButton addItem = new JButton("Add");
private JButton deleteItem = new JButton("Delete");
// //
// //
// Beginning of Constructor and Method //
// //
// //
SalesScreen(JTabbedPane tabp , JFrame f) //tabp is Jtabbedpanel , f is the Jframe
{
mainPanel.setLayout(new BorderLayout());
memberID.setText(" Member ID :");
memberIDField.setText("");
memberName.setText("Member Name :");
memberNameField.setText("");
memberNameField.setEditable(false);
memberPanel.add(memberID);
memberPanel.add(memberIDField);
memberPanel.add(memberName);
memberPanel.add(memberNameField);
memberPanel.add(memberButton);
//
model.setColumnIdentifiers(columnNames);
itemList = new JTable(model);
scroll = new JScrollPane(itemList);
//tablePanel.add(scroll);
//
total.setLabelFor(totalPrice);
totalPrice.setEditable(false);
discountRate.setEditable(false);
overallPrice.setEditable(false);
process.setPreferredSize(new Dimension(100, 100));
discount.setLabelFor(discountRate);
overall.setLabelFor(overallPrice);
payment.setLabelFor(paymentMade);
infoGridPanel.add(total);
infoGridPanel.add(totalPrice);
infoGridPanel.add(discount);
infoGridPanel.add(discountRate);
infoGridPanel.add(overall);
infoGridPanel.add(overallPrice);
infoGridPanel.add(payment);
infoGridPanel.add(paymentMade);
infoFlowPanel.add(infoGridPanel);
infoFlowPanel.add(process);
infoPanel.add(infoFlowPanel , "South");
//
testPanel.setMaximumSize( new Dimension( 100, 100) );
testPanel.add(barcode);
testPanel.add(barcodeScanner);
testPanel.add(addItem);
testPanel.add(deleteItem);
testPanel.add(quantity);
testPanel.add(itemQuantity);
testPanel.add(emptyLabel);
testPanel.add(emptyLabel2);
itemPanel.add(testPanel , "West");
//
mainPanel.add(memberPanel , "North");
mainPanel.add(scroll , "Center");
mainPanel.add(infoPanel , "East");
mainPanel.add(itemPanel , "South");
tabp.add("Sales" , mainPanel);
f.add(tabp);
}
}
问题:
1.)如您所见,中间有一个Jtable,右下面板有一个JPanel,底部也有一个。我想要做的是通过将底部面板稍微移到上方来消除 Jtable 末尾的可用空间。我不断尝试,但无济于事。