Adding Image to JWindow via JLabel not Appearing on the Screen - java

I am trying to create a splash screen for a risk game that I am developing. For some reason the splash screen doesnt appear even though I am adding it to the Jwindow, I would appreciate an expert eye that will see what I can't. Thanks in advance
/new jWindow as it is better for splash screen as there is no borders
JWindow window = new JWindow();
ImageIcon imageIcon = new ImageIcon("Images/riskimage.jpg");
JLabel label = new JLabel(imageIcon);
window.getContentPane().add(label);
window.setBounds(200, 200, 200, 100);
window.setVisible(true);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
window.setVisible(false);
// title of frame
JFrame frame = new JFrame("Risk");
//Creating a text field
JTextField textField = new JTextField(20);
frame.add(textField, BorderLayout.SOUTH);
JLabel welcome = new JLabel("");
welcome.setFont (welcome.getFont ().deriveFont (20.0f));
welcome.setText("Please Enter name for Player 1 in the text box at the bottom");
frame.add(welcome,BorderLayout.NORTH);
//action listener listens for enter key
textField.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
//checks if player Ones name is set and sets player Twos name aswell
if (!playerOneNameSet)
{
playerOneName = textField.getText();
textField.setText("");
welcome.setText("Please Enter name for Player 2 in the text box at the bottom");
playerOneNameSet = true;
}
else
{
playerTwoName = textField.getText();
textField.setText("");
//turning the nameSetUpDone to true as we are finished setting up the names
nameSetUpDone = true;
// repainting as we want to update the screen
frame.getContentPane().repaint();
welcome.setText("Player One:" + playerOneName +" Player Two:" + playerTwoName + " Awaiting player one move");
}
}
});

Related

JLabel not Moving or Appearing

I am trying to make a fade out transition my moving a JLabel with a transparent gradient so it starts fading out from the bottom up. This will be activated by a button that when pressed, will begin the transition by creating a label with the transparent gradient image and changing its position using a for loop. However, whenever I click the button, it appears to do nothing and it does not load the image nor the animation.
This is the image I am trying to load https://imgur.com/a/WhSCVna
setSize(800, 600);
setLayout(null);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Start Menu Background
ImageIcon start_menu_img = new ImageIcon("src/img/menu_background.png");
JLabel start_menu = new JLabel("", start_menu_img, JLabel.CENTER);
start_menu.setBounds(0,0,800,600);
// Start Button
start = new JButton(new ImageIcon("src/img/start.png"));
start.setBounds(240,327,320,100);
start.setBorder(BorderFactory.createEmptyBorder());
// Sets main menu label and adds it to the frame
start.addActionListener(this);
JLabel main_menu = new JLabel();
main_menu.setBounds(getBounds());
main_menu.add(start);
main_menu.add(start_menu);
add(main_menu);
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == start) {
try {
AudioInputStream select = getAudioFile("select.wav");
Clip clip = AudioSystem.getClip();
clip.open(select);
clip.start();
Thread.sleep(500);
ImageIcon transition_ImageIcon = new ImageIcon("src/img/transition.png");
JLabel transition = new JLabel();
transition.setIcon(transition_ImageIcon);
transition.setBounds(0,600,800,1800);
add(transition);
for(int i = 600; i > -1200; i--) {
transition.setBounds(0, i, 800, 1800);
Thread.sleep(1);
}
} catch (Exception e1) {}
}
}

Getting user input and then printing new text using AWT

