2014-09-20 49 views
0

Q1:我的JTextFeild和JMenuBar不顯示,我不知道爲什麼 有一個程序的圖片。 Q2:我已經看到了很多編寫界面程序的方法,我不知道哪種方法更好。是這樣或這樣:(?在windowhttps://www.youtube.com/watch?v=706Ye4ubtEY爲什麼我的Jtextfield和jmenubar不起作用

import javax.swing.*; 
import java.awt.*; 
import javax.swing.JTextField; 
import javax.swing.JMenuBar; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

public class Real_Interface extends JFrame implements ActionListener { 

    public Real_Interface(){ 
     JFrame window = new JFrame(); 
     window.add(new JLabel("Rocket Data Visualization Tool")); 
     window.setTitle("Rocket Data Visualization Tool"); 
     window.setSize(640, 480); 
     window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     window.setLocationRelativeTo(null); 
     window.setVisible(true); 
     window.setResizable(false); 

     JTextField tf = new JTextField(); 
     tf.setText("New Text"); 
     String str = tf.getText(); 
     tf.setVisible(true); 

     JMenuBar bar = new JMenuBar(); 
     JMenu file = new JMenu("File"); 
     JMenuItem menuItem = new JMenuItem("haha"); 
     JMenuItem menuItem2 = new JMenuItem("haha2"); 
     file.add(menuItem); 
     file.addSeparator(); 
     file.add(menuItem2); 
     bar.add(file); 


     JButton button = new JButton("OK"); 
     window.add(button); 
     button.setBounds(250, 400, 150, 40); 
     button.addActionListener(this); 
    } 
    public static void main(String[] args){ 
     new Real_Interface(); 
    } 

    public void actionPerformed(ActionEvent e) { 
     System.out.println("The Button Works!"); 
     Plot.main(null); 
    } 

} 

回答

3

幾個原因

的菜單欄尚未分配到

window.setJMenuBar(bar); 

該文本框尚未添加

window.add(textfield, BorderLayout.PAGE_START); 

此組件框架時默認調用setVisible是可見不必要。

的幀需要畢竟成分被添加到幀,而不是預先

window.setVisible(true); 
可以變得可見
2

JTextField tfJMenuBar bar不會添加到他們的父母。