我是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,另一个是底部的JPanel。我想做的是通过稍微向上移动底部面板来消除Jtable末端可用的空白空间。我一直试着尝试无济于事。
答案 0 :(得分:1)
如果您希望稍微向上移动底部面板,可以简单地减少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
将通过根据所选组件的首选大小排列所有组件来调整窗口大小来完成任务。
答案 1 :(得分:0)
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"));
}
}
试试这段代码..