我有点问题,我的程序的功能是显示所选项目 我点击了JList区域,点击OK按钮后,收据将从 带有总额,税金和物品的JtextArea,我一直在尝试,但收据总额,税金和物品(JTextArea)不会 出来。
答案 0 :(得分:4)
JList区域,点击OK按钮后,收据将从JtextArea出来,包含总额,税金和物品,我一直在试用,但收据总额,税金和物品(JTextArea)赢了不出来。
此表单中的问题无法回复,发布SSCCE
可能JTextArea不适合展示a receipt will come out from the JtextArea with the total, tax and items
的JComponent,更好的方法是使用其他JTable(或JList)进行展示{{1} }
是否只有少数字段用于计算或禁用total, tax and items
以使用JFormattedTextFiedls和total, tax and items
来避免任何解析字符串编号,反之亦然
答案 1 :(得分: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();
}
}