创建JFrame时添加Shape。在哪里放置构造函数?

时间:2017-12-10 20:08:13

标签: java swing user-interface jcomponent

我知道下面的代码不能用于我正在尝试的内容,但我还没有找到解决方法。我试图在程序运行时在框架底部添加一个空白矩形。当我运行以下代码时,框架使用我的标签,按钮和TextFieldArea正确填充,但它没有创建我想要添加到底部的空白矩形(最终目标是在程序工作时填充矩形完成又名进度条)。

我的想法是你可以在我的TicketCounterFrame类中创建一个能够扩展JComponent的子类,这样我就可以创建我的矩形了。我有一个名为createComponents的伴随方法,我在其中创建其余的组件,然后在构造函数中创建一个面板,将所有组件添加到面板,然后将面板添加到框架中。我没有使用创建形状并将它们添加到框架中所以我很困惑,如果你需要创建一个类型为ProgressBar的私有实例变量progBar,以及你如何创建该组件并将其添加到框架中使用createComponents方法创建的其余组件。

我很肯定我没有正确地考虑这个问题,但却找不到有用的例子。有人可以告诉我如何/为什么我这样做不正确?任何帮助将不胜感激。

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class TicketCounterFrame extends JFrame{
    TicketCounter tickets;

    private JLabel headerLbl;
    private JLabel promptLbl;
    private JLabel soldLbl;
    private JTextField ticketAmount;
    private JButton purchaseBtn;
    private ProgressBar progBar;

public TicketCounterFrame(TicketCounter tkts){
    this.tickets = tkts;
    createComponents();

    submitClickListener s = new submitClickListener();
    purchaseBtn.addActionListener(s);

    JPanel panel = new JPanel();
    panel.add(headerLbl);
    panel.add(ticketAmount);
    panel.add(purchaseBtn);
    panel.add(promptLbl);
    panel.add(soldLbl);
    panel.add(progBar);
    add(panel);
    setSize(600,400);
}

class submitClickListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        String t = ticketAmount.getText();
        try 
        {
            int ticketAmt = Integer.parseInt(t);
            if(ticketAmt <= 6 && ticketAmt > 0)
            {
                if (ticketAmt < tickets.getRemaining())
                {
                    tickets.purchaseTickets(ticketAmt);
                    ticketAmount.setText("");
                    promptLbl.setText(ticketAmt + " ticket(s) purchased. There are " + tickets.getRemaining() + " tickets remaining for sale");
                }
                else if(ticketAmt > tickets.getRemaining())
                {
                    ticketAmount.setText("");
                    promptLbl.setText("Invalid entry. There are only " + tickets.getRemaining() + " tickets left. Try again.");
                }
                else if (ticketAmt == tickets.getRemaining())
                {
                    tickets.purchaseTickets(ticketAmt);
                    ticketAmount.setText("");
                    promptLbl.setText("There are " + tickets.getRemaining() + " tickets left.");
                    soldLbl.setText("SOLD OUT!");
                }
            }
            else
            {
                ticketAmount.setText("");
                promptLbl.setText("Invalid entry. Try again. There are " + tickets.getRemaining() + " tickets remaining for sale");
            }
        }
        catch (NumberFormatException err){
            ticketAmount.setText("");
            promptLbl.setText("Invalid entry. Try again. There are " + tickets.getRemaining() + " tickets remaining");
        }
    }
}

private void createComponents(){
    headerLbl = new JLabel("Enter how many tickets for purchase (1 - 6)");
    ticketAmount = new JTextField(10);
    promptLbl = new JLabel("There are " + tickets.getRemaining() + " tickets for sale");
    soldLbl = new JLabel();
    purchaseBtn = new JButton("Buy tickets");
    progBar = new ProgressBar();
}

public class ProgressBar extends JComponent{
    @Override
    public void paintComponent(Graphics g){
        g.drawRect(100,  200,  300,  50);
        }
    }
}

0 个答案:

没有答案