I am trying to create a simple program where when I press a button, new text will appear but I have no idea how to do it (I imagine it is very simple).
The code I have right now is:
import java.awt.*;
public class ConsumptionGUI extends Frame
{
public ConsumptionGUI()
{
Frame fr = new Frame();
Button b1 = new Button ("Terminate Program");
Button b2 = new Button ("Start");
b1.setBounds(50,50,50,50);
b2.setBounds(50,50,50,50);
b1.addActionListener(e-> System.exit(0));
Label txt = new Label ("This is my first GUI");
//add to frame (after all buttons and text was added)
fr.add(b2);
fr.add(txt);
fr.add(b1);
fr.setSize(500,300);
fr.setTitle("Vehicles Information System");
fr.setLayout(new FlowLayout());
fr.setVisible(true);
} //end constructor
public static void main(String args[]){
ConsumptionGUI frame1= new ConsumptionGUI();
} //end main
Basically after this point I managed to create a frame with 2 buttons and some text in the middle.
I am really struggling to continue from here.
I need the program to first start by the press of a button then print some new text (something like "please enter your car's speed") and then save this information (to be used in a simple formula).
Afterwards the program needs to display the formula used and print what is the value calculated.
Can anyone please help?
Thanks
To get user input, you can implement a Dialog like in below code. You can use another similar dialog to show the formula and result as well.
import java.awt.*;
import java.awt.event.*;
public class ConsumptionGUI extends Frame
{
public ConsumptionGUI()
{
Frame fr = new Frame();
Button b1 = new Button("Terminate Program");
Button b2 = new Button("Start");
//b1.setBounds(50, 50, 50, 50); // Unnecessary
//b2.setBounds(50, 50, 50, 50); // Unnecessary
b1.addActionListener(e -> System.exit(0));
b2.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
InputDialog dialog = new InputDialog(fr);
dialog.setVisible(true);
System.out.println("User inputted speed = " + dialog.getSpeed());
}
});
Label txt = new Label("This is my first GUI");
//add to frame (after all buttons and text was added)
fr.add(b2);
fr.add(txt);
fr.add(b1);
fr.setSize(500, 300);
fr.setTitle("Vehicles Information System");
fr.setLayout(new FlowLayout());
fr.setVisible(true);
} //end constructor
public static void main(String args[])
{
ConsumptionGUI frame1 = new ConsumptionGUI();
} //end main
}
class InputDialog extends Dialog
{
private int speed;
InputDialog(Frame owner)
{
super(owner, "Input", true);
addWindowListener(new WindowAdapter()
{
#Override
public void windowClosing(WindowEvent e)
{
dispose();
}
});
TextField textField = new TextField(20);
Button okButton = new Button("OK");
okButton.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
String speedString = textField.getText();
speed = !speedString.isEmpty() ? Integer.parseInt(speedString) : 0;
dispose();
}
});
setLayout(new GridLayout(3, 1));
add(new Label("Please enter your car's speed"));
add(textField);
add(okButton);
pack();
}
int getSpeed()
{
return speed;
}
}

Add component every time actionPerformed is called

Hello guys I've been trying to do a small program which is just a window with a JButton that opens a JOptionPane on click and lets me input an entry for a vacation list. I want to add that entry as an JCheckBox to the JLabel every time the action of the JButton is performed. My problem currently is that even though my code seems so work the JCheckBox won't show up after inputting the String into the JOptionPane. It probably has to do something with actionPerformed being a void method? I'd be glad for some help and I'm sorry if that question has already occurred but I didn't find it anywhere.
Thanks in advance!
My Code:
public class Urlaub extends JFrame {
public Urlaub() {
super("Urlaub");
JFrame window = this;
window.setSize(800, 600);
window.setLocationRelativeTo(null);
window.setVisible(true);
JLabel grouped = new JLabel();
window.add(grouped);
grouped.setLayout(new FlowLayout());
JButton addThing = new JButton("Add things");
addThing.setVisible(true);
grouped.add(addThing);
addThing.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String entry = JOptionPane.showInputDialog(this, "Enter item");
JCheckBox checkItem = new JCheckBox(entry);
grouped.add(checkItem); // this is the line which should add the JCheckBox to the JLabel/Window
}
});
}
}
You need to revalidate the container after changing it's children. This forces a repaint.
You're also adding the elements to a JLabel, which is unusual. You're better off with a JPanel:
super("Urlaub");
JFrame window = this;
window.setSize(800, 600);
window.setLocationRelativeTo(null);
window.setVisible(true);
JPanel grouped = new JPanel();
window.getContentPane().add(grouped);
grouped.setLayout(new FlowLayout());
JButton addThing = new JButton("Add things");
grouped.add(addThing);
grouped.add(new JCheckBox("je"));
addThing.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String entry = JOptionPane.showInputDialog(this, "Enter item");
JCheckBox checkItem = new JCheckBox(entry);
grouped.add(checkItem); // this is the line which should add the JCheckBox to the JLabel/Window#
window.getContentPane().revalidate();
}
});

