JTextArea 的麻烦
JTextArea trouble
我正在制作一个扑克游戏,我遇到了一个问题,几乎所有的事情都取决于交易按钮的 actionListener。它应该删除交易按钮并添加一个新的 JTextArea(此文本只是一个占位符)。在那之前几乎一切正常,但在提交按钮 actionListener 中,它不会删除 inputText JTextArea(相反,我只是将 Visible 设置为 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);
}
});
}
布局是惰性的,当您添加或删除组件时,不会立即验证容器(这会导致更新布局层次结构),这是出于性能原因。
因此,一旦您对 UI 准备就绪感到满意(您已经完成 adding/removing 内容),您应该在容器上调用 revalidate
和 repaint
(s) 你修改了
不要将重量级 (AWT) 组件与轻量级 (Swing) 组件混合使用,这会导致无穷无尽的问题。如果您不知道,Button
和 CheckBox
是重量级组件。请参阅 How to Use Buttons, Check Boxes, and Radio Buttons 了解替代方案
我正在制作一个扑克游戏,我遇到了一个问题,几乎所有的事情都取决于交易按钮的 actionListener。它应该删除交易按钮并添加一个新的 JTextArea(此文本只是一个占位符)。在那之前几乎一切正常,但在提交按钮 actionListener 中,它不会删除 inputText JTextArea(相反,我只是将 Visible 设置为 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);
}
});
}
布局是惰性的,当您添加或删除组件时,不会立即验证容器(这会导致更新布局层次结构),这是出于性能原因。
因此,一旦您对 UI 准备就绪感到满意(您已经完成 adding/removing 内容),您应该在容器上调用 revalidate
和 repaint
(s) 你修改了
不要将重量级 (AWT) 组件与轻量级 (Swing) 组件混合使用,这会导致无穷无尽的问题。如果您不知道,Button
和 CheckBox
是重量级组件。请参阅 How to Use Buttons, Check Boxes, and Radio Buttons 了解替代方案