2017-10-19 80 views
0

嘿,我實際上是java編程中的新手。我可以把背景顏色的代碼行放在哪裏。因爲當我在main方法中使用黃色的jpanel時。設置在jframe工作背景顏色,但jlabel,jtextfield和jbutton現在失蹤..一切都只是黃色。如何在java中設置背景顏色

package testpath; 
import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 
import java.io.*; 

public class Testpath extends JFrame { 

     JLabel label; 
     JTextField tf; 
     JButton button; 

public Testpath(){ 
     setLayout(new FlowLayout()); 

     label= new JLabel("Enter First Name"); 
     add(label); 

     tf=new JTextField(10); 
     add(tf); 

     button=new JButton("Log In"); 
     add(button); 

     event e=new event(); 
    button.addActionListener(e); 
    } 

public class event implements ActionListener{ 
    public void actionPerformed(ActionEvent e){ 
     try{ 
      String word=tf.getText(); 
      FileWriter stream= new  FileWriter("C://Users//Keyboard//Desktop//file.txt"); 
      BufferedWriter out=new BufferedWriter(stream); 
      out.write(word); 
     }catch (Exception ex){} 
    } 
} 


public static void main(String[] args) { 
    Testpath gui=new Testpath(); 
    gui.setLocationRelativeTo(null); 
    gui.setVisible(true); 
    gui.setSize(400,250); 
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

} 

}

回答

1

更改jPanel BACKGROUNDCOLOR:jPanel.setBackground(Color.YELLOW); 然後你需要設置jPanel.setOpaque(false);因爲默認it's透明。 如果您只是將其添加到特定組件,則其他組件不會更改其顏色。

+0

Uhhmm..sir可以指出我在哪裏把這行代碼放在我的程序中。 – BlackLotus

+0

我喜歡叫先生。在將jPanes添加到幀後,只需添加這些行。所以jPanel會像以前一樣將其顏色和其他所有東西都拼湊起來。 – LenglBoy

+0

非常感謝您先生 – BlackLotus

0

嘗試開發使用netbeans。它具有使用IDE本身進行裝飾的所有功能。然後你可以在源代碼中看到代碼。

+0

非常感謝您先生。我會盡快嘗試您的解決方案。 – BlackLotus