【问题标题】:IntelliJ IDEA cannot drop component onto formIntelliJ IDEA 无法将组件拖放到表单上
【发布时间】:2015-05-29 18:02:59
【问题描述】:

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

这是组件的代码:

package comps;

import javax.swing.*;

/**
 * Created by danielmartin1 on 25.03.15.
 */
public class TestComp extends JLabel {
}

我正在使用带有 JDK 1.8 的 OSX Yosemite

希望任何人都可以帮助我。

【问题讨论】:

  • 如果你用JDK 1.8构建你的项目,IDEA也必须在JDK 1.8下运行,否则将无法加载用JDK 1.8编译的类。将目标语言级别更改为 1.6 或使用 JDK 1.8 for Mac 获取 IDEA:download.jetbrains.com/idea/ideaIU-14.1-custom-jdk-bundled.dmg。有帮助吗?
  • 请详细说明如何它不工作。你有错误吗?或者这些组件显然没有添加到屏幕上?一个问题可能是组件的定位使其超出了可见区域。
  • 另外,您是如何将组件添加到 GUI 的?也许您根本没有添加组件。仅仅创建一个扩展JPanel 的新类是不够的;你必须使用它。但是您包含的唯一代码是定义组件的代码。

标签: java intellij-idea components custom-controls


【解决方案1】:

使用自定义捆绑的 IDEA 有所帮助。现在我可以在 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

}
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-24
    • 2017-06-28
    • 1970-01-01
    • 2018-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多