Java IntelliJ IDEA无法将组件放到窗体上

Java IntelliJ IDEA无法将组件放到窗体上,java,intellij-idea,components,custom-controls,Java,Intellij Idea,Components,Custom Controls,我对这个想法很陌生,我想知道为什么我的自定义组件不能添加到GUI中。我创建了一个新项目,并添加了一个新的GUI和一个新的类,它是一个组件 以下是组件的代码: package comps; import javax.swing.*; /** * Created by danielmartin1 on 25.03.15. */ public class TestComp extends JLabel { } 我正在使用OSX Yosemite和JDK1.8 希望任何人都能帮助我。使用定制捆

我对这个想法很陌生,我想知道为什么我的自定义组件不能添加到GUI中。我创建了一个新项目,并添加了一个新的GUI和一个新的类,它是一个组件

以下是组件的代码:

package comps;

import javax.swing.*;

/**
 * Created by danielmartin1 on 25.03.15.
 */
public class TestComp extends JLabel {
}
我正在使用OSX Yosemite和JDK1.8


希望任何人都能帮助我。

使用定制捆绑的想法对我有所帮助。现在我可以在GUI中添加任何自定义组件。 但现在还有一些其他问题: 首先,组件在表单编辑器中看起来不像它应该的样子(它看起来像一个组合框), 第二:如果布局设置为GridlayoutManager(IntelliJ),我的程序在尝试运行时崩溃。选中Borderlayout后,它将运行,并且我的组件将正确显示(仅在运行时)

下面是我的组件的代码:

package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.*;

public class CustomTracker extends JComponent implements MouseListener {
/**
 *
 */
private static final long serialVersionUID = 1L;

private boolean isSelected = false;

public String getText() {
    return text;
}

public String text;

public int fontSize = 13;

private ImageIcon iIBack = new ImageIcon(getClass().getResource("/CheckBox/Tracker_Back.png"));
private ImageIcon iIButton = new ImageIcon(getClass().getResource("/CheckBox/Tracker_Button.png"));

private ImageIcon stretchedBack, stretchedButton;

private Font fontTracker = new Font("Arial", Font.BOLD, fontSize);
private Font fontText = new Font("Arial", Font.BOLD, fontSize);
private Color textColor = new Color(103, 125, 129);
private Color buttonColor = new Color(255, 255, 255);

private int backOriginalWidth, backOriginalHeight;
private int buttonOriginalWidth, buttonOriginalHeight;
private float scale = .7f;

/**
 * Constructor
 *
 * @param text
 *            set the text of this component
 */
public CustomTracker(String text) {
    this.text = text;
    this.setSize(this.getWidth(), iIBack.getIconHeight());
    this.addMouseListener(this);

    this.backOriginalWidth = iIBack.getIconWidth();
    this.backOriginalHeight = iIBack.getIconHeight();

    this.buttonOriginalWidth = iIButton.getIconWidth();
    this.buttonOriginalHeight = iIButton.getIconHeight();

    // Stretch Back
    Image img = iIBack.getImage();
    Image newimg = img.getScaledInstance((int) (backOriginalWidth * scale), (int) (backOriginalHeight * scale), java.awt.Image.SCALE_SMOOTH);
    stretchedBack = new ImageIcon(newimg);

    // Stretch Button
    img = iIButton.getImage();
    newimg = img.getScaledInstance((int) (buttonOriginalWidth * scale), (int) (buttonOriginalHeight * scale), java.awt.Image.SCALE_SMOOTH);
    stretchedButton = new ImageIcon(newimg);

    // stretch font
    this.fontSize = (int)(this.fontSize * scale);
    this.fontTracker = new Font("Arial", Font.BOLD, fontSize);
}

/**
 * get selected value
 */
//    @Override
//    public boolean isSelected() {
//        return this.isSelected;
//    }

/**
 * set selected value
 *
 * @param selected
 *            the value
 */
public void setSelected(boolean selected) {
    this.isSelected = selected;
}

@Override
public Dimension getMinimumSize() {
    return new Dimension(this.getWidth(), this.getHeight());
}

@Override
public Dimension getPreferredSize() {
    return new Dimension(this.getWidth(), this.getHeight());
}

/**
 * paint the component
 *
 * @param g
 *            the graphics object
 */
protected void paintComponent(Graphics g) {
    // create a Graphics2D Object
    Graphics2D g2d = (Graphics2D) g.create();

    // Draw Box Image
    g2d.drawImage(stretchedBack.getImage(), 0, 0, stretchedBack.getIconWidth(), stretchedBack.getIconHeight(), this);

    // Set Font-Values
    g2d.setFont(fontTracker);
    g2d.setColor(buttonColor);

    // Set text coordinates
    int x;
    int y = (int) (((iIBack.getIconHeight() / 2) + (fontSize / 2) + 1) * scale);

    if (this.isSelected == true) {
        g2d.drawImage(stretchedButton.getImage(), (int) (40 * scale), (int) (2 * scale), stretchedButton.getIconWidth(),
                stretchedButton.getIconHeight(), this);
        x = (int) (15 * scale);
        g2d.drawString("ON", x, y);
    } else {
        g2d.drawImage(stretchedButton.getImage(), 0, (int) (2 * scale), stretchedButton.getIconWidth(), stretchedButton.getIconHeight(), this);
        x = (int) (37 * scale);
        g2d.drawString("OFF", x, y);
    }

    // Draw ON or OFF
    g2d.drawString(getText(), (int)((iIBack.getIconWidth() + 10) * scale), y);

    // Set Font-Values
    g2d.setFont(fontText);
    g2d.setColor(textColor);

    // Set new y-Position
    y = ((stretchedBack.getIconHeight() / 2) + (fontSize / 2) + 1);

    // draw the text behind the Control
    g2d.drawString(getText(), stretchedBack.getIconWidth() + 10, y);

    // dispose Variables
    g2d.dispose();
}

@Override
public void setBounds(int x, int y, int width, int height) {
    super.setBounds(x, y, width, height);
}

/**
 * set the text of this component
 *
 * @param text
 *            the text to display
 */
public void setText(String text) {
    setText(text);
}

public void mouseClicked(MouseEvent e) {
    this.isSelected = !this.isSelected;
    repaint();
}

public void mousePressed(MouseEvent e) {
    // TODO Auto-generated method stub

}

public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub

}

public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub

}

public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub

}
}

有人知道这些问题的解决方案吗?

如果您使用JDK 1.8构建项目,IDEA也必须在JDK 1.8下运行,否则它将无法加载使用JDK 1.8编译的类。将目标语言级别更改为1.6,或使用JDK1.8 for Mac获得IDEA:。它有帮助吗?请包括一个更详细的描述它是如何不工作的。你有错误吗?或者这些组件显然没有添加到屏幕上?一个问题可能是组件的定位将其置于可见区域之外。另外,您如何将组件添加到GUI?也许您根本没有添加组件。仅仅创建一个扩展
JPanel
的新类是不够的;你必须使用它。但是您包含的唯一代码是定义组件的代码。