切换复选框使用反应失败

时间:2016-10-01 05:54:39

标签: javascript reactjs

我接受了反应。但通常我都不知道错误是什么。下面的代码没有正确呈现,并且在jsbin的控制台中它没有显示错误。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;

public class Sample extends JFrame
{
    public Sample()
    {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(500, 300));

        GridBagConstraints cGBC = new GridBagConstraints();
        cGBC.insets = new Insets(5, 5, 5, 5);
        cGBC.gridx = 0;
        cGBC.gridy = 0;
        cGBC.weightx = .5;
        cGBC.weighty = 1;
        cGBC.anchor = GridBagConstraints.NORTHWEST;
        cGBC.fill = GridBagConstraints.BOTH;
        setLayout(new GridBagLayout());

        Font titleFont = new Font("", Font.PLAIN, 16);

        JPanel leftSide = new JPanel(new GridBagLayout());
        leftSide.setBorder(new EmptyBorder(5, 5, 5, 5));
        JLabel leftTitle = new JLabel("Objective");
        leftTitle.setFont(titleFont);
        leftTitle.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLACK));

        GridBagConstraints leftGBC = new GridBagConstraints();
        leftGBC.gridx =0;
        leftGBC.gridy = 0;
        leftGBC.fill = GridBagConstraints.HORIZONTAL;
        leftGBC.weightx = 1;
        leftGBC.weighty = .1;
        leftGBC.anchor = GridBagConstraints.NORTH;

        leftSide.add(leftTitle, leftGBC);
        leftGBC.gridy = 1;
        leftGBC.weightx = 0;

        //If you comment this section out, it will become 50/50 again
        JTextArea objectiveArea = new JTextArea("TestTTestTest TestTest TestTest TestTest TestTest TestTest "
                + "TestTest TestTest TestTest TestTest TestTest TestTest "
                + "TestTest TestTest TestTest TestTest TestTest ");
        objectiveArea.setEditable(false);
        objectiveArea.setFocusable(false);
        objectiveArea.setFont(new Font("", Font.PLAIN, 16));
        objectiveArea.setLineWrap(true);
        objectiveArea.setWrapStyleWord(true);
        leftSide.add(objectiveArea, leftGBC);

        add(leftSide, cGBC);
        cGBC.fill = GridBagConstraints.HORIZONTAL;
        cGBC.weightx = .5;
        cGBC.gridx = 1;

        JPanel rightSide = new JPanel(new GridBagLayout());
        rightSide.setBorder(new EmptyBorder(5, 5, 5, 5));
        JLabel rightTitle = new JLabel("Controls");
        rightTitle.setFont(titleFont);
        rightTitle.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLACK));

        GridBagConstraints rightGBC = new GridBagConstraints();
        rightGBC.gridx = 0;
        rightGBC.gridy = 0;
        rightGBC.fill = GridBagConstraints.HORIZONTAL;
        rightGBC.weightx = 1;
        rightGBC.weighty = 1;
        rightGBC.anchor = GridBagConstraints.NORTHWEST;

        rightSide.add(rightTitle, rightGBC);

        rightGBC.gridx = 0;
        rightGBC.gridy = 1;
        rightGBC.weightx = 0.5;
        rightGBC.fill = GridBagConstraints.HORIZONTAL;
        rightGBC.anchor = GridBagConstraints.NORTHWEST;

        JPanel controlPane = new JPanel(new GridLayout(3, 2, 2, 2));
        controlPane.add(new JLabel("test1"));
        controlPane.add(new JLabel("test2"));
        controlPane.add(new JLabel("test3"));
        controlPane.add(new JLabel("test4"));

        rightSide.add(controlPane, rightGBC);

        add(rightSide, cGBC);

        pack();
        setVisible(true);
    }

    public static void main(String[] args)
    {
        new Sample();
    }
}

https://jsbin.com/dehafizaba/1/edit 需要建议。

2 个答案:

答案 0 :(得分:1)

Boolean在React中打印不到。使用toString()将其转换为string

示例:https://jsfiddle.net/Pranesh456/557ab8wa/1/

答案 1 :(得分:0)

它“工作正常”。问题是React没有显示布尔值的字符串表示。如果你输出其他东西就可以了,例如:

<p>This box is {this.state.checked ? 'checked' : 'unchecked'}</p>