2017-01-22 26 views
0

我试图制作一个模型浏览器,但是这一个错误不断出现,看起来很好的代码行。找不到添加(TextField)的合适方法?

import java.awt.Container; 
import java.awt.Font; 
import java.awt.Insets; 
import javafx.scene.control.TextField; 
import javax.swing.JEditorPane; 
import javax.swing.JFrame; 
import javax.swing.JScrollPane; 

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 

/** 
* 
* @author PC 
*/ 
public class Browser extends JFrame{ 
    private TextField field = new TextField(); 
    private JEditorPane display = new JEditorPane(); 
    private JScrollPane scroller = new JScrollPane(display); 

    public static void main(String args[]){ 
    Browser file = new Browser(); 
    file.framelander(); 
    } 

    public void framelander() { 
     setTitle("Browser"); 
     setSize(1200, 800); 
     setResizable(false); 
     setVisible(true); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLayout(null); 
     setLocationRelativeTo(null); 
     addComponentsToFrame(getContentPane()); 
    } 

    public void addComponentsToFrame(Container pane) { 
     Insets insets = getInsets(); 

     pane.add(field); 
     pane.add(scroller); 

     Font font = new Font("Menlo", Font.PLAIN, 11); 
    field.setFont(font); 
    } 



} 

我得到的错误是在pane.add(field)与错误;

no suitable method found for add(TextField) 
    method Component.add(PopupMenu) is not applicable 
     (argument mismatch; TextField cannot be converted to PopupMenu) 
    method Container.add(Component) is not applicable 
     (argument mismatch; TextField cannot be converted to Component) 
---- 
(Alt-Enter shows hints) 

我也越来越“不兼容的类型:java.awt.Font中不能转换到javafx.scene.text.Font”在field.setFont(字体)的加粗部分;但我假设这是由于最初的错误。不过,我会在这里发布它。

任何和所有的帮助表示赞赏。提前致谢。

+0

你的提示应该是“无法转换”以及包名......“javafx”和“awt”类不是完全兼容 –

回答

3

您输入了错误的TextField

import javafx.scene.control.TextField; 

你可能要导入此一:

import javax.swing.JTextField; 

,然后改变fieldJTextField

private JTextField field = new JTextField(); 

这应该可以解决你所描述的错误。 此外,你的JFrame应该有一个布局集,我建议你看看这个指南A Visual Guide to Layout Managers

+0

''你可能想要导入这个:java.awt.TextField“ - 不是。如果不需要,不要鼓励混合AWT和Swing组件。 –

+0

除了使用Swing中的JTextField,我不明白为什么他不会使用那个,基于上下文以及他如何使用它。我刚刚看到他正在使用的课程的名称,并与之一起使用,但我明白你的观点。 –

相关问题