I have a simple app, showing picture made of tiled images(named u1, u2,...,u16.jpg). Now I'd like to add some Events to it, so that I can show these images only when proper button is clicked. I've tried doing it on my own, but it's not working. Where am I doing something wrong?
Original code :
import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.border.BevelBorder;
public class Tiles_2 {
public static void main(String[] args) {
final JFrame f = new JFrame("Usmiech");
JPanel panel = new JPanel(new GridLayout(4, 4, 3, 3));
JLabel l = new JLabel();
for (int i = 1; i < 17; i++) {
String path = "u"+ i+".jpg";
l = new JLabel(new ImageIcon(path));
l.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
panel.add(l);
}
f.setContentPane(panel);
f.setSize(300, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
New code :
import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.event.*;
public class Zad_8_1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
JButton b = (JButton)(e.getSource());
String i = b.getText();
b = new JButton(new ImageIcon("u"+i+".jpg"));
}
public static void main(String[] args) {
final JFrame f = new JFrame("Smile");
JPanel panel = new JPanel(new GridLayout(4, 4, 3, 3));
JButton l = null;
for (int i = 1; i < 17; i++) {
String path = "u"+ i+".jpg";
l = new JButton(""+i);
l.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
l.setSize(53,53);
panel.add(l);
}
f.setContentPane(panel);
f.setSize(300, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
This should work like this :
this http://img535.imageshack.us/img535/3129/lab8a.jpg
Try registering the buttons to listen for the event like this:
for (int i = 1; i < 17; i++) {
String path = "u"+ i+".jpg";
l = new JButton(""+i);
l.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
l.setSize(53,53);
l.addActionListener(this);
panel.add(l);
}
You need to have constructor to add event listeners.
import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.event.*;
import java.awt.*;
public class Demo {
public static void main(String args[]) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
render("Window");
}
});
}
public static void render(String s){
JFrame f = new JFrame("Smile");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Pane pan = new Pane();
f.add(pan);
f.setSize(300,300);
f.setVisible(true);
}
}
class Pane extends JPanel implements ActionListener {
Pane(){
setLayout(new GridLayout(4, 4, 3, 3));
JButton l = null;
for (int i = 1; i < 17; i++) {
l = new JButton(""+i);
l.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
l.addActionListener(this);
l.setSize(53,53);
add(l);
}
}
public void actionPerformed(ActionEvent e) {
JButton b = (JButton)(e.getSource());
String i = b.getText();
b.setIcon(new ImageIcon("u"+i+".jpg"));
}
}
b = new JButton(new ImageIcon("u"+i+".jpg"));
Creating a new button doesn't do anything the button just sits there in memory as it hasn't been added to the GUI.
The simple solution is to just reset the icon:
b.setIcon( new ImageIcon(...) );
Instead of reassigning b a new instance of JButton, you should simply reset the icon:
b.setIcon(new ImageIcon("u"+i+".jpg"));
(take a look at pass by reference/pass by value concepts in Java, well explained here)
on top of that, you need to follow Vincent's advice and actually register with all of the buttons as an ActionListener
l.addActionListener(this);
Related
I am trying to add and actionListener to a JPanel it's self but keep getting the error"cannot find symbol". I was just wondering if it is possible to do this as I want to be able to click on the panel and make the colour change. Any help would be appreciated.
Here is what i have so far.
import java.awt.*;
import javax.swing.*;
import javax.swing.JPanel.*;
import java.awt.Color.*;
import java.awt.event.*;
/**
* Write a description of class SimpleFrame here.
*
* #author OFJ2
* #version
*/
public class Game extends JFrame
implements ActionListener
{
private final int ROWS = 5;
private final int COLUMS = 2;
private final int GAP = 2;
private final int SIZE = ROWS * COLUMS;
private int x;
private JPanel leftPanel = new JPanel(new GridLayout(ROWS,COLUMS, GAP,GAP));
private JPanel [] gridPanel = new JPanel[SIZE];
private JPanel middlePanel = new JPanel();
private JPanel rightPanel = new JPanel();
private Color col1 = Color.GREEN;
private Color col2 = Color.YELLOW;
private Color tempColor;
public Game()
{
super("Chasing Bombs OFJ2");
setSize(200,200);
setVisible(true);
makeFrame();
}
public void makeFrame()
{
Container contentPane = getContentPane();
contentPane.setLayout(new GridLayout());
leftPanel.setLayout(new GridLayout(ROWS, COLUMS));
//JLabel label2 = new JLabel("Pocahontas");
JButton playButton = new JButton("Play Game");
JButton exitButton = new JButton("Exit");
JButton easyButton = new JButton("Easy");
JButton mediumButton = new JButton("Medium");
JButton hardButton = new JButton("Hard");
add(leftPanel);
add(middlePanel, new FlowLayout());
add(rightPanel);
setGrid();
middlePanel.add(playButton);
middlePanel.add(exitButton);
rightPanel.add(easyButton);
rightPanel.add(mediumButton);
rightPanel.add(hardButton);
leftPanel.setBackground(Color.PINK);
middlePanel.setBackground(Color.RED);
easyButton.addActionListener(this);
mediumButton.addActionListener(this);
hardButton.addActionListener(this);
playButton.addActionListener(this);
exitButton.addActionListener(this);
}
public void setGrid()
{
for(int x = 0; x < SIZE; x++) {
gridPanel[x] = new JPanel();
gridPanel[x].setBorder(BorderFactory.createLineBorder(Color.BLACK));
leftPanel.add(gridPanel[x]);
gridPanel[x].setBackground(Color.GREEN);
gridPanel[x].addActionListener(this);
}
}
#Override
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if (source == gridPanel[0]){
gridPanel[x].setBackground(Color.BLACK);
}
}
}
I have tried to find if there is any other method that is needed to do this but cant find anything. Is it possible that I will have to add a button to fill each of the panels to make this work?
Thanks!
It defaults to no addActionListener, and the code below is a reference suggestion.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
class GPanel extends JPanel {
private List<ActionListener> listenerList = new ArrayList<>();
public GPanel() {
addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
var event = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "GPanel");
for (var i : listenerList) {
i.actionPerformed(event);
}
}
});
}
public void addActionListener(ActionListener listener) {
listenerList.add(listener);
}
}
public class ActionListenerTest extends JFrame {
public static void main(String[] args) {
new ActionListenerTest();
}
public ActionListenerTest() {
GPanel test = new GPanel();
test.setBackground(Color.BLUE);
add(test);
test.addActionListener(e-> {
System.out.println("Click: " + e.getActionCommand());
});
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
}
I have checked everwhere for a fix but nothing can work to make my checkbox appear. I added it to the panel and added the panel to the window. The button is appearing so it must be a problem with the checkbox. Here is my code:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainApplication {
public static Toolkit tk = Toolkit.getDefaultToolkit();
public static void main(String[] args) {
MainApplication instance = new MainApplication();
instance.start();
}
private JFrame window;
private JPanel mainPanel;
private JPanel contingencyPanel;
private JButton applyButton = new JButton("Apply Changes");
private JCheckBox autoRedLightBox = new JCheckBox("Red Light");
private JCheckBox autoYellowLightBox = new JCheckBox("Yellow Light");
private JCheckBox autoGreenLightBox = new JCheckBox("Green Light");
private JCheckBox autoBlueLightBox = new JCheckBox("Blue Light");
public void start() {
window = new JFrame("Main Control Window");
mainPanel = new JPanel();
contingencyPanel = new JPanel();
window.setSize(1280, 720);
window.setResizable(false);
window.setFocusable(true);
window.setFocusTraversalKeysEnabled(true);
int screenWidth = (int)tk.getScreenSize().getWidth();
int screenHeight = (int)tk.getScreenSize().getHeight();
window.setLocation((screenWidth/2)-(window.getWidth()/2), (screenHeight/2)-(window.getHeight()/2));
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPanel.setLayout(null);
contingencyPanel.setLayout(null);
applyButton.setToolTipText("Changes will be applied to the arduino.");
applyButton.setSize(new Dimension(120, 30));
applyButton.setLocation(new Point((1280-120)-10, (720-56)-10));
autoRedLightBox.setSelected(true);
autoRedLightBox.setLocation(new Point(30, 30));
autoRedLightBox.setMnemonic(KeyEvent.VK_R);
mainPanel.add(applyButton);
mainPanel.add(autoRedLightBox, BorderLayout.CENTER);
window.add(mainPanel);
window.setVisible(true);
}
}
Desired Outcome:
That's well suited to a GridLayout.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class ButtonsAndChecks {
private JComponent ui = null;
ButtonsAndChecks() {
initUI();
}
public void initUI() {
if (ui!=null) return;
// adjust last two numbers to need..
ui = new JPanel(new GridLayout(0,5,20,20));
ui.setBorder(new EmptyBorder(4,4,4,4));
// adjust numbers to need..
for (int i=1; i<26; i++) {
ui.add(new JButton("Button " + i));
}
// adjust numbers to need..
for (int i=1; i<26; i++) {
ui.add(new JCheckBox("Check " + i, i%2==0));
}
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
ButtonsAndChecks o = new ButtonsAndChecks();
JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
Read a bit about LayoutManagers in Swing. Every panel needs a layout to hold components, if you don't specify layout for JPanel it uses default one (FlowLayout).
The problem was in this line, when you set null for layout
mainPanel.setLayout(null);
just comment it and you will see buttons on form.
Also, for putting your form in center of screen you can call this method
window.setLocationRelativeTo(null);
I have created 2 classes that are working together to show pictures by clicking different buttons. In my EventEvent class I tried to make it so that when you press the "Picture 1" button, the variable ImageIcon xpic gets the value of ImageIcon vpic (which holds an image), after xpic has the same value as vpic my frame is supposed to somehow refresh so that xpic's new value applies and gets then shows the picture.
Why doesn't my image show up even though the button press repaints the JPanel the image is in?
Main class:
import java.awt.*;
import javax.swing.*;
public class EventMain extends JFrame{
EventEvent obje = new EventEvent(this);
// Build Buttons
JButton picB1;
JButton picB2;
JButton picB3;
JButton picB4;
JButton picB5;
//Build Panels
JPanel row0;
//Build Pictures
ImageIcon xpic;
ImageIcon vpic;
public EventMain(){
super("Buttons");
setLookAndFeel();
setSize(470, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout layout1 = new GridLayout(3,4);
setLayout(layout1);
picB1 = new JButton("Picture 1");
picB2 = new JButton("Picture 2");
picB3 = new JButton("Picture 3");
picB4 = new JButton("Picture 4");
picB5 = new JButton("Picture 5");
vpic = new ImageIcon(getClass().getResource("Images/vanessa.png"));
// Set up Row 0
row0 = new JPanel();
JLabel statement = new JLabel("Choose a picture: ", JLabel.LEFT);
JLabel picture = new JLabel(xpic);
// Set up Row 1
JPanel row1 = new JPanel();
// Set up Row 2
JPanel row2 = new JPanel();
//Listeners
picB1.addActionListener(obje);
FlowLayout grid0 = new FlowLayout (FlowLayout.CENTER);
row0.setLayout(grid0);
row0.add(statement);
row0.add(picture);
add(row0);
FlowLayout grid1 = new FlowLayout(FlowLayout.CENTER);
row1.setLayout(grid1);
row1.add(picB1);
row1.add(picB2);
add(row1);
FlowLayout grid2 = new FlowLayout(FlowLayout.CENTER);
row2.setLayout(grid2);
row2.add(picB3);
row2.add(picB4);
row2.add(picB5);
add(row2);
setVisible(true);
}
private void setLookAndFeel() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.NimbusLookAndFeel");
} catch (Exception exc) {
}
}
public static void main(String[] args) {
EventMain con = new EventMain();
}
}
Class containing events:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class EventEvent implements ActionListener {
EventMain gui;
public EventEvent(EventMain in){
gui = in;
}
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
if (command.equals("Picture 1")){
gui.xpic = gui.vpic;
gui.row0.repaint();
}
}
}
You're confusing variables with objects. Just because you change the object associated with the xpic variable, don't assume that this will change the object (the Icon) held by the JLabel. There is no magic in Java, and changing the object that a variable refers to will have no effect on the prior object.
In other words, this:
gui.xpic = gui.vpic;
gui.row0.repaint();
will have no effect on the icon that the picture JLabel is displaying
To swap icons, you must call setIcon(...) on the JLabel. Period. You will need to make the picture JLabel a field, not a local variable, and give your GUI class a public method that allows outside classes to change the state of the JLabel's icon.
Also, you should not manipulate object fields directly. Instead give your gui public methods that your event object can call.
Edit
For example:
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.*;
#SuppressWarnings("serial")
public class MyGui extends JPanel {
public static final String IMAGE_PATH = "https://duke.kenai.com/cards/.Midsize/CardFaces.png.png";
private static final int ROWS = 4;
private static final int COLS = 13;
private BufferedImage largeImg;
private List<ImageIcon> iconList = new ArrayList<>();
private JLabel pictureLabel = new JLabel();
private JButton swapPictureBtn = new JButton(new SwapPictureAction(this, "Swap Picture"));
private int iconIndex = 0;
public MyGui() throws IOException {
add(pictureLabel);
add(swapPictureBtn);
URL imgUrl = new URL(IMAGE_PATH);
largeImg = ImageIO.read(imgUrl);
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
int x = (j * largeImg.getWidth()) / COLS;
int y = (i * largeImg.getHeight()) / ROWS;
int w = largeImg.getWidth() / COLS;
int h = largeImg.getHeight() / ROWS;
iconList.add(new ImageIcon(largeImg.getSubimage(x, y, w, h)));
}
}
pictureLabel.setIcon(iconList.get(iconIndex));
}
public void swapPicture() {
iconIndex++;
iconIndex %= iconList.size();
pictureLabel.setIcon(iconList.get(iconIndex));
}
private static void createAndShowGui() {
MyGui mainPanel;
try {
mainPanel = new MyGui();
JFrame frame = new JFrame("MyGui");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
#SuppressWarnings("serial")
class SwapPictureAction extends AbstractAction {
private MyGui myGui;
public SwapPictureAction(MyGui myGui, String name) {
super(name);
this.myGui = myGui;
}
#Override
public void actionPerformed(ActionEvent e) {
myGui.swapPicture();
}
}
See the createAction() method below and how it creates an AbstractAction. See also the tutorial related to using actions with buttons. http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html
import javax.swing.*;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;
import java.awt.*;
import java.awt.event.ActionEvent;
public class EventMain {
private static final ImageIcon PICTURE_1 = new ImageIcon(EventMain.class.getResource("images/v1.png"));
private static final ImageIcon PICTURE_2 = new ImageIcon(EventMain.class.getResource("images/v2.png"));
private JFrame frame;
EventMain create() {
setLookAndFeel();
frame = createFrame();
frame.getContentPane().add(createContent());
return this;
}
void show() {
frame.setSize(470, 300);
frame.setVisible(true);
}
private JFrame createFrame() {
JFrame frame = new JFrame("Buttons");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
return frame;
}
private Component createContent() {
final JLabel picture = new JLabel();
JButton picB1 = new JButton(createAction("Picture 1", picture, PICTURE_1));
JButton picB2 = new JButton(createAction("Picture 2", picture, PICTURE_2));
JButton picB3 = new JButton(createAction("Picture 3", picture, PICTURE_1));
JButton picB4 = new JButton(createAction("Picture 4", picture, PICTURE_2));
JButton picB5 = new JButton(createAction("Picture 5", picture, PICTURE_1));
JLabel statement = new JLabel("Choose a picture: ", JLabel.LEFT);
// Create rows 1, 2, 3
JPanel panel = new JPanel(new GridLayout(3, 4));
panel.add(createRow(statement, picture));
panel.add(createRow(picB1, picB2));
panel.add(createRow(picB3, picB4, picB5));
return panel;
}
/**
* Create an action for the button. http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html
*/
private Action createAction(String label, final JLabel picture, final Icon icon) {
AbstractAction action = new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
picture.setIcon(icon);
}
};
action.putValue(Action.NAME, label);
return action;
}
private Component createRow(Component... componentsToAdd) {
JPanel row = new JPanel(new FlowLayout(FlowLayout.CENTER));
for (Component component : componentsToAdd) {
row.add(component);
}
return row;
}
private void setLookAndFeel() {
try {
UIManager.setLookAndFeel(new NimbusLookAndFeel());
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new EventMain().create().show();
}
});
}
}
Currently displays a GUI with an 8x8 grid of randomized colored buttons. The newButton is to reset the score display to 0 and reset the grid with a fresh like it does on startup after being clicked. I haven't been able to find many solutions other than that it's to do with the way Java displays it's buttons and the way layering works. Here are the 2 classes I'm using.
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JPanel;
import java.awt.Color;
public class ShinyButtonsApp extends JFrame implements ActionListener {
private static byte ROWS = 8;
public int useThis;
ShinyButtons shiny = new ShinyButtons();
public static ImageIcon[] icons = {new ImageIcon("RedButton.png"),
new ImageIcon("OrangeButton.png"),
new ImageIcon("YellowButton.png"),
new ImageIcon("GreenButton.png"),
new ImageIcon("BlueButton.png"),
new ImageIcon("LightGrayButton.png"),
new ImageIcon("DarkGrayButton.png")};
public ShinyButtonsApp(String title) {
super(title); // Set title of window
setDefaultCloseOperation(EXIT_ON_CLOSE); // allow window to close
setSize(578, 634); // Set size of window
setResizable(false);
getContentPane().setLayout(null);
JLabel aLabel = new JLabel("Score: ");
aLabel.setLocation(10, 570);
aLabel.setSize(80,30);
getContentPane().add(aLabel);
JTextField scoreField = new JTextField();
scoreField.setText(Integer.toString(shiny.score));
scoreField.setEditable(false);
scoreField.setHorizontalAlignment(JTextField.RIGHT);
scoreField.setLocation(60, 570);
scoreField.setSize(120,30);
scoreField.setBackground(Color.WHITE);
getContentPane().add(scoreField);
JButton newButton = new JButton("New Game");
newButton.addActionListener(this);
newButton.setLocation(348,570);
newButton.setSize(110,30);
getContentPane().add(newButton);
JButton quitButton = new JButton("Quit");
quitButton.setLocation(468,570);
quitButton.setSize(80,30);
getContentPane().add(quitButton);
resetButtons2();
}
public void actionPerformed(ActionEvent e) {
shiny.score = 0;
shiny.resetButtons();
resetButtons2();
}
public void resetButtons2() {
for (int r=0; r<ROWS; r++) {
for (int c=0; c<ROWS; c++) {
ImageIcon image1 = icons[(int)shiny.getButton(r,c)];
JButton button = new JButton(image1);
button.setLocation(10+(69*r),10+(69*c));
button.setSize(69,69);
button.setBorder(BorderFactory.createLineBorder(Color.GRAY,1));
getContentPane().add(button);
}
}
}
public static void main(String[] args) {
ShinyButtonsApp frame;
frame = new ShinyButtonsApp("Shiny Buttons"); // Create window
frame.setVisible(true); // Show window
}
}
and
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JPanel;
import java.awt.Color;
public class ShinyButtons extends JPanel{
public static byte RED = 0;
public static byte ORANGE = 1;
public static byte YELLOW = 2;
public static byte GREEN = 3;
public static byte BLUE = 4;
public static byte LIGHT_GRAY = 5;
public static byte DARK_GRAY = 6;
public static byte ROWS = 8;
public byte[][] buttonTable;
public int score = 0;
public ShinyButtons() {
buttonTable = new byte[ROWS][ROWS];
resetButtons();
}
public void resetButtons() {
for (int r=0; r<ROWS; r++)
for (int c=0; c<ROWS; c++)
buttonTable[r][c] = (byte)(Math.random()*7);
}
public byte getButton(int r, int c) { return buttonTable[r][c]; }
public int getScore() { return score; }
}
Building on to what this answer points out (using layout managers instead of setting size, as you should be doing) you could reset the the images just by looping through the components (JLabels) of the JPanel and changing their icons.
This particular example uses JLabels with MouseListeners but it could easily be switched to JButtons with ActionListeners
newGame.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
reset(iconPanel, icons); <--- call reset method from below
score = 0;
scoreField.setText(String.valueOf(score));
}
});
....
private void reset(JPanel panel, ImageIcon[] icons) {
Component[] comps = panel.getComponents();
Random random = new Random();
for(Component c : comps) {
if (c instanceof JLabel) {
JLabel button = (JLabel)c;
int index = random.nextInt(icons.length);
button.setIcon(icons[index]);
}
}
}
Here's the complete running code. You just need to replace the image paths.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;
public class CircleImages {
private int score = 0;
private JTextField scoreField = new JTextField(10);
public CircleImages() {
scoreField.setEditable(false);
final ImageIcon[] icons = createImageIcons();
final JPanel iconPanel = createPanel(icons, 8);
JPanel bottomLeftPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
bottomLeftPanel.add(new JLabel("Score: "));
bottomLeftPanel.add(scoreField);
JPanel bottomRightPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
JButton newGame = new JButton("New Game");
bottomRightPanel.add(newGame);
JButton quit = new JButton("Quit");
bottomRightPanel.add(quit);
JPanel bottomPanel = new JPanel(new GridLayout(1, 2));
bottomPanel.add(bottomLeftPanel);
bottomPanel.add(bottomRightPanel);
newGame.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
reset(iconPanel, icons);
score = 0;
scoreField.setText(String.valueOf(score));
}
});
JFrame frame = new JFrame();
frame.add(iconPanel);
frame.add(bottomPanel, BorderLayout.PAGE_END);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private void reset(JPanel panel, ImageIcon[] icons) {
Component[] comps = panel.getComponents();
Random random = new Random();
for(Component c : comps) {
if (c instanceof JLabel) {
JLabel button = (JLabel)c;
int index = random.nextInt(icons.length);
button.setIcon(icons[index]);
}
}
}
private JPanel createPanel(ImageIcon[] icons, int gridSize) {
Random random = new Random();
JPanel panel = new JPanel(new GridLayout(gridSize, gridSize));
for (int i = 0; i < gridSize * gridSize; i++) {
int index = random.nextInt(icons.length);
JLabel label = new JLabel(icons[index]);
label.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
score += 1;
scoreField.setText(String.valueOf(score));
}
});
label.setBorder(new LineBorder(Color.GRAY, 2));
panel.add(label);
}
return panel;
}
private ImageIcon[] createImageIcons() {
String[] files = {"blackcircle.png",
"bluecircle.png",
"greencircle.png",
"greycircle.png",
"orangecircle.png",
"redcircle.png",
"yellowcircle.png"
};
ImageIcon[] icons = new ImageIcon[files.length];
for (int i = 0; i < files.length; i++) {
icons[i] = new ImageIcon(getClass().getResource("/circles/" + files[i]));
}
return icons;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new CircleImages();
}
});
}
}
You are creating a new set of buttons in resetButtons2() and placing them on top (or rather under) of the already existing set of buttons in the same locations. You should create the set of buttons once and only update their icons upon reset.
You should do a number of things:
Use a proper layout manager, e.g., GridLayout
Create the 8x8 grid of buttons only once and replace their icons when needed
Call invalidate/repaint to refresh the content pane
And for a JFrame you don't need getContentPane().add(...), you can directly do add(...)
Two things.
First, make sure you remove the existing buttons first. Alternatively, you could simply update the state of the buttons. This would require you to create an array or List of buttons first, which your reset method would then iterate over and update their properties as required.
Second, make use of an appropriate layout manager. A null layout ias a very bad idea. You do not control the font metrics of the underlying platform and this will change how your buttons look on different systems, making your UI less dynamic then it should be
my first post after long time lurking.
I'm trying to write a java program to train my calculation skills as Khan academy removed the never-ending mode on sum, subtraction, etc.
I somehow managed to write the skeleton but I got stuck when I had to implement listeners: if I create a class that implements ActionListener everything works. But when I try to use a sublass that implements ActionListener the code breaks. I'd like to figure out why.
I have 3 classes.
Question: generates 2 random int
public class Question {
public int rand1;
public int rand2;
public Question (){
rand1 = (int) (100 +(Math.random()*900)); // to be sure I have at least 3 digits. See AnswerV2.generate()
rand2 = (int) (100 + (Math.random()*900));
}
}
Answersv2: takes the 2 random int
from Question, sums them, create 3 different answers switching
digits, adds the right answer and shuffles them.
import java.util.ArrayList;
import javax.swing.JButton;
import java.util.Collections;
public class Answersv2 {
public ArrayList Answer_list = new ArrayList();
public int int1;
public int int2;
String uno;
public Answersv2 (int a, int b) {
int1 = a;
int2 = b;
}
public void generate (){
StringBuilder due = new StringBuilder();
StringBuilder tre = new StringBuilder();
StringBuilder quattro = new StringBuilder();
uno = Integer.toString(int1+int2); // create the string version of int1+int2
ArrayList <Character> first_answer = new ArrayList<Character>(); // create an arraylist of char to store the chars
for (char c : uno.toCharArray()) {
first_answer.add(c);
}
Collections.swap(first_answer,first_answer.size()-2,first_answer.size()-1); // switch tens with units
for (char c : first_answer) {
due.append(c);
}
String dueString = due.toString();
Collections.swap(first_answer,first_answer.size()-3,first_answer.size()-2); // switchs hundres with tens
for (char c : first_answer) {
tre.append(c);
}
String treString = tre.toString();
Collections.swap(first_answer,first_answer.size()-2,first_answer.size()-1); // switch tens with units
for (char c : first_answer) {
quattro.append(c);
}
String quattroString = quattro.toString();
add(uno,dueString,treString,quattroString);
}
public void add (String one,String two,String three,String four){
Answer_list.add(one);
Answer_list.add(two);
Answer_list.add(three);
Answer_list.add(four);
shuffle();
}
public void shuffle() {
Collections.shuffle(Answer_list);
}
public void stampa (){ // command code line version to test the program, ignore this
System.out.println("--------------------------------");
System.out.println(int1 + " + " + int2 + " = : ");
System.out.println("A " + Answer_list.get(0));
System.out.println("B " + Answer_list.get(1));
System.out.println("C " + Answer_list.get(2));
System.out.println("D " + Answer_list.get(3));
}
public class CoolButton extends JButton{
public CoolButton(String answer) {
setText(answer);
}
public boolean checkme() { // method to check if the button pressed was the one with the right answer. I still haven't implemented this properly, ignore this too
if (getText() == uno) {
return true;
} else {
return false;
}
}
}
}
3 QuizV2: Creates the GUI and starts the program.
Now... I created a StartListener subclass of QuizV2 in order to make the buttons being able to read the 4 answers from the answer object created in the QuizV2's main and use it to
setText() and to change label text, etc.
Here is the code (Quizv2) with the subclass:
import java.util.ArrayList;
import java.util.Collections;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class Quizv2{
public MyLabel label = new MyLabel("Click Start");
JButton button = new JButton("Start");
Answersv2 pinolo;
Question domanda;
Answersv2.CoolButton button1;
Answersv2.CoolButton button2;
Answersv2.CoolButton button3;
Answersv2.CoolButton button4;
public static void main (String [] args) {
Quizv2 quiz = new Quizv2();
quiz.go();
}
public void go () {
Question domanda = new Question();
Answersv2 pinolo = new Answersv2(domanda.rand1,domanda.rand2);
pinolo.generate();
button1 = pinolo.new CoolButton(pinolo.Answer_list.get(0));
button1.setAlignmentX(Component.CENTER_ALIGNMENT);
button2 = pinolo.new CoolButton(pinolo.Answer_list.get(1));
button2.setAlignmentX(Component.CENTER_ALIGNMENT);
button3 = pinolo.new CoolButton(pinolo.Answer_list.get(2));
button3.setAlignmentX(Component.CENTER_ALIGNMENT);
button4 = pinolo.new CoolButton(pinolo.Answer_list.get(3));
button4.setAlignmentX(Component.CENTER_ALIGNMENT);
JFrame frame = new JFrame("SPI trainer - Sum");
JPanel panel = new JPanel();
label.setAlignmentX(Component.CENTER_ALIGNMENT);
int R = (int) (Math.random( )*256);
int G = (int)(Math.random( )*256);
int B= (int)(Math.random( )*256);
Color randomColor = new Color(R, G, B);
label.setForeground(randomColor);
panel.add(label);
button.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(button);
ActionListener doGreeting = new StartListener();
button.addActionListener(doGreeting );
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(BorderLayout.CENTER,panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(BorderLayout.CENTER,panel);
frame.setSize(300,300);
frame.setVisible(true);
frame.setLocationRelativeTo( null );
}
}
class StartListener extends Quizv2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("boo");
label.setLabelText("The button text changed.");
}
}
However it seems I'm doing something wrong as it prints 'boo' but it doesn't change the label text. I'd like to avoid to use
class StartListener extends Quizv2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (buttony == e.getSource()) {
label.setLabelText( domanda.rand1 + " + " + domanda.rand2 + " = : ????");
button1.setVisible(true);
button2.setVisible(true);
button3.setVisible(true);
button4.setVisible(true);
button.setVisible(false);
.....
.....
else if (buttonx == e.getSource())
....
}
}
in order to figure out which button was pressed so that the programs knows which block of code execute.
I then tried not to use a subclass and everything worked out. Here is the code (Quizv2)
import java.util.ArrayList;
import java.util.Collections;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class Quizv2 implements ActionListener{
public MyLabel label = new MyLabel("Click Start");
JButton button = new JButton("Start");
Answersv2 pinolo;
Question domanda;
Answersv2.CoolButton button1;
Answersv2.CoolButton button2;
Answersv2.CoolButton button3;
Answersv2.CoolButton button4;
public static void main (String [] args) {
Quizv2 quiz = new Quizv2();
quiz.go();
}
public void go () {
domanda = new Question();
pinolo = new Answersv2(domanda.rand1,domanda.rand2);
pinolo.generate();
button1 = pinolo.new CoolButton(pinolo.Answer_list.get(0));
button1.setAlignmentX(Component.CENTER_ALIGNMENT);
button2 = pinolo.new CoolButton(pinolo.Answer_list.get(1));
button2.setAlignmentX(Component.CENTER_ALIGNMENT);
button3 = pinolo.new CoolButton(pinolo.Answer_list.get(2));
button3.setAlignmentX(Component.CENTER_ALIGNMENT);
button4 = pinolo.new CoolButton(pinolo.Answer_list.get(3));
button4.setAlignmentX(Component.CENTER_ALIGNMENT);
JFrame frame = new JFrame("SPI trainer - Sum");
JPanel panel = new JPanel();
label.setAlignmentX(Component.CENTER_ALIGNMENT);
int R = (int) (Math.random( )*256);
int G = (int)(Math.random( )*256);
int B= (int)(Math.random( )*256);
Color randomColor = new Color(R, G, B);
label.setForeground(randomColor); // Little bit of color
panel.add(label);
button.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(button);
button.addActionListener(this);
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
panel.add(button1);
button1.setVisible(false);
panel.add(button2);
button2.setVisible(false);
panel.add(button3);
button3.setVisible(false);
panel.add(button4);
button4.setVisible(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(BorderLayout.CENTER,panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(BorderLayout.CENTER,panel);
frame.setSize(300,300);
frame.setVisible(true);
frame.setLocationRelativeTo( null );
}
public void actionPerformed(ActionEvent e) {
label.setLabelText( domanda.rand1 + " + " + domanda.rand2 + " = : ????");
button1.setVisible(true);
button2.setVisible(true);
button3.setVisible(true);
button4.setVisible(true);
button.setVisible(false);
}
}
1) I suggest you put this program aside for quite awhile. You are making a lot of basic errors, so I don't see how you got anything to compile. And your code is a labyrinth which is a sign that the program is way too complex for your abilities at this time.
2) Your post also shows that you need to improve your debugging skills. You really shouldn't post more than about 20 lines of code when asking a question. Reducing a problem to around 20 lines of code is an exercise that improves your debugging skills. 90% of the code you posted is irrelevant to your problem. For instance, your whole Answerv2 class could have been reduced to this:
public class Answersv2 {
public ArrayList<String> Answer_list = new ArrayList<String>();
public Answersv2 () {
Answer_list.add("300", "150", "160", "170");
}
}
Do you really think that the way your code calculated those strings is relevant to why clicking on a button fails to change the text of a label? In fact, your whole Answerv2 class is irrelevant.
The number of lines of code your program can contain is proportional to your debugging skills. You cannot write a 500 line program two days after learning java. And writing Swing programs adds a lot of moving parts, so you need to have a solid grasp of the basics before attempting Swing--like not trying to access a non-static variable inside a static method.
When you are having trouble with some code, like your inheritance problem, start a new program to experiment. Make the new program as simple as possible:
1) Write a basic Swing program that sets up the relevant swing components...
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MyGui {
protected JLabel label = new JLabel("Hello");
protected JButton button = new JButton("Click me");
public MyGui() {
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(300, 100, 500, 300);
JPanel panel = new JPanel();
panel.add(label);
panel.add(button);
Container cpane = frame.getContentPane();
cpane.add(panel);
frame.setVisible(true);
}
}
public class SwingProg {
private static void createAndShowGUI() {
new MyGui();
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
} }
2) Get an actionPerformed() method in the same class to work:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MyGui implements ActionListener { //********
protected JLabel label = new JLabel("Hello");
protected JButton button = new JButton("Click me");
public MyGui() {
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(300, 100, 500, 300);
JPanel panel = new JPanel();
panel.add(label);
button.addActionListener(this); //**********
panel.add(button);
Container cpane = frame.getContentPane();
cpane.add(panel);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) { //**************
System.out.println("boo");
label.setText("The button was clicked!");
}
}
public class SwingProg {
private static void createAndShowGUI() {
new MyGui();
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
3) Now try inherit from MyGui and put the actionPerformed method in the child class. Okay, so you can't figure out how to make it work. Now at least you have a simple example to post.
The problem with your button is: you never specified that the actionPerformed() method in the subclass should be the listener for the button. Here is a solution to your problem:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MyGui {
protected JLabel label = new JLabel("Hello");
protected JButton button = new JButton("Click me");
public MyGui() {
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(300, 100, 500, 300);
JPanel panel = new JPanel();
panel.add(label);
panel.add(button);
Container cpane = frame.getContentPane();
cpane.add(panel);
frame.setVisible(true);
}
}
class StartListener extends MyGui implements ActionListener {
public StartListener(){
super();
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
System.out.println("boo");
label.setText("The button text changed.");
}
}
public class SwingProg {
private static void createAndShowGUI() {
new StartListener();
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Don't try to solve problems in the middle of a complex program. Instead, extrapolate the problem out into a new program, and solve the problem in the new program.