Возможный дубликат:
Как установить изображение в качестве фона для фрейма в графическом интерфейсе Swing java?
Мне сложно понять, как установить фоновое изображение в моем окне. Я не уверен, что случилось с моим кодом. Изображение называется Background.jpg
Кто-нибудь знает, что я делаю неправильно с моим кодом и как его можно исправить.
//apples code
import javax.swing.JFrame;
import java.applet.*;
import javax.swing.*;
import java.awt.*;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import java.awt.*;
import javax.swing.*;
class Apples{
public static void main(String[] args) {
gui go = new gui();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(200,290);
go.setVisible(true);
}
}
//Gui Code
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class gui extends JFrame{
private JButton A;
private JButton B;
private JButton C;
private JButton D;
private JButton E;
private JButton F;
private JButton G;
private JButton H;
private JButton I;
private JButton J;
private JButton K;
private JButton L;
private JButton M;
private JButton N;
private JButton O;
private JButton P;
private JButton Q;
private JButton R;
private JButton S;
private JButton T;
private JButton U;
private JButton V;
private JButton W;
private JButton X;
private JButton Y;
private JButton Z;
public gui(){
super("APUSH ABCs");
setLayout(new FlowLayout());
A = new JButton("A");
add(A);
B = new JButton("B");
add(B);
C = new JButton("C");
add(C);
D = new JButton("D");
add(D);
E = new JButton("E");
add(E);
F = new JButton("F");
add(F);
G = new JButton("G");
add(G);
H = new JButton("H");
add(H);
I = new JButton("I");
add(I);
J = new JButton("J");
add(J);
K = new JButton("K");
add(K);
L = new JButton("L");
add(L);
M = new JButton("M");
add(M);
N = new JButton("N");
add(N);
O = new JButton("O");
add(O);
P = new JButton("P");
add(P);
Q = new JButton("Q");
add(Q);
R = new JButton("R");
add(R);
S = new JButton("S");
add(S);
T = new JButton("T");
add(T);
U = new JButton("U");
add(U);
V = new JButton("V");
add(V);
W = new JButton("W");
add(W);
X = new JButton("X");
add(X);
Y = new JButton("Y");
add(Y);
Z = new JButton("Z");
add(Z);
HandlerClass handler = new HandlerClass();
A.addActionListener(handler);
B.addActionListener(handler);
C.addActionListener(handler);
D.addActionListener(handler);
E.addActionListener(handler);
F.addActionListener(handler);
G.addActionListener(handler);
H.addActionListener(handler);
I.addActionListener(handler);
J.addActionListener(handler);
K.addActionListener(handler);
L.addActionListener(handler);
M.addActionListener(handler);
N.addActionListener(handler);
O.addActionListener(handler);
P.addActionListener(handler);
Q.addActionListener(handler);
R.addActionListener(handler);
S.addActionListener(handler);
T.addActionListener(handler);
U.addActionListener(handler);
V.addActionListener(handler);
W.addActionListener(handler);
X.addActionListener(handler);
Y.addActionListener(handler);
Z.addActionListener(handler);
}
private class HandlerClass implements ActionListener{
public void actionPerformed(ActionEvent event){
Object button = event.getSource();
if(button == A){
String text = "asaasas ";
JTextArea textArea = new JTextArea(text);
textArea.setColumns(30);
textArea.setLineWrap( true );
textArea.setWrapStyleWord( true );
textArea.setSize(textArea.getPreferredSize().width, 1);
Font font = new Font("Verdana", Font.BOLD, 12);
textArea.setFont(font);
textArea.setForeground(Color.BLUE);
JOptionPane.showMessageDialog(
null, textArea, "Appomattox Court House", JOptionPane.PLAIN_MESSAGE);
}else if(button == B){
String text = "Duriing the last days of the Civil War, Robert E. Lee had abandoned the Confederate capital of Richmond and city of Petersburg, hoping to escape with the remnants of his Army of Northern Virginia to meet up with additional Confederate forces in North Carolina and resume fighting. Union Forces cut him off and finally the Continental army had to surrender. The Appomattox Courthouse is the locaiton where General Robert E. Lee of the Confederate Army surrendered his approximately 28,000 troops to Ulysses S. Grant and the Union Army on April 9, 1865, ending the American Civil War. ";
JTextArea textArea = new JTextArea(text);
textArea.setColumns(30);
textArea.setLineWrap( true );
textArea.setWrapStyleWord( true );
textArea.setSize(textArea.getPreferredSize().width, 1);
Font font = new Font("Verdana", Font.BOLD, 12);
textArea.setFont(font);
textArea.setForeground(Color.BLUE);
JOptionPane.showMessageDialog(
null, textArea, "Appomattox Court House", JOptionPane.PLAIN_MESSAGE);
} else if(button == C){
String text = "Duriing the last days of the Civil War, Robert E. Lee had abandoned the Confederate capital of Richmond and city of Petersburg, hoping to escape with the remnants of his Army of Northern Virginia to meet up with additional Confederate forces in North Carolina and resume fighting. Union Forces cut him off and finally the Continental army had to surrender. The Appomattox Courthouse is the locaiton where General Robert E. Lee of the Confederate Army surrendered his approximately 28,000 troops to Ulysses S. Grant and the Union Army on April 9, 1865, ending the American Civil War. ";
JTextArea textArea = new JTextArea(text);
textArea.setColumns(30);
textArea.setLineWrap( true );
textArea.setWrapStyleWord( true );
textArea.setSize(textArea.getPreferredSize().width, 1);
Font font = new Font("Verdana", Font.BOLD, 12);
textArea.setFont(font);
textArea.setForeground(Color.BLUE);
JOptionPane.showMessageDialog(
null, textArea, "Appomattox Court House", JOptionPane.PLAIN_MESSAGE);
}
}
}