2013-10-12 46 views
0

我已經構建了一個程序(運行良好),但我真的想用JList而不是單選按鈕。問題是,我有一個可怕的時間試圖做到這一點,我最終得到了一個錯誤和一個功能紊亂的程序。如果任何人都可以提供任何有關如何在Java中正確使用列表的示例,那麼將不勝感激!我沒有發佈我的程序,因爲我不在尋找答案,只是關於JList的一般性建議。感謝那些建議教程鏈接的人 - 它幫助了! :)無法理解JList

回答

2

如果有人可以提供如何名單中的Java

正確使用閱讀JList的API,並按照標題爲How to Use Lists到包含工作實例的Swing指南鏈接任何例子。

其他評論:

  1. 不要使用的setBounds()大小/位置組件。 Swing被設計爲與佈局經理一起使用,出於太多理由在此列出。 Swing教程還有一個關於佈局管理器的部分。

  2. 請勿使用KeyListner。在使用AWT時這是一種老方法。 Swing有更好的API。在這種情況下,您可以將DocumentListener添加到文本字段的Document中。本教程再次介紹瞭如何編寫DocumentListener。

保持教程鏈接方便,它將有助於解決許多問題。

0

我與你的代碼做了一些有看

import java.awt.*; 
import java.awt.event.*; 
import java.text.*; 
import javax.swing.*; 
import javax.swing.event.ListSelectionEvent; 
import javax.swing.event.ListSelectionListener; 

