很长一段时间以来,我一直在试图弄清楚如何正确布局我的程序。我基本上有 5 个 JLabel、5 个 JTextField 和 6 个 JButton,我需要将其对齐,如下所示。
我尝试使用 GridLayout,但它使 JTextFields 非常厚,我尝试了不同的流布局组合,结果不一。我想知道是否有人可以指导我如何获得以下结果?
提前致谢。
这是我现在的代码:
public class MyClass extends JFrame{
private JTextField item1;
private JTextField item2;
private JTextField item3;
private JTextField item4;
private JTextField item5;
JLabel label1 = new JLabel("Enter number of items in this order:");
JLabel label2 = new JLabel("Enter CD ID for Item #1:");
JLabel label3 = new JLabel("Enter quantity for Item #1:");
JLabel label4 = new JLabel("Item #1 info:");
JLabel label5 = new JLabel("Order subtotal for 0 item(s):");
private JButton button1 = new JButton("Process Item #1");
private JButton button2 = new JButton("Confirm Item #1");
private JButton button3 = new JButton("View Order");
private JButton button4 = new JButton("Finish Order");
private JButton button5 = new JButton("New Order");
private JButton button6 = new JButton("Exit");
private Scanner x;
private int exitFlag = 0;
public String[] idArray = new String[10];
public String[] recordArray = new String[10];
public String[] priceArray = new String[10];
public int myNum=1;
private JPanel jp = new JPanel();
public void openFile(){
try{
x = new Scanner(new File("inventory.txt"));
x.useDelimiter(",|" + System.getProperty("line.separator"));
}
catch(Exception e){
System.out.println("Could not find file");
}
}
public void readFile(){
int i=0;
while(x.hasNext()){
idArray[i] = x.next();
recordArray[i] = x.next();
priceArray[i] = x.next();
i++;
}
}
public int itemNum(int num){
return num+1;
}
public MyClass(){
super("Matt's World of Music");
jp.setLayout(new FlowLayout(FlowLayout.RIGHT));
Box vertBox = Box.createVerticalBox();
Box vertBox2 = Box.createVerticalBox();
Box itemBox2 = Box.createHorizontalBox();
item1 = new JTextField(40);
item2 = new JTextField(40);
item3 = new JTextField(40);
item4 = new JTextField(40);
item5 = new JTextField(40);
vertBox.add(label1);
vertBox.add(label2);
vertBox.add(label3);
vertBox.add(label4);
vertBox.add(label5);
jp.add(vertBox);
vertBox2.add(item1);
vertBox2.add(item2);
vertBox2.add(item3);
vertBox2.add(item4);
vertBox2.add(item5);
jp.add(vertBox2);
itemBox2.add(button1);
itemBox2.add(button2);
itemBox2.add(button3);
itemBox2.add(button4);
itemBox2.add(button5);
itemBox2.add(button6);
jp.add(itemBox2);
add(jp);
button2.setEnabled(false);
button3.setEnabled(false);
button4.setEnabled(false);
item4.setEditable(false);
item5.setEditable(false);
//Process Item
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
MyClass obj = new MyClass();
button1.setEnabled(false);
button2.setEnabled(true);
item1.setEditable(false);
obj.openFile();
obj.readFile();
//start loop
for(int i=0; i < idArray.length; i++){
if(item2.getText().equals(obj.idArray[i])==true){
//set item4 text field to price id and other details
item4.setText(obj.idArray[i] + " " + obj.recordArray[i] + " $" + obj.priceArray[i].replaceAll("\\s",""));
}
}
}
});
//Confirm Item
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(myNum==Integer.parseInt(item1.getText())){
JOptionPane.showMessageDialog(null, "Item #" + (myNum) + " accepted");
button2.setEnabled(false);
button1.setText("Process Item");
button2.setText("Confirm Item");
button1.setEnabled(false);
button3.setEnabled(true);
button4.setEnabled(true);
item2.setText("");
item3.setText("");
label2.setText("");
label3.setText("");
item2.setEditable(false);
item3.setEditable(false);
}else{
//Execute when button is pressed
button1.setEnabled(true);
button2.setEnabled(false);
JOptionPane.showMessageDialog(null, "Item #" + (myNum) + " accepted");
item2.setText("");
item3.setText("");
label2.setText("Enter CD ID for Item #" + (myNum+1) + ":");
label3.setText("Enter quantity for Item #" + (myNum+1) + ":");
label4.setText("Item #" + (myNum+1) + " info:");
myNum++;
button1.setText("Process item #" + (myNum));
button2.setText("Confirm item #" + (myNum));
}
}
});
//View Order
button3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//Execute when button is pressed
System.out.println("View Order");
}
});
//Finish Order
button4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//Execute when button is pressed
System.out.println("Finish Order");
}
});
//New Order
button5.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//Execute when button is pressed
System.out.println("New Order");
}
});
//Quit
button6.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//exit program
}
});
}
}