gpt4 book ai didi

java - JTextArea 麻烦

转载 作者:行者123 更新时间:2023-12-01 10:30:58 29 4
gpt4 key购买 nike

我正在制作一款扑克游戏,但遇到了一个问题,几乎所有事情都可以按照交易按钮的 actionListener 进行。它应该删除交易按钮并添加一个新的 JTextArea (此文本只是一个占位符)。在那之前几乎一切都有效,但在提交按钮 actionListener 中,它不会删除 inputText JTextArea (相反,我只是将 Visible 设置为 false)。如何添加另一个文本区域并将其显示在屏幕上?

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);
}
});
}

最佳答案

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

因此,一旦您对 UI 准备就绪(您已完成添加/删除内容)感到满意,您应该在容器上调用 revalidaterepaint )你修改了

不要将重量级 (AWT) 组件与轻量级 (Swing) 组件混合在一起,这会导致无穷无尽的问题。如果您不知道,ButtonCheckBox 是重量级组件。请参阅How to Use Buttons, Check Boxes, and Radio Buttons替代方案

关于java - JTextArea 麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35094758/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com