public class PizzaOrder extends JFrame implements ActionListener, KeyListener, ListSelectionListener { 


double smallPizzaPrice = 7.00, mediumPizzaPrice = 9.00, 
     largePizzaPrice = 11.00; 

double sausage = 1.00, ham = 1.00, pineapple = 1.00, mushroom = 1.00, 
     pepperoni = 1.00; 

JLabel lab1, lab2, lab3, toppers, lab4, lab5; 
Button button; 
JTextField text1, text2; 
ButtonGroup group; 
JRadioButton small, medium, large; 
JCheckBox chk1, chk2, chk3, chk4, chk5; 
JScrollPane jScrollPane1 = new javax.swing.JScrollPane(); 
JList jList1 = new javax.swing.JList(); 
DefaultListModel modellist=new DefaultListModel(); 

PizzaOrder() { 
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
    getContentPane().setLayout(null); 
    lab1 = new JLabel("Name: "); 
    lab2 = new JLabel("Quantity: "); 
    lab3 = new JLabel("How hungry are you?"); 
    lab3.setForeground(Color.BLUE); 
    lab3.setFont(new Font("Arial", Font.BOLD, 14)); 

    toppers = new JLabel("Add some toppings : "); 
    toppers.setForeground(new Color(0, 0, 205)); 
    toppers.setFont(new Font("Arial", Font.ITALIC, 14)); 

    lab4 = new JLabel("Total: "); 
    lab4.setForeground(Color.RED); 
    lab4.setFont(new Font("Arial", Font.BOLD, 14)); 

    lab5 = new JLabel("$0.00"); 
    lab5.setForeground(Color.RED); 

    text1 = new JTextField(20); 

    text2 = new JTextField(20); 
    text2.setText(""); 

    small = new JRadioButton("Small", true); 
    medium = new JRadioButton("Medium", false); 
    large = new JRadioButton("Large", false); 
    group = new ButtonGroup(); 
    group.add(small); 
    group.add(medium); 
    group.add(large); 

    chk1 = new JCheckBox("Mushroom", false); 
    chk2 = new JCheckBox("Ham", false); 
    chk3 = new JCheckBox("Pepperoni", false); 
    chk4 = new JCheckBox("Sausage", false); 
    chk5 = new JCheckBox("Pineapple", false); 

    modellist.addElement("Small"); 
    modellist.addElement("Medium"); 
    modellist.addElement("Large"); 
    jList1.setModel(modellist); 

    button = new Button("Order Now"); 
    small.addActionListener(this); 
    medium.addActionListener(this); 
    large.addActionListener(this); 



    chk1.addActionListener(this); 
    chk2.addActionListener(this); 
    chk3.addActionListener(this); 
    chk4.addActionListener(this); 
    chk5.addActionListener(this); 

    text2.addKeyListener(this); 
    button.addActionListener(this); 

    jList1.addListSelectionListener(this); 

    lab1.setBounds(50, 50, 200, 20); 
    lab2.setBounds(50, 90, 200, 20); 

    text1.setBounds(200, 50, 200, 20); 
    text2.setBounds(200, 90, 200, 20); 

    lab3.setBounds(50, 170, 500, 20); 
    small.setBounds(300, 170, 100, 20); 
    medium.setBounds(400, 170, 100, 20); 
    large.setBounds(500, 170, 100, 20); 

    toppers.setBounds(50, 200, 300, 20); 
    chk1.setBounds(50, 230, 200, 20); 
    chk2.setBounds(50, 260, 200, 20); 
    chk3.setBounds(50, 290, 200, 20); 
    chk4.setBounds(50, 320, 200, 20); 
    chk5.setBounds(50, 350, 200, 20); 
    lab4.setBounds(50, 550, 400, 40); 
    lab5.setBounds(200, 550, 500, 40); 

    jScrollPane1.setBounds(300, 270, 200, 300); 

    jScrollPane1.setViewportView(jList1); 

    button.setBounds(50, 600, 100, 20); 
    add(lab1); 
    add(lab2); 
    add(text1); 
    add(text2); 
    add(lab3); 
    add(small); 
    add(medium); 
    add(large); 
    add(toppers); 
    add(chk1); 
    add(chk2); 
    add(chk3); 
    add(chk4); 
    add(chk5); 
    add(lab4); 
    add(lab5); 
    add(button); 
    add(jScrollPane1); 
    text2.selectAll(); 
    setVisible(true); 
    setSize(800, 700); 

} 

public void keyTyped(KeyEvent e) { 
} 

public void keyPressed(KeyEvent e) { 
} 

public void keyReleased(KeyEvent e) { 

    try { 
     Integer.parseInt(text2.getText()); 
    } catch (NumberFormatException fe) { 
     text2.setText(""); 
    } 

    refreshPrice(); 
} 

public void actionPerformed(ActionEvent e) { 
    if (e.getSource() == button) { 
     JOptionPane.showMessageDialog(this, "Hi " + text1.getText() + ", thanks for ordering with  us!" 
       + "\n\nYour pizza's in the oven. ", 
       "Orders Confirmed", JOptionPane.INFORMATION_MESSAGE); 
    } 
    refreshPrice(); 
} 

private void refreshPrice() { 
    double price = 0; 
    int numOfPizzas = Integer.parseInt(text2.getText()); 

    NumberFormat numberForm = NumberFormat.getNumberInstance(); 
    DecimalFormat moneyForm = (DecimalFormat) numberForm; 
    moneyForm.applyPattern("0.00"); 

    if (small.isSelected()) { 
     price += smallPizzaPrice * numOfPizzas; 
    } 
    if (medium.isSelected()) { 
     price += mediumPizzaPrice * numOfPizzas; 
    } 
    if (large.isSelected()) { 
     price += largePizzaPrice * numOfPizzas; 
    } 
    if (chk1.isSelected()) { 
     price += mushroom * numOfPizzas; 
    } 
    if (chk2.isSelected()) { 
     price += sausage * numOfPizzas; 
    } 
    if (chk3.isSelected()) { 
     price += pineapple * numOfPizzas; 
    } 
    if (chk4.isSelected()) { 
     price += pepperoni * numOfPizzas; 
    } 
    if (chk5.isSelected()) { 
     price += ham * numOfPizzas; 
    } 
    lab5.setText("$" + moneyForm.format(price)); 
} 

public static void main(String[] args) { 
    @SuppressWarnings("unused") 
    PizzaOrder order = new PizzaOrder(); 
} 
    @Override 
    public void valueChanged(ListSelectionEvent e) { 
      double price = 0; 
      int numOfPizzas = Integer.parseInt(text2.getText()); 

      NumberFormat numberForm = NumberFormat.getNumberInstance(); 
      DecimalFormat moneyForm = (DecimalFormat) numberForm; 
      moneyForm.applyPattern("0.00"); 

      System.out.println(jList1.getSelectedIndex()); 

      if(jList1.getSelectedIndex()==0) { 
      price += smallPizzaPrice * numOfPizzas; 
      } 
      if(jList1.getSelectedIndex()==1) { 
      price += mediumPizzaPrice * numOfPizzas; 
      } 
      if(jList1.getSelectedIndex()==2) { 
      price += largePizzaPrice * numOfPizzas; 
      } 
      lab5.setText("$" + moneyForm.format(price)); 
    } 
} 
+0

由於代碼有多大,它如果你點出了什麼變化,而不是讓其他人狩獵和猜是最好的。 –

+0

或者更好的讓OP閱讀教程並理解工作示例,然後使用從教程中獲得的知識來更改程序。給一個人一條魚,他吃了一天。教一個人去釣魚,並且爲了生命而吃飯。當教程中存在簡單的eaxamples時,勺子餵食代碼不會幫助OP學習,特別是當這是一個家庭作業問題時,我們不應該爲他們編寫代碼,只能將它們指向正確的方向。 – camickr

+0

謝謝你的智慧 – user2868431