0

我目前的最新项目有问题。我是初学者,所以请记住这一点:D

我希望我的项目做的是在选中复选框时更改某些文本的字体,如果取消选中则使其恢复正常。

我已经设法实现了。

但是,有多行复选框。如果选择了 2 并且我取消选择其中之一,则文本将恢复正常。只要列中的复选框仍处于选中状态,我不希望文本变得正常。

我怎样才能做到这一点?=(

希望你们中的一个可以帮助我!

import java.awt.Color;

public class WindowBuilderTest extends JFrame
{

private JPanel  contentPane;

//Launch the application.
public static void main(final String[] args)
{
    EventQueue.invokeLater(new Runnable()
    {
        @Override
        public void run()
        {
            try
            {
                final WindowBuilderTest frame = new WindowBuilderTest();
                frame.setVisible(true);
            }
            catch (final Exception e)
            {
                e.printStackTrace();
            }
        }
    });
}

// Creating Frame
public WindowBuilderTest()
{
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 1280, 720);
    this.contentPane = new JPanel();
    this.contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(this.contentPane);
    this.contentPane.setLayout(null);

    // Create Font
    final Font headfont = new Font("Serif", Font.PLAIN, 15);
    final Font headfontRed = new Font("Serif", Font.PLAIN, 15);

    // Role Headlines
    final JTextArea txtTop = new JTextArea();
    txtTop.setEditable(false);
    txtTop.setText("TOP");
    txtTop.setBounds(180, 95, 32, 23);
    this.contentPane.add(txtTop);

    final JTextArea txtMid = new JTextArea();
    txtMid.setEditable(false);
    txtMid.setText("MID");
    txtMid.setBounds(252, 95, 32, 23);
    this.contentPane.add(txtMid);

    final JTextArea txtJng = new JTextArea();
    txtJng.setEditable(false);
    txtJng.setText("JNG");
    txtJng.setBounds(320, 95, 32, 23);
    this.contentPane.add(txtJng);

    final JTextArea txtAdc = new JTextArea();
    txtAdc.setEditable(false);
    txtAdc.setText("ADC");
    txtAdc.setBounds(389, 95, 32, 23);
    this.contentPane.add(txtAdc);

    final JTextArea txtSup = new JTextArea();
    txtSup.setEditable(false);
    txtSup.setText("SUP");
    txtSup.setBounds(453, 95, 32, 23);
    this.contentPane.add(txtSup);

    // Checkbox 1st row
    addCheckBox(183, 143, 23, 23, txtTop);
    addCheckBox(255, 143, 23, 23, txtMid);
    addCheckBox(322, 143, 23, 23, txtJng);
    addCheckBox(393, 143, 23, 23, txtAdc);
    addCheckBox(457, 143, 23, 23, txtSup);

    // Checkbox 2nd row
    addCheckBox(183, 200, 23, 23, txtTop);
    addCheckBox(255, 200, 23, 23, txtMid);
    addCheckBox(322, 200, 23, 23, txtJng);
    addCheckBox(393, 200, 23, 23, txtAdc);
    addCheckBox(457, 200, 23, 23, txtSup);

    // Checkbox 3nd row
    addCheckBox(183, 257, 23, 23, txtTop);
    addCheckBox(255, 257, 23, 23, txtMid);
    addCheckBox(322, 257, 23, 23, txtJng);
    addCheckBox(393, 257, 23, 23, txtAdc);
    addCheckBox(457, 257, 23, 23, txtSup);
}


private void addCheckBox(final int x, final int y, final int width, final int height, final JTextArea txtTop)
{
    final JCheckBox checkBox = new JCheckBox();
    checkBox.setBounds(x, y, width, height);
    checkBox.addItemListener(new FontChanger(txtTop));
    this.contentPane.add(checkBox);
}

class FontChanger implements ItemListener
{
    private JTextArea textArea;

    public FontChanger(final JTextArea textArea)
    {
        this.textArea = textArea;
    }

    @Override
    public void itemStateChanged(final ItemEvent e)
    {
        if (e.getStateChange() == ItemEvent.SELECTED)
        {
            final Font boldFont = this.textArea.getFont().deriveFont(Font.BOLD);
            this.textArea.setForeground(Color.RED);
            this.textArea.setFont(boldFont);
        }
        else
        {
            final Font boldFont = this.textArea.getFont().deriveFont(Font.PLAIN);
            this.textArea.setForeground(Color.BLACK);
            this.textArea.setFont(boldFont);
        }
    }
}

}

4

1 回答 1

0

有几种方法可以实现您想要尝试做的事情,但我相信这个问题更具概念性。您添加到面板的每个复选框都有自己的* FontChanger。这些 FontChangers 中的每一个都不知道其他 FontChangers 或复选框。您可以做的是将单个 FontChanger 添加到所有复选框,然后让您的单个 FontChanger 知道 itemStateChanged() 方法中每个复选框的状态。这样,您可以检查是否/多少/哪个复选框被选中并采取行动。

编辑:这是一个完整的例子(注意我已经广泛地修改了你的代码并且没有测试它,这只是为了给你一个大致的想法。此外,为了代码的可读性,我去掉了大部分最终关键字,如果你有没有特定理由使用不同的线程来运行您的程序,无需在新的 Runnable() 中运行 WindowBuilderTest,请参阅注释部分)。

import java.awt.Color;
import java.util.*;

