0

私は 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 があり、下部にも 1 つあります。私がしたいことは、下部パネルを少し上に移動して、Jtable の最後にある空きスペースをなくすことです。私は試してみて、役に立たないようにしています。

4

2 に答える 2

1

下部パネルを少し上に移動したい場合は、GridLayoutforの垂直ギャップを単純に減らすことができます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は、すべてのコンポーネントを好みのサイズに合わせてウィンドウのサイズを変更することでタスクを完了します。

于 2013-01-01T14:15:30.130 に答える
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"));
}
}

このコードを試してください..

于 2013-01-01T17:04:18.243 に答える