for my program I currently want to use the open button to open a JFileChooser and select an image and then draw it on the JPanel on the left side of the applet, I know that the file is being retrieved but when i go to repaint the graphics context nothing happens. Thanks in advance.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
public class FinalProject390 {
public static void main(String[] args) {
createAndShowGUI();
}
private static void createAndShowGUI() {
JFrame f = new JFrame("Final Project");
f.setSize(1025, 520);
f.add(new GraphicsPanel());
f.add(new MainPanel());
f.setVisible(true);
}
}
class MainPanel extends JPanel implements ActionListener {
JButton openButton = new JButton("Open");
JButton newButton = new JButton("New");
JButton saveButton = new JButton("Save");
JButton saveAsButton = new JButton("Save As");
JButton drawLineButton = new JButton("Draw Line");
JButton drawRectangleButton = new JButton("Draw Rectangle");
JButton drawOvalButton = new JButton("Draw Oval");
JButton drawArcButton = new JButton("Draw Arc");
JButton extractPixelDataButton = new JButton("Extract Pixel Data");
JButton convoluteButton = new JButton("Convolute");
JTextField xPosStartField = new JTextField("x-Position Start");
JTextField yPosStartField = new JTextField("y-Position Start");
JTextField xPosEndField = new JTextField("x-Position End");
JTextField yPosEndField = new JTextField("y-Position End");
JTextField angleStartField = new JTextField("Angle Start");
JTextField angleSizeField = new JTextField("Angle Size");
public MainPanel() {
openButton.setBounds(660, 50, 100, 30);
openButton.addActionListener(this);
newButton.setBounds(780, 50, 100, 30);
saveButton.setBounds(900, 50, 100, 30);
drawLineButton.setBounds(660, 110, 160, 30);
drawRectangleButton.setBounds(840, 110, 160, 30);
drawOvalButton.setBounds(660, 155, 160, 30);
drawArcButton.setBounds(840, 155, 160, 30);
extractPixelDataButton.setBounds(660, 220, 160, 30);
convoluteButton.setBounds(840, 220, 160, 30);
xPosStartField.setBounds(660, 280, 100, 30);
yPosStartField.setBounds(660, 320, 100, 30);
xPosEndField.setBounds(660, 380, 100, 30);
yPosEndField.setBounds(660, 420, 100, 30);
angleStartField.setBounds(900, 280, 100, 30);
angleSizeField.setBounds(900, 320, 100, 30);
setLayout(null);
setBackground(Color.green);
setBounds(641, 0, 370, 480);
add(openButton);
add(newButton);
add(saveButton);
add(drawLineButton);
add(drawRectangleButton);
add(drawOvalButton);
add(drawArcButton);
add(extractPixelDataButton);
add(convoluteButton);
add(xPosStartField);
add(yPosStartField);
add(xPosEndField);
add(yPosEndField);
add(angleStartField);
add(angleSizeField);
}
#Override
public void actionPerformed(ActionEvent e) {
javax.swing.JFileChooser fileChooser = new JFileChooser();
BufferedImage bin = null, bi = null;
GraphicsPanel gPanel = new GraphicsPanel();
fileChooser.setCurrentDirectory(new File(System
.getProperty("user.home")));
int result = fileChooser.showOpenDialog(null);
if (result == javax.swing.JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
System.out.println("Selected file: "
+ selectedFile.getAbsolutePath());
try {
bin = ImageIO.read(new File(selectedFile.getAbsolutePath()));
} catch (IOException e1) {
e1.printStackTrace();
}
gPanel.setImg(bin);
}
}
}
class GraphicsPanel extends JPanel {
BufferedImage bi = null, bin = null;
public GraphicsPanel() {
setLayout(null);
setBackground(Color.blue);
setSize(640, 480);
setLocation(0, 0);
}
public void setImg(BufferedImage b) {
this.bi = b;
repaint();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bi, 0, 0, null);
}
}
This looks like a homework assignment, so rather than an outright answer with code, instead please consider the following:
In your method createAndShowGUI() you instantiate a GraphicsPanel object and add it to a JFrame
In your actionPerformed() method, you instantiate another GraphicsPanel object (which is never added to the JFrame) and you call setImage().
Your image does not display because the GraphicsPanel control that has been added to your JFrame is not the same GraphicsPanel control that you set the image on.
I hope this will be enough to help you fix your code.
Related
i'm currently adding functionality and completing a Hang-Man game my programing teacher made.
I added a frame to enable the user to write their own word to play the game however i stumbled on the following problem.
The PaintComponent that is supposed to draw when the user guesses the wrong word does not work. it does not draw when i guess the wrong word. (right now the word to guess is set to "hello" but i am going to change it after PaintComponent works).
i have tried to put all the components including the object to a JPanel but after i tried to do that the JFrame did not show upp after i pressed the button (b).
should i try with JPanel again or can i do anything to solve it as it currently is. i would like to point out that my teachers original code does not have the components put into a JPanel, he only has a frame. However his class extends with JPanel.
Anny suggestions ?
package hangtest2;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class HangTest2 {
public static void main(String[] args) {
// frame intro skärm
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("HangMan");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(3,1));
//label till orden
JLabel intro = new JLabel();
String ord = "Valfri";
intro.setText(ord);
Font font = new Font("Verdana", Font.BOLD, 25);
intro.setFont(font);
//till att välja ord
JTextField field = new JTextField(10);
field.setFont(font);
JButton b = new JButton("ok");
b.setBounds(370, 300, 100, 30);
frame.add(intro);
frame.add(field);
frame.add(b);
frame.setBackground(Color.white);
frame.setSize(600, 400);
frame.setVisible(true);
// frame hang
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame2 = new JFrame("HangMan");
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setLayout(null);
JPanel p2 = new JPanel();
p2.setLayout(null);
JLabel hang = new JLabel();
String firstinstruct = "Gissa genom att Mata in en bokstav ";
hang.setText(firstinstruct);
Font font2 = new Font("Verdana", Font.BOLD, 10);
hang.setFont(font2);
hang.setBounds(70, 100, 600,150 );
JTextField field2 = new JTextField();
field2.setVisible(true);
field2.setSize(300, 30);
field2.setLocation(60, 300);
field2.setVisible(true);
JButton b2 = new JButton("ok");
b2.setBounds(370, 300, 100, 30);
frame2.add(b2);
frame2.add(field2);
frame2.add(hang);
frame2.setBackground(Color.white);
frame2.setSize(600, 400);
DynamicHangTest2 object = new DynamicHangTest2(field, field2, b, b2, frame, frame2);
b.addActionListener(object);
field.addActionListener(object);
b2.addActionListener(object);
field2.addActionListener(object);
frame2.add(object);
frame2.setVisible(false);
}
}
The Dynamic Class
package hangtest2;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class DynamicHangTest2 extends JPanel implements ActionListener, MouseListener {
private int error;
public static String a = "hello";
JFrame frame, frame2;
JButton b,b2;
JTextField field, field2;
public DynamicHangTest2(JTextField field, JTextField field2, JButton b, JButton b2, JFrame frame, JFrame frame2) {
this.frame = frame;
this.frame2 = frame2;
this.b = b;
this.b2 = b2;
this.field = field;
this.field2 = field2;
}
#Override
public void mouseClicked(MouseEvent e) {
System.out.println(e.getX() + " " + e.getY());
}
#Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if(source.equals(b)) {
frame.setVisible(false);
frame2.setVisible(true);
}
if (source.equals(b2)) {
if (!a.contains(field2.getText())) {
error++;
frame2.repaint();
}
field2.setText("");
}
}
public void paintComponent(Graphics g) {
if (error == 1)
g.drawLine(10, 270, 500, 270);
if (error == 2) {
g.drawLine(10, 270, 500, 270);
g.drawLine(200, 30, 200, 270);
}
if (error == 3) {
g.drawLine(10, 270, 500, 270);
g.drawLine(200, 30, 200, 270);
g.drawLine(200, 30, 350, 30);
}
if (error > 3) {
Font font = new Font("Verdana", Font.BOLD, 25);
g.setFont(font);
g.drawString("GAME OVER", 270, 170);
}
}
}
I have run into a problem with what I thought was a pretty simple program. I simply want to use a GUI, click and button to display data in a text file. I seem to be close but am running into a problem I do not understand. If I leave the code the way it is here I get the error that highScores is not declared (symbol not found) for line 72. But if I try to declare highSchores the I get the error "unreported exception java.io.IOException; must be caught or declared to be thrown" for line 69. Any idea what I am doing wrong and how I can fix it?
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
public class tetrisScores extends JFrame{
private JPanel contentPanel;
private JButton btnSearch;
private JButton btnLoad;
private JButton btnSort;
private JRadioButton firstSort;
private JRadioButton secondSort;
private JTextField searchInput;
private JTextArea output;
private String[] highScores;
private void add (Container con, Component widget, int left, int top, int width, int height) //creates variables for bounds
{
widget.setBounds(left, top, width, height); //sets setBounds to created variables
con.add(widget); //tells program container to use widget's bounds
}
tetrisScores()
{
contentPanel=(JPanel)getContentPane();
contentPanel.setLayout(null);
btnLoad = new JButton("Load File");
add(contentPanel, btnLoad, 10, 10, 360, 40);
searchInput = new JTextField("");
add(contentPanel, searchInput, 10, 60, 240, 40);
btnSearch = new JButton("Search");
add(contentPanel, btnSearch, 260, 60, 110, 40);
firstSort = new JRadioButton("Bubble Sort");
add(contentPanel, firstSort, 20, 110, 110, 40);
firstSort = new JRadioButton("Linear Sort");
add(contentPanel, firstSort, 140, 110, 110, 40);
btnSearch = new JButton("Sort");
add(contentPanel, btnSearch, 260, 110, 110, 40);
output = new JTextArea("");
add(contentPanel, output, 10, 160, 360, 150);
output.setEditable(false);
setTitle("High Scores");
setSize(500,500);
setLocation(new Point (150,150));
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
btnLoad.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent add)
{
OpenFile();
for (int j = 0; j<10;j++)
{
output.append(highScores[j] + "/n");
}
}
});
}
public String [] OpenFile() throws IOException
{
FileReader fr = new FileReader("tetrishighscore.txt");
BufferedReader scoreReader = new BufferedReader (fr);
int numbLines = 10;
String[] textData = new String [numbLines];
int i;
for (i=0; i < numbLines; i++)
{
textData[i] = scoreReader.readLine();
}
scoreReader.close();
return textData;
}
public static void main (String [] args)
{
new tetrisScores();
}
}
The OpenFile() method throws an IOException, but it is never caught.
I have made some modifications:
highScores could be declared as a List (so you don't have to give the number of lines in advance)
the Exception is caught
the line break character is "\n", not "/n"
Some more modifications could be made, but this should work:
import java.awt.Component;
import java.awt.Container;
import java.util.List;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class TetrisScores extends JFrame {
private JPanel contentPanel;
private JButton btnSearch;
private JButton btnLoad;
private JButton btnSort;
private JRadioButton firstSort;
private JRadioButton secondSort;
private JTextField searchInput;
private JTextArea output;
private List<String> highScores = new ArrayList<>();
private void add(Container con, Component widget, int left, int top, int width, int height) // creates variables for
// bounds
{
widget.setBounds(left, top, width, height); // sets setBounds to created variables
con.add(widget); // tells program container to use widget's bounds
}
TetrisScores() {
contentPanel = (JPanel) getContentPane();
contentPanel.setLayout(null);
btnLoad = new JButton("Load File");
add(contentPanel, btnLoad, 10, 10, 360, 40);
searchInput = new JTextField("");
add(contentPanel, searchInput, 10, 60, 240, 40);
btnSearch = new JButton("Search");
add(contentPanel, btnSearch, 260, 60, 110, 40);
firstSort = new JRadioButton("Bubble Sort");
add(contentPanel, firstSort, 20, 110, 110, 40);
firstSort = new JRadioButton("Linear Sort");
add(contentPanel, firstSort, 140, 110, 110, 40);
btnSearch = new JButton("Sort");
add(contentPanel, btnSearch, 260, 110, 110, 40);
output = new JTextArea("");
add(contentPanel, output, 10, 160, 360, 150);
output.setEditable(false);
setTitle("High Scores");
setSize(500, 500);
setLocation(new Point(150, 150));
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
btnLoad.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent add) {
try {
OpenFile();
for (int j = 0; j < highScores.size(); j++) {
output.append(highScores.get(j) + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
public void OpenFile() throws IOException {
FileReader fr = new FileReader("tetrishighscore.txt");
BufferedReader scoreReader = new BufferedReader(fr);
String line;
while((line = scoreReader.readLine()) != null) {
highScores.add(line);
}
scoreReader.close();
}
public static void main(String[] args) {
new TetrisScores();
}
}
So, I have been trying to figure this out for a little bit and cannot figure out how to do it. I want one of my buttons in another class to change the text of a JLabel in the GUI class.
Here is the code from GUI class:`import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class GUI extends JFrame{
Container pane = getContentPane();
JButton guess = new JButton("Guess");
JButton gen = new JButton("Generate number");
JTextField userInput = new JTextField();
JLabel Numbers = new JLabel("Press generate to start.");
JLabel guessedNum = new JLabel("");
JLabel error = new JLabel("");
public void CreateGUI(){
final int WIDTH = 325;
final int HEIGHT = 200;
final int centerWIDTH = WIDTH / 4;
final int centerHEIGHT = HEIGHT / 4;
guessHandler guessHandle;
genHandler genHandle;
pane.setLayout(null);
guessHandle = new guessHandler();
guess.addActionListener(guessHandle);
genHandle = new genHandler();
gen.addActionListener(genHandle);
userInput.setBounds(centerWIDTH - 20, centerHEIGHT, 200, 20);
guess.setBounds(userInput.getX() - 35, (userInput.getY() + 25), 105, 50);
gen.setBounds((guess.getX() + 105), guess.getY(), 165, 50);
error.setBounds(70, 125, 300, 20);
Numbers.setBounds(90, 0, 300, 20);
guessedNum.setBounds(20, 25, 300, 20);
pane.add(userInput);
pane.add(guess);
pane.add(gen);
pane.add(Numbers);
pane.add(guessedNum);
pane.add(error);
setSize(WIDTH,HEIGHT);
setTitle("Number Guesser");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setResizable(false);
setLocation(350, 150);
}
}
And here the code from the button trying to change the JLabel "error":
`
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class guessHandler implements ActionListener{
public void actionPerformed(ActionEvent e) {
GUI gui = new GUI();
gui.changePOS(4, 50, 0, 300, 20);
gui.error.setText("HI from guessHandler.java");
}
}
First, add a getter with public access so your second class can access the field. Something like,
public JLabel getError() {
return error;
}
Or (as #MadProgrammer suggested in the comments, a mutator) like
public void setError(String txt) {
error.setText(txt);
}
Then modify your second class, and pass the instance of GUI to it in the constructor. Like,
public class guessHandler implements ActionListener{
private GUI gui;
public guessHandler(GUI gui) {
this.gui = gui;
}
public void actionPerformed(ActionEvent e) {
gui.changePOS(4, 50, 0, 300, 20);
gui.setError("HI from guessHandler.java");
}
}
I'm trying to rename file or directory using JFileChooser() and JButton(). But it gives me a "NullPointer exeption" in line 81 where I have
action listener
// button action
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String newFileName = textField.getText();
// exeption is here!!!
File oldFile = new File(fileChooser.getSelectedFile(), null);
File newFileOrDirectoryName = new File(newFileName);
if (oldFile.renameTo(newFileOrDirectoryName)) {
System.out.println("renamed");
} else {
System.out.println("Error");
}
}
});
I have to select some file or directory from my FileChooser(), then write new name for file or directory in TextField() and then push the Button() to rename it.Can you say where I go wrong and how to solve this problem.
This is a full code:
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JFileChooser;
public class App {
private JFrame frame;
private JTextField textField;
private JFileChooser fileChooser;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
App window = new App();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public App() {
initialize();
}
public void initialize() {
frame = new JFrame();
frame.getContentPane().setFont(new Font("Tahoma", Font.BOLD, 14));
frame.setBounds(100, 100, 539, 469);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.getContentPane().setBackground(new Color(255, 246, 143));
JLabel label = new JLabel("Rename file or package");
label.setFont(new Font("Tahoma", Font.BOLD, 14));
label.setHorizontalAlignment(SwingConstants.LEFT);
label.setBounds(10, 0, 302, 32);
frame.getContentPane().add(label);
JLabel label_1 = new JLabel("New file/package name");
label_1.setHorizontalAlignment(SwingConstants.LEFT);
label_1.setFont(new Font("Tahoma", Font.BOLD, 14));
label_1.setBounds(10, 358, 194, 25);
frame.getContentPane().add(label_1);
textField = new JTextField();
textField.setBounds(10, 387, 179, 32);
frame.getContentPane().add(textField);
textField.setColumns(10);
JButton button = new JButton("Rename");
button.setFont(new Font("Tahoma", Font.BOLD, 14));
button.setBounds(199, 384, 151, 34);
button.addActionListener(null);
frame.getContentPane().add(button);
fileChooser = new JFileChooser();
fileChooser.setBounds(10, 51, 505, 289);
frame.getContentPane().add(fileChooser);
// button action
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String newFileName = textField.getText();
File oldFile = new File(fileChooser.getSelectedFile(), null);
File newFileOrDirectoryName = new File(newFileName);
if (oldFile.renameTo(newFileOrDirectoryName)) {
System.out.println("renamed");
} else {
System.out.println("Error");
}
}
});
}
}
It's because you're passing null to the File constructor at line 81, which give a NullPointerException when passed null. This can be read in the documentation here.
I think the solution is to remove the 'new File' part and replace it by:
File oldFile = fileChooser.getSelectedFile();
The bug was actually quite easy to see by pasting the code into IntelliJ:
Okay this is probably a stupid question but I am new to GUI's and Java in general. In my GUI that I have made what is my frame called because no where do I see JFrame. Or do I have to create a JFrame and put everything I have on that. I need a JFrame to do things like minimize the screen, change the icon etc. Thanks for any help!
private ImageIcon bgi;
private JLabel bgl;
private JButton startButton;
private JButton helpButton;
private JButton backButton;
private final Action action = new SwingAction();
public static void main(String[] args) throws MalformedURLException, IOException {
TwitterUnfollower gui = new TwitterUnfollower ();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // when click x close program
gui.setSize(902, 305);
gui.setVisible(true);
gui.setTitle("Solid Cloud Inc - Twitter Unfolower");
}
public TwitterUnfollower() throws MalformedURLException, IOException{
bgi = new ImageIcon(getClass().getResource("tu.png"));
getContentPane().setLayout(null);
BufferedImage img = ImageIO.read(new URL("http://i1344.photobucket.com/albums/p656/SolidCloudInc/start_zpsf3781681.png"));
//ImageIcon start = new ImageIcon(getClass().getResource("start.png"));
startButton = new JButton("");
startButton.setIcon(new ImageIcon(img));
startButton.setBounds(22, 186, 114, 50);
getContentPane().add(startButton);
BufferedImage img2 = ImageIO.read(new URL("http://i1344.photobucket.com/albums/p656/SolidCloudInc/help_zpsc4fad867.png"));
helpButton = new JButton("");
helpButton.setIcon(new ImageIcon(img2));
helpButton.setBounds(192, 186, 114, 50);
getContentPane().add(helpButton);
BufferedImage img3 = ImageIO.read(new URL("http://i1344.photobucket.com/albums/p656/SolidCloudInc/back_zps9d62b65b.png"));
backButton = new JButton("");
backButton.setIcon(new ImageIcon(img3));
backButton.setBounds(105, 205, 82, 44);
backButton.setBorder(BorderFactory.createEmptyBorder());
backButton.setContentAreaFilled(false);
backButton.setVisible(false);
getContentPane().add(backButton);
bgl = new JLabel (bgi);
bgl.setBounds(0, 0, 886, 272);
getContentPane().add(bgl);
Events e = new Events();
startButton.addActionListener(e);
helpButton.addActionListener(e);
backButton.addActionListener(e);
}
}
I do have an action listener I deleted it from the code to make it shorter. And I know I should avoid null layouts but I am using WindowBuilder, and that will probably change. Thanks again!
Do you mean something like this:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TwitterUnfollower extends JFrame {
private ImageIcon bgi;
private JLabel bgl;
private JButton startButton;
private JButton helpButton;
private JButton backButton;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
try {
TwitterUnfollower gui = new TwitterUnfollower();
gui.setTitle("Solid Cloud Inc - Twitter Unfolower");
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // when click x close program
gui.pack();
gui.setVisible(true);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
public TwitterUnfollower() throws MalformedURLException, IOException {
bgi = new ImageIcon(ImageIO.read(new URL(
"http://content.mcfc.co.uk//~/media/Images/Home/News/Club%20news/2012/twitter%20background%20new.ashx")));
bgl = new JLabel(bgi);
bgl.setLayout(new BorderLayout());
getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
getContentPane().add(bgl);
BufferedImage img = ImageIO.read(new URL("http://i1344.photobucket.com/albums/p656/SolidCloudInc/start_zpsf3781681.png"));
// ImageIcon start = new ImageIcon(getClass().getResource("start.png"));
startButton = getButton(img);
BufferedImage img2 = ImageIO.read(new URL("http://i1344.photobucket.com/albums/p656/SolidCloudInc/help_zpsc4fad867.png"));
helpButton = getButton(img2);
BufferedImage img3 = ImageIO.read(new URL("http://i1344.photobucket.com/albums/p656/SolidCloudInc/back_zps9d62b65b.png"));
backButton = getButton(img3);
JPanel alignLeftPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
alignLeftPanel.setOpaque(false);
JPanel buttonPanel = new JPanel(new GridLayout(1, 0));
buttonPanel.setOpaque(false);
buttonPanel.add(startButton);
buttonPanel.add(helpButton);
buttonPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
alignLeftPanel.add(buttonPanel);
bgl.add(alignLeftPanel, BorderLayout.SOUTH);
}
private JButton getButton(BufferedImage img) {
JButton button = new JButton(new ImageIcon(img));
button.setContentAreaFilled(false);
button.setBorderPainted(false);
button.setFocusPainted(false);
return button;
}
}
Important: rather use a LayoutManager than using null layout which will only get you in trouble.
Caveat: photobucket returns status 503 so I can no longer test this code.