如何使用 JScrollPane



我正在尝试使用Java的swing工具包创建一个简单的预算应用程序。应用程序应要求用户在两个 JTextFields 中输入预算项目和该项目的价格。它将信息存储在两个 ArrayList 中,稍后输出到文本文档中。每次用户输入项目和价格(通过单击"Ok"JButton)时,都会创建两个新的JTextFields和一个新的"Ok"JButton来存储另一个项目及其价格。

我的问题是我希望用户能够输入他/她需要的尽可能多的预算项目。目前,一旦用户添加了许多项目,JTextFields 就会消失。我相信解决方案是添加某种滚动机制,但我在添加 JScrollPane 时遇到问题。有这种经验的人有解决这个问题的方法吗?

我将非常感谢任何帮助。

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;
public class Budget3 {
    private JFrame frame;
    private ArrayList<String> itemList = new ArrayList<String>();
    private ArrayList<Double> priceList = new ArrayList<Double>();
    private double totalPrice = 0;
    private int okCount = 0;
    private DecimalFormat f = new DecimalFormat("$##,##0.00");
    private JLabel itemTitleLabel;
    private JLabel priceTotal;
    public static void main(String[] args){
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Budget3 window = new Budget3();
                    window.frame.setVisible(true);  
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    public Budget3() {
        initialize();
    }
    public void initialize(){
        frame = new JFrame();
        frame.setBounds(100,100,800,800);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        JLabel titleLabel = new JLabel("BUDGET");
        titleLabel.setBounds(330,16,125,48);
        frame.getContentPane().add(titleLabel);
        titleLabel.setFont(new Font("Tahoma", Font.BOLD, 30));
        itemTitleLabel = new JLabel("Item (" + itemList.size() + ")");
        itemTitleLabel.setFont(new Font("Tahoma", Font.PLAIN, 25));
        itemTitleLabel.setBounds(49, 117, 151, 37);
        frame.getContentPane().add(itemTitleLabel);
        JLabel priceTitleLabel = new JLabel("Price");
        priceTitleLabel.setFont(new Font("Tahoma", Font.PLAIN, 25));
        priceTitleLabel.setBounds(225, 117, 69, 37);
        frame.getContentPane().add(priceTitleLabel);
        priceTotal = new JLabel("Total:     " + f.format(totalPrice));
        priceTotal.setFont(new Font("Tahoma", Font.PLAIN, 25));
        priceTotal.setBounds(450, 117, 274, 37);
        frame.getContentPane().add(priceTotal);
        addItem();
    }
    int textFieldCount = 0;
    public void addItem(){
        int labelY = 180 + okCount*30;  
        JTextField itemField = new JTextField();
        frame.getContentPane().add(itemField);
        itemField.setBounds(50, labelY, 150, 30);
        itemField.setColumns(10);
        JTextField priceField = new JTextField();
        textFieldCount++;
        frame.getContentPane().add(priceField);
        priceField.setBounds(225, labelY, 150, 30);
        priceField.setColumns(10);
        JButton okButton = new JButton("OK");
        okButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                int buttonY = okButton.getY();
                int spotCount = (buttonY - 180) / 30;
                try{
                    itemList.remove(spotCount);
                    priceList.remove(spotCount);
                } catch (IndexOutOfBoundsException bounds){}
                String item = itemField.getText();
                String priceString = priceField.getText();
                double price = Double.parseDouble(priceString);         
                itemList.add(spotCount, item);
                priceList.add(spotCount, price);
                totalPrice = 0;
                for(int i = 0; i < priceList.size(); i++){
                    totalPrice = totalPrice + priceList.get(i);
                }
                if(itemList.size() < 2){
                    itemTitleLabel.setText("Item (" + itemList.size() + ")");
                }
                else{
                    itemTitleLabel.setText("Items (" + itemList.size() + ")");
                }
                priceTotal.setText("Total:     " + f.format(totalPrice));
                if(itemList.size() == textFieldCount)
                {
                    okCount++;
                    addItem();
                }
            }
        });
        frame.getContentPane().add(okButton);
        okButton.setBounds(450,labelY,62,30);
        frame.revalidate();
        frame.repaint();
}

}

// Create a JScrollBar with all default properties. Its orientation
// will be vertical, current value 0, extent 10, minimum 0, and maximum
// 100
   JScrollBar sb1 = new JScrollBar();
// Create a horizontal JScrollBar with default values
   JScrollBar sb2 = new JScrollBar(JScrollBar.HORIZONTAL);
// Create a horizontal JScrollBar with a current value of 50,
// extent 15, minimum 1 and maximum 150
   JScrollBar sb3 = new JScrollBar(JScrollBar.HORIZONTAL, 50, 15, 1,150);
// Create a JScrollPane with no component as its viewport and
// with default scrollbars policy as "As Needed"
   JScrollPane sp1 = new JScrollPane();
// Create a JScrollPane with a JTextArea as its viewport and
// with default scrollbars policy as "As Needed"
   JTextArea description = new JTextArea(10, 60);
   JScrollPane sp2 = new JScrollPane(description);
// Create a JScrollPane with a JTextArea as its viewport and
// both scrollbars policy set to "show always"
   JTextArea comments = new JTextArea(10, 60);
   JScrollPane sp3 = new JScrollPane(comments,
   JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
   JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

相关内容

  • 没有找到相关文章

最新更新