我对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末尾的可用空间。我不断地尝试,但都无济于事。
如果您想稍微向上移动底部面板,您可以简单地减小GridLayout
中testPanel
的垂直间隙。更换
private JPanel testPanel = new JPanel(new GridLayout(2 , 4 , 4 , 20));
带有
private JPanel testPanel = new JPanel(new GridLayout(2, 4, 4, 5));
此外,最好覆盖组件的getPreferredSize()
,而不是调用setPreferredSize
。这样可以确保首选大小不能由其他组件设置。
调用JFrame.pack
将通过根据其首选大小排列所有组件来调整窗口大小来完成任务。
import javax.swing.*;
import java.awt.*;
import javax.swing.table.DefaultTableModel;
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 = 50;
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");
itemPanel.add(infoPanel , "East");
//
mainPanel.add(memberPanel , "North");
mainPanel.add(scroll , "Center");
// mainPanel.add(infoPanel , "East");
mainPanel.add(itemPanel , "South");
tabp.add("Sales" , mainPanel);
f.add(tabp);
f.setVisible(true);
}
public static void main(String[] args){
SalesScreen s=new SalesScreen(new JTabbedPane(),new JFrame("test"));
}
}
请尝试此代码。。