public class WindowBuilderTest extends JFrame
{

private JPanel  contentPane;
private FontChanger fChanger; //<--NEW
private JTextArea txtTop; //<--NEW
private List<CheckBox> checkBoxes; //<--NEW

//Launch the application.
public static void main(final String[] args)
{
    EventQueue.invokeLater(new Runnable()
    {
        @Override
        public void run()
        {
            try
            {
                WindowBuilderTest frame = new WindowBuilderTest();
                frame.setVisible(true);
            }
            catch (final Exception e)
            {
                e.printStackTrace();
            }
        }
    });

    //just use this and comment out the whole Eventqueue block.
    /*
    WindowBuilderTest frame = new WindowBuilderTest();
    frame.setVisible(true);
    */
}

// Creating Frame
public WindowBuilderTest()
{
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 1280, 720);
    this.contentPane = new JPanel();
    this.contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(this.contentPane);
    this.contentPane.setLayout(null);

    // Create Font
    Font headfont = new Font("Serif", Font.PLAIN, 15);
    Font headfontRed = new Font("Serif", Font.PLAIN, 15);

    // Role Headlines
    txtTop = new JTextArea(); //<--NEW
    fChanger = new FontChanger(txtTop); //<--NEW
    checkBoxes = new ArrayList<CheckBox>(); //<-- NEW you will need to update your imports.

    txtTop.setEditable(false);
    txtTop.setText("TOP");
    txtTop.setBounds(180, 95, 32, 23);
    this.contentPane.add(txtTop);

    final JTextArea txtMid = new JTextArea();
    txtMid.setEditable(false);
    txtMid.setText("MID");
    txtMid.setBounds(252, 95, 32, 23);
    this.contentPane.add(txtMid);

    final JTextArea txtJng = new JTextArea();
    txtJng.setEditable(false);
    txtJng.setText("JNG");
    txtJng.setBounds(320, 95, 32, 23);
    this.contentPane.add(txtJng);

    final JTextArea txtAdc = new JTextArea();
    txtAdc.setEditable(false);
    txtAdc.setText("ADC");
    txtAdc.setBounds(389, 95, 32, 23);
    this.contentPane.add(txtAdc);

    final JTextArea txtSup = new JTextArea();
    txtSup.setEditable(false);
    txtSup.setText("SUP");
    txtSup.setBounds(453, 95, 32, 23);
    this.contentPane.add(txtSup);

    // Checkbox 1st row
    addCheckBox(183, 143, 23, 23, txtTop);
    addCheckBox(255, 143, 23, 23, txtMid);
    addCheckBox(322, 143, 23, 23, txtJng);
    addCheckBox(393, 143, 23, 23, txtAdc);
    addCheckBox(457, 143, 23, 23, txtSup);

    // Checkbox 2nd row
    addCheckBox(183, 200, 23, 23, txtTop);
    addCheckBox(255, 200, 23, 23, txtMid);
    addCheckBox(322, 200, 23, 23, txtJng);
    addCheckBox(393, 200, 23, 23, txtAdc);
    addCheckBox(457, 200, 23, 23, txtSup);

    // Checkbox 3nd row
    addCheckBox(183, 257, 23, 23, txtTop);
    addCheckBox(255, 257, 23, 23, txtMid);
    addCheckBox(322, 257, 23, 23, txtJng);
    addCheckBox(393, 257, 23, 23, txtAdc);
    addCheckBox(457, 257, 23, 23, txtSup);
}


private void addCheckBox(int x, int y, int width, int height)
{
    JCheckBox checkBox = new JCheckBox();
    checkBoxes.add(checkBox); // <-- NEW (add to checkboxes list)
    checkBox.setBounds(x, y, width, height);
    checkBox.addItemListener(fChanger); // <-- NEW (add the same listener to all)
    this.contentPane.add(checkBox);
}

class FontChanger implements ItemListener
{
    private JTextArea textArea;

    public FontChanger(JTextArea textArea)
    {
        this.textArea = textArea;
    }

    @Override
    public void itemStateChanged(ItemEvent e)
    {
        if (e.getStateChange() == ItemEvent.SELECTED)
        {
            Font boldFont = this.textArea.getFont().deriveFont(Font.BOLD);
            this.textArea.setForeground(Color.RED);
            this.textArea.setFont(boldFont);
        }
        else
        {
            //iterate through list of checkboxes to see if there's still one selected.
            boolean stillOneCheckBoxSelected = false;
            for(int i = 0; i < checkBoxes.size(); i++){
                if(checkBoxes.get(i).isSelected()){ // <- Verify the method here, not sure
                    stillOneCheckBoxSelected = true;
                    break;
                }
            }
            if(!stillOneCheckBoxSelected){//if no check boxes are selected, reset
                Font boldFont = this.textArea.getFont().deriveFont(Font.PLAIN);
                this.textArea.setForeground(Color.BLACK);
                this.textArea.setFont(boldFont);
            }
        }
    }
}

再说一次,我还没有测试过,你可能需要修补一些东西,但我认为你可以从这里继续。这是 ArrayLists 的 JavaDoc 的链接,您可能需要查阅:ArrayList JavaDoc

第二次编辑: 我重新阅读了您的问题,您实际上只需要针对一组特定列的此逻辑,因此您必须更改逻辑以仅将相关复选框添加到列表中,以便每次进行检查时,只有相关的复选框被选中。

于 2013-09-26T15:27:35.960 回答