I would like to open an image when a user clicks on a button in GUI

I am trying to get an image to open within my frame once a button has been clicked, so far I have put the image in a JLabel within the button I want it to open to once clicked. But it shows up straight away. Any ideas on how to set it so that the image opens once the "scissors" button has been clicked?
btnPlaySci = new JButton ("Scissors!");
btnPlaySci.setBounds(180, 40, 110, 20);
btnPlaySci.addActionListener(new PlaySciHandler());
panel.add (btnPlaySci);
btnPlaySci.setForeground(Color.MAGENTA);
//below is my image, and above is the button I want it to open to
ImageIcon rock1 = new ImageIcon("rock.jpg");
JLabel picture = new JLabel(rock1);
picture.setBounds(60, 200, 400, 400);
panel.add(picture);
Edited code pasted in comment
class PlaySciHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
String computerRand = sps.computerChoice();
txtComputerRand.setText(computerRand);
String result = sps.play(Sps.SCISSORS);
txtResult.setText(result);
ImageIcon rock1 = new ImageIcon("rock.jpg");
JLabel picture = new JLabel(rock1);
picture.setBounds(60, 200, 400, 400); panel.add(picture);
}
}
I think you want to add the JLabel (containing image) to panel when button is clciked.
You will have to code the below class which will handle the event when user clicks button 'btnPlaySci'.
btnPlaySci.addActionListener(new PlaySciHandler(panel)); //replace your addActionListener line with this code.
import java.awt.event.*;
class PlaySciHandler implements ActionListener
{
JPanel panel;
PlaySciHandler(JPanel p)
{
panel = p;
}
public void actionPerformed(ActionEvent ae)
{
ImageIcon rock1 = new ImageIcon("rock.jpg");
JLabel picture = new JLabel(rock1);
picture.setBounds(60, 200, 400, 400);
panel.add(picture);
}
}

JTabbedPane with button getting the pane

I have a JTappedPane with a button on that I want to make close that tab.
I am doing it like so:
jTabbedPane1.addTab(title, null, panel, null);
JPanel pnl = new JPanel();
JButton close = new JButton();
try {
Image img = ImageIO.read(getClass().getResource("x.png"));
close.setIcon(new ImageIcon(img));
} catch (IOException ex) {
ex.printStackTrace();
}
close.setPreferredSize(new Dimension(10, 10));
close.setBorderPainted(false);
close.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent evt) {
//TODO CLOSE THE TAP WHEN BUTTON IS PRESSED
}
}});
JLabel lab = new JLabel(s);
pnl.setOpaque(false);
pnl.add(lab);
pnl.add(close);
jTabbedPane1.setTabComponentAt(jTabbedPane1.getTabCount() - 1, pnl);
I am trying to get the title of the tab on the tab that the button has been pressed on.
I thought i could do something like
close.getContaining() to return the tab it is on but I was wrong.
Any Ideas?
If I understand you correctly, you want to find the index of the tab which has the parent of the button as tabComponent:
public void actionPerformed(ActionEvent evt) {
JComponent source = (JComponent) evt.getSource();
Container tabComponent = source.getParent();
int tabIndex = jTabbedPane1.indexOfTabComponent(tabComponent);
jTabbedPane1.removeTabAt(tabIndex);
}
You could simply write:
jTabbedPane1.removeTabAt(jTabbedPane1.getSelectedIndex());

Categories

Resources