0

我有一点问题,我的程序的功能是显示我在 JList 区域上单击的选定项目,然后单击 OK 按钮后,将从 JtextArea 中出现一张收据,其中包含总额、税金和项目,我' 一直在尝试,但带有总计、税款和项目 (JTextArea) 的收据不会出来。

4

2 回答 2

4

JList 区域,点击 OK 按钮后,JtextArea 会出一张带有总额、税款和项目的收据,我一直在尝试,但没有总额、税款和项目的收据(JTextArea)出去。

  1. 此表格中的问题无法回答,请发布SSCCE

  2. 也许JTextArea不是适合显示的 JComponent a receipt will come out from the JtextArea with the total, tax and items,最好使用另一个JTable(或JList)进行显示total, tax and items

  3. 是否只有少数字段用于计算或显示total, tax and items使用JFormattedTextFiedlsNumber Formatter避免任何将字符串解析为数字或反之亦然

于 2012-09-26T09:31:43.847 回答
3

检查 JList 的以下示例代码:

 public class PhilosophersJList extends JFrame {

 private DefaultListModel philosophers;
 private JList list;

 public PhilosophersJList()
     {
     super( "Favorite Philosophers" );

     // create a DefaultListModel to store philosophers
     philosophers = new DefaultListModel();
     philosophers.addElement( "Socrates" );
     philosophers.addElement( "Plato" );
     philosophers.addElement( "Aristotle" );
     philosophers.addElement( "St. Thomas Aquinas" );
     philosophers.addElement( "Soren Kierkegaard" );
     philosophers.addElement( "Immanuel Kant" );
     philosophers.addElement( "Friedrich Nietzsche" );
     philosophers.addElement( "Hannah Arendt" );

     // create a JList for philosophers DefaultListModel
     list = new JList( philosophers );

     // allow user to select only one philosopher at a time
     list.setSelectionMode(
     ListSelectionModel.SINGLE_SELECTION );

     // create JButton for adding philosophers
     JButton addButton = new JButton( "Add Philosopher" );
     addButton.addActionListener(
         new ActionListener() {

         public void actionPerformed( ActionEvent event )
             {
             // prompt user for new philosopher's name
             String name = JOptionPane.showInputDialog(
             PhilosophersJList.this, "Enter Name" );

             // add new philosopher to model
             philosophers.addElement( name );
         }
     }
     );

     // create JButton for removing selected philosopher
     JButton removeButton =
     new JButton( "Show Details" );

     removeButton.addActionListener(
         new ActionListener() {

         public void actionPerformed( ActionEvent event )
             {
              String details = JOptionPane.showInputDialog(PhilosophersJList.this, "Tax :", list.getSelectedValue());
        philosophers.addElement(details);

         }
     }
     );

     // lay out GUI components
     JPanel inputPanel = new JPanel();
     inputPanel.add( addButton );
     inputPanel.add( removeButton );

     Container container = getContentPane();
     container.add( list, BorderLayout.CENTER );
     container.add( inputPanel, BorderLayout.NORTH );

     setDefaultCloseOperation( EXIT_ON_CLOSE );
     setSize( 400, 300 );
     setVisible( true );

 } // end PhilosophersJList constructor

 // execute application
 public static void main( String args[] )
     {
     new PhilosophersJList();
 }
}
于 2012-09-26T09:55:20.860 回答