嘿,我實際上是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);
}
}
Uhhmm..sir可以指出我在哪裏把這行代碼放在我的程序中。 – BlackLotus
我喜歡叫先生。在將jPanes添加到幀後,只需添加這些行。所以jPanel會像以前一樣將其顏色和其他所有東西都拼湊起來。 – LenglBoy
非常感謝您先生 – BlackLotus