JTextArea麻烦

时间:2016-01-29 21:56:37

标签: java swing jframe awt jtextarea

我正在制作扑克游戏而且我遇到了一个问题,几乎所有事情都取决于交易按钮的actionListener。它应该删除交易按钮并添加一个新的JTextArea(此文本只是一个占位符)。几乎所有事情都有效,直到那一点,但在提交按钮actionListener中,它不会删除inputText JTextArea(而是将setVisible设置为false)。如何添加另一个textArea并将其显示在屏幕上?

private ArrayList<String> deck;
private Button submit;
private Button deal;
private Font buttonFont;
private Font textFont;
private JFrame framePoker;
private JPanel masterPanel;
private JPanel cardPanel;
private JPanel textPanel;
private JPanel buttonPanel;
private Checkbox chip100;
private Checkbox chip500;
private Checkbox chip900;
private Checkbox op1;
private Checkbox op2;
private Checkbox op3;
private CheckboxGroup opponentInput;
private CheckboxGroup chipInput;
private Container cont;
private SpringLayout layout;
private JTextArea inputText;
private JTextArea test;

// initialize frame contents
private void initialize() throws IOException 
{
    // set defaults
    layout = new SpringLayout();
    masterPanel = new JPanel(layout);
    textPanel = new JPanel(layout);
    buttonPanel = new JPanel(layout);
    cardPanel = new JPanel(layout);
    opponentInput = new CheckboxGroup();
    chipInput = new CheckboxGroup();
    chip100 = new Checkbox("100", chipInput, false);
    chip500 = new Checkbox("500", chipInput, true);
    chip900 = new Checkbox("900", chipInput, false);
    op1 = new Checkbox("1", opponentInput, true);
    op2 = new Checkbox("2", opponentInput, false);
    op3 = new Checkbox("3", opponentInput, false);
    buttonFont = new Font("TimesRoman", Font.BOLD, 46);
    textFont = new Font("TimesRoman", Font.PLAIN, 32);
    submit = new Button("SUBMIT");
    inputText = new JTextArea("How many chips?       How many opponents?");
    deal = new Button("DEAL");
    deck = new ArrayList<>(52);

    fillDeck();

    // set up frame (window)
    framePoker = new JFrame();
    framePoker.setTitle("SWING Poker - Texas Hold 'Em");
    framePoker.setBounds(0, 0, 1200, 650);
    framePoker.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    framePoker.setBackground(Color.GREEN);


    try {
    framePoker.setContentPane(new JLabel(
            new ImageIcon(ImageIO.read(new File(
            "C:/Users/Austin/Documents/custom made codes/random programs/Poker/src/background.png")))));
    } catch (IOException e) {
        e.printStackTrace();
    }

    framePoker.setResizable(false);
    framePoker.pack();
    framePoker.setLayout(layout);

    cont = framePoker.getContentPane();

    // setup and add master panel
    masterPanel.setPreferredSize(framePoker.getPreferredSize());
    masterPanel.setOpaque(false);
    cardPanel.setPreferredSize(framePoker.getPreferredSize());
    cardPanel.setOpaque(false);
    textPanel.setPreferredSize(framePoker.getPreferredSize());
    textPanel.setOpaque(false);
    buttonPanel.setPreferredSize(framePoker.getPreferredSize());
    buttonPanel.setOpaque(false);

    framePoker.add(masterPanel);

    // set font for buttons
    chip100.setFont(buttonFont);
    chip500.setFont(buttonFont);
    chip900.setFont(buttonFont);
    op1.setFont(buttonFont);
    op2.setFont(buttonFont);
    op3.setFont(buttonFont);
    submit.setFont(buttonFont);
    deal.setFont(buttonFont);
    inputText.setFont(textFont);
    inputText.setOpaque(false);

    // display beginning buttons
    buttonPanel.add(submit);

    buttonPanel.add(chip100);
    buttonPanel.add(chip500);
    buttonPanel.add(chip900);

    buttonPanel.add(op1);
    buttonPanel.add(op2);
    buttonPanel.add(op3);

    masterPanel.add(buttonPanel);

    textPanel.add(inputText);
    masterPanel.add(textPanel);

    layout.putConstraint(SpringLayout.WEST, submit, (framePoker.getWidth() / 2) - 100, SpringLayout.WEST, cont);
    layout.putConstraint(SpringLayout.NORTH, submit, 500, SpringLayout.NORTH, cont);

    layout.putConstraint(SpringLayout.WEST, chip100, framePoker.getWidth() / 3, SpringLayout.WEST, cont);
    layout.putConstraint(SpringLayout.NORTH, chip100, (framePoker.getHeight() / 2) - 150, SpringLayout.NORTH, cont);
    layout.putConstraint(SpringLayout.WEST, chip500, framePoker.getWidth() / 3, SpringLayout.WEST, cont);
    layout.putConstraint(SpringLayout.NORTH, chip500, (framePoker.getHeight() / 2) - 50, SpringLayout.NORTH, cont);
    layout.putConstraint(SpringLayout.WEST, chip900, framePoker.getWidth() / 3, SpringLayout.WEST, cont);
    layout.putConstraint(SpringLayout.NORTH, chip900, (framePoker.getHeight() / 2) + 50, SpringLayout.NORTH, cont);

    layout.putConstraint(SpringLayout.WEST, op1, ((framePoker.getWidth() / 3) * 2) - 75, SpringLayout.WEST, cont);
    layout.putConstraint(SpringLayout.NORTH, op1, (framePoker.getHeight() / 2) - 150, SpringLayout.NORTH, cont);
    layout.putConstraint(SpringLayout.WEST, op2, ((framePoker.getWidth() / 3) * 2) - 75, SpringLayout.WEST, cont);
    layout.putConstraint(SpringLayout.NORTH, op2, (framePoker.getHeight() / 2) - 50, SpringLayout.NORTH, cont);
    layout.putConstraint(SpringLayout.WEST, op3, ((framePoker.getWidth() / 3) * 2) - 75, SpringLayout.WEST, cont);
    layout.putConstraint(SpringLayout.NORTH, op3, (framePoker.getHeight() / 2) + 50, SpringLayout.NORTH, cont);

    layout.putConstraint(SpringLayout.WEST, inputText, 325, SpringLayout.WEST, cont);
    layout.putConstraint(SpringLayout.NORTH, inputText, 140, SpringLayout.NORTH, cont);

    // action listener for the submit button
    submit.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) 
        {
            startChips = Integer.parseInt(chipInput.getSelectedCheckbox().getLabel());
            numPlayers = Integer.parseInt(opponentInput.getSelectedCheckbox().getLabel()) + 1;
            chipText = new JTextArea[numPlayers * 2];

            // remove beginning buttons
            buttonPanel.remove(submit);
            buttonPanel.remove(chip100);
            buttonPanel.remove(chip500);
            buttonPanel.remove(chip900);
            buttonPanel.remove(op1);
            buttonPanel.remove(op2);
            buttonPanel.remove(op3);
            inputText.setVisible(false);    // still refuses to remove

            // display deal button
            framePoker.add(deal);

            layout.putConstraint(SpringLayout.WEST, deal, (framePoker.getWidth() / 2) - 75, SpringLayout.WEST, cont);
            layout.putConstraint(SpringLayout.NORTH, deal, 415, SpringLayout.NORTH, cont);
        }
    });

    // action listener for deal button
    deal.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent arg0) 
        {
            framePoker.remove(deal);

            test = new JTextArea("tessting");
            test.setVisible(true);

            textPanel.add(test);
            masterPanel.add(textPanel);
        }
    });
}

1 个答案:

答案 0 :(得分:1)

布局是惰性的,当您添加或删除组件时,容器不会立即验证(这会导致布局层次结构更新),这是出于性能原因而完成的。

因此,一旦您对用户界面已准备好(您已完成添加/删除内容)感到满意,就应该在容器上调用revalidaterepaint改性

不要将重量级(AWT)组件与轻量级(Swing)组件混合使用,这将导致问题无法解决。如果您不了解,ButtonCheckBox是重量级组件。有关替代方案,请参阅How to Use Buttons, Check Boxes, and Radio Buttons