我需要创建将我的jframe的背景设置为图片任何人都可以帮助我吗? 我还有一个名为FolderCreate的第二个类,但它根本不使用框架。它会让布局看起来很糟糕。
/* Conrad Rowan & Hayley Cummings
* Period 6
* Address book
*/
//change all the text fields to text areas
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import javax.swing.AbstractButton;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Scanner;
import javax.imageio.ImageIO;
public class AddressFinal implements ActionListener, ListSelectionListener{
// The start.
JLabel myLabel = new JLabel(new ImageIcon("C/Users/399430/Desktop/eagle.jpg"));
//JPanels's
JPanel panel = new JPanel();
JPanel panel2 = new JPanel();
String toThings = new String();
//JFrame's
JFrame frame = new JFrame();
//Containers
Container east = new Container();
Container south = new Container();
Container west = new Container();
Container north = new Container();
//JButtons
JButton create = new JButton("Create");
JButton save = new JButton("Save");
//The Layout
GridLayout grid = new GridLayout(3, 1);
//Text Fields
JTextArea labelsTF = new JTextArea("Enter in some information in the box to the bottom right, if you'd like!");
JTextArea finderBarTF = new JTextArea("Type in your new contact name.");
JTextArea Contact2 = new JTextArea
("Last Name: " + "\n" + "\n" +
"Street address: " + "\n" + "\n" +
"City: " + "\n" + "\n" +
"State: " + "\n" + "\n" +
"ZIP code: " + "\n" + "\n" +
"Phone number: " + "\n" + "\n" +
"Extra: " + "\n" + "\n"
);
DefaultListModel data = new DefaultListModel();
JList Contact1;
FolderCreate folder = new FolderCreate();
public AddressFinal() {
Contact2.setFont(new Font("Serif", Font.PLAIN, 14));
//data[1] = "george";
Contact1 = new JList(data);
toThings = Contact2.getText();
panel.setOpaque(false);
// Set the sizes
finderBarTF.setSize(50, 60);
panel.setSize(825, 625);
frame.setSize(850, 650);
create.setSize(50, 60);
Contact1.setSize(100, 60);
save.setSize(50, 60);
labelsTF.setSize(50, 60);
east.setSize(400, 300);
west.setSize(400, 300);
frame.setLayout(new BorderLayout());
panel.setLayout(new BorderLayout());
Contact1.setOpaque(false);
save.setOpaque(false);
save.setContentAreaFilled(false);
save.setBorderPainted(false);
create.setOpaque(false);
create.setContentAreaFilled(false);
create.setBorderPainted(false);
// north east south west stuff
Contact2.setOpaque(false);
finderBarTF.setOpaque(false);
labelsTF.setOpaque(false);
south.setLayout(new GridLayout(1, 2));
north.setLayout(new GridLayout(2, 2));
//finderBarTF.addActionListener(this);
north.add(labelsTF);
create.addActionListener(this);
//labelsTF.addActionListener(this);
save.addActionListener(this);
south.add(Contact2);
south.add(Contact1);
Contact1.addListSelectionListener(this);
final File folder = new File("contacts");
listFilesForFolder(folder);
east.setLayout(grid);
//south.setLayout(grid);
north.add(create);
north.add(finderBarTF);
north.add(save);
((javax.swing.DefaultListCellRenderer)Contact1.getCellRenderer()).setOpaque(false);
panel.add(south, BorderLayout.CENTER);
panel.add(north, BorderLayout.NORTH);
panel2.add(east, BorderLayout.EAST);
frame.add(panel);
//JTextArea.setBackground(Color.RED);
frame.add(panel2);
frame.add(east, BorderLayout.EAST);
north.add(finderBarTF);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.add(myLabel);
}
public static void main(String[] args) {
new AddressFinal();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == create ) {
panel.setBackground(Color.BLUE);
toThings = Contact2.getText();
folder.createFile(finderBarTF.getText(), toThings);
data.addElement(finderBarTF.getText());
}
if (e.getSource() == save) {
toThings = Contact2.getText();
folder.createFile((String) Contact1.getSelectedValue(), toThings);
}
}
@Override
public void valueChanged(ListSelectionEvent e) {
System.out.println(Contact1.getSelectedValue());
File f = new File("contacts"+File.separator+(String) Contact1.getSelectedValue());
Scanner s;
try {
s = new Scanner(f);
String input = "";
while (s.hasNext()){
input = input + s.nextLine();
input = input + "\n";
}
s.close();
Contact2.setText(input);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
public void listFilesForFolder(final File folder) {
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
listFilesForFolder(fileEntry);
} else {
//System.out.println(fileEntry.getName());
data.addElement(fileEntry.getName());
}
}
}
}
答案 0 :(得分:0)
据我所知,设置JFrame的背景是不可能的,但是,您可以在标签内设置图像,然后将标签用作图像。
JLabel myLabel = new JLabel(new ImageIcon("/path/to/file.jpg"));