JButtons In A BoxLayout?
Mar 29, 2015
I try to make a JFrame with JButtons using the BoxLayout. I want the buttons to be in one row. This is my code so far:
package Menu;
import java.awt.Component;
import java.awt.Container;
import javax.swing.BoxLayout;
import javax.swing.JButton;
[code]...
However, when i run the code i see only one Button "settings" which fills the entire window. What am i doing wrong?
--- Update ---
forget it, ive made a mistake when i changed to boxlayout, of course i have to call the setLayout on the Container
View Replies
ADVERTISEMENT
Sep 24, 2014
I am trying to use a BoxLayout to make the equivalent of the vertical flow layout within a JPanel. I followed this tutorial: URL..., but was a little confused as to what the first argument in the constructor for the BoxLayout is (the tutorials that it was "the container that it manages," but I don't know what that means). All the examples I can find online just have the first argument as what ever kind of component they are trying to initialize using the BoxLayout, so I tried that but it produces the error "BoxLayout cannot be shared. "Here is my code:
//Main class
public class TestProject
{
public static void main(String[] args)
{
GUI mainWindow = new GUI();
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWindow.setSize(1000,1000);
mainWindow.setVisible(true);
[code]....
View Replies
View Related
Apr 17, 2014
I want to add a "play" and a "stop" button to stop and play a sound file. Sound file works good, but buttons aren't appearing in the window.
public class SpelaLjud extends JPanel implements ActionListener {
JButton bPlay, bStop;
AudioClip ac;
public SpelaLjud() {
bPlay = new JButton ("PLAY");
bStop = new JButton ("STOP");
add(bPlay);
add(bStop);
bPlay.addActionListener(this);
bStop.addActionListener(this);
[code]....
View Replies
View Related
May 18, 2014
Java Code:
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Screen implements ActionListener {
public JButton[][] b=new JButton[200][200];
[Code] ....
I am trying to create the A* Algorithm and I REALLY need a 2D array to handle this.
This is the error:
Java Code:
at java.awt.AWTEventMulticaster.componentShown(AWTEventMulticaster.java:162)
at java.awt.AWTEventMulticaster.componentShown(AWTEventMulticaster.java:162)
at java.awt.AWTEventMulticaster.componentShown(AWTEventMulticaster.java:162)
at java.awt.AWTEventMulticaster.componentShown(AWTEventMulticaster.java:162)
at java.awt.AWTEventMulticaster.componentShown(AWTEventMulticaster.java:162) mh_sh_highlight_all('java');
View Replies
View Related
Jan 8, 2015
I am in the process of creating a Tic Tac Toe game with a GUI using JButtons that will hold the X and O pictures. I have the picture appearing when i click it once but it jumbles up all the other buttons which is why i have to run the method resetAllButton() to set everything back. Once i click the x again it puts everything back how it is supposed to be and works normal. How can i get this to work in one click.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TicTacToeGui extends JFrame implements ActionListener {
[Code] .....
View Replies
View Related
Jun 14, 2014
I have a simple program and i want to use ActionCommand with my JButtons to print text. This is the code i have so far, why this isn't working?
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
[Code] ....
View Replies
View Related
Apr 7, 2014
I am trying to create a game in java for my final project for schhol but am having problems with my jframe. I want my 3 Jbuttons to be in the center of the jframe and be vertical but can not seem to get the 3 buttons in the center of the jframe ....
import javax.swing.*;
import java.awt.Container;
import java.awt.*;
public class SnakeObject extends JFrame
{
public SnakeObject()
{
createframe();
[Code] ....
View Replies
View Related
Mar 11, 2009
I have an application that reads through a ResultSet... while I'm reading that ResultSet, I am creating new JButtons..... Now I need to add an ActionListener to those JButtons...... but they are not visible to the actionPerformed method. So I thought I'd try to add an ActionListener as I created the buttons with the following code:
final File file = new File(rs.getString(3));
SKPictureButton spb = new SKPictureButton(url,file.getName());
spb.addActionListener (
new ActionListener() {
public void actionPerformed( ActionEvent e )
[Code] ....
However this seems a bit excessive to me..... I don't think I really want to be creating a new ActionListener class for each JButton do I ? Is there a better way to make the JButtons created in this method visible to the actionPerformed method ? I'm just gettin' back into the "Swing" of things in Java.... haven't coded it for a while.
View Replies
View Related
Mar 16, 2014
i have 2 java program the first java program is main menu of my program. the 2nd is Calculator.
#1 Program: Main Menu
import java.awt.event.ActionListener;
import java.awt.event.*;
import java.awt.*;
[Code].....
in the main menu there is a Jbuttons. is there a way to run the #2 program: Calculator when the 1st Jbutton(Calculator button) clicked ?
View Replies
View Related
Mar 8, 2014
I need to add actionlistener to my code which allow the number 1 to 9 to function which are located in the nested for loop.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class EX_7_2 extends JFrame {
public EX_7_2() {
setLayout(new BorderLayout(5, 10));
[Code] .....
View Replies
View Related
Sep 25, 2014
I am in the middle of creating a chess application, and am currently working on laying out a list of moves on the right of the actual board. Specifically, I want to have a JLabel that says "moves" on top of a JList that contains the moves, on top of two buttons, labeled "<" and ">" respectively, to which I will eventually add functionality to take back and un-take back moves. Currently I have the first two parts down, but when I add my buttons,the other components are getting messed up.
I have all of these things in a vertical BoxLayout inside of a JPanel with a fixed width of 70, and a height that varies according to the size of the window (I overrode the getPreferredSize method of the JPanel). The two buttons are within their own JPanel with a grid layout with one row and two columns. Then I basically just add the moves list label, the movesList, and the panel housing the buttons to the larger panel, in that order.
However, I am encountering two problems. One, the buttons display the text "... " instead of "<" and ">" as if there is not enough room, although I'm pretty sure three periods take up more space than one less than or more than sign. The next is that, without the buttons at the bottom, the label reading "moves" is appropriately centered (or left aligned, I can't tell which) over the movesList, and you can read its whole text. However, when I add in the buttons at the bottom, the label of the top shifts over to the right and then reads "mov..." because it has run out of room.
Here is my code:
//move list
movesListLabel = new JLabel("Moves");
//the components for the actual movesList come from another class with an object called pen, but I don't think this is the problem
listModel = pen.getListModel();
JList movesList = pen.getMovesList();
JScrollPane listScroller = pen.getMovesListScrollPane();
[Code] .....
View Replies
View Related
Apr 22, 2014
In my code 0,1,2 work fine an present my prompts an use my listener correctly but for some reason 3-6 is not an I don't know why? Here's my attached code.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Guess extends JFrame
[Code] ....
I meant 3-5 not 6
View Replies
View Related
Dec 12, 2014
I am designing/coding a blackjack GUI program and I'm trying to add function to my JButtons. Problem is, I now have 2 NullPointerExceptions.
public class GameConsole {
Player user;
Player dealer;
Deck playingDeck;
ValidateInput validate = new ValidateInput();
[Code] .....
I am trying to do. I am getting the following exception/stack trace at line 14 in userTurn()
Exception in thread "main" java.lang.NullPointerException
at blackjackControls.GameConsole.userTurn(GameConsole.java:163)
at blackjackControls.GameConsole.gamePlay(GameConsole.java:87)
at blackjackControls.Main.main(Main.java:7)
and then the program continues and after I click the button I get :
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at blackjackControls.GameConsole.userTurn(GameConsole.java:163)
at blackjackControls.ResultPanel$1.actionPerformed(ResultPanel.java:34)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
//and then about 30 more "at" lines
Why am I getting these exceptions and how do I fix it?
View Replies
View Related
Jun 2, 2013
I am trying to create a code which allows the users to create JButtons to a an existing frame.So what I want is that users can create buttons wihch opens a URL. The user need to be able to click on an Existing JButton called "Add Favorite", insert the name of the favorite and URL and the button is added with the functionality to open the URL in Internet Explorer.
I already have a beginning but have no clue how to add that functionaly to create JButton by pressing on button "Add Favorites" with the needed functionality The functionality needes to be added to Button4 ("add Favorite" ). As you can see, Button4 ("add Favorite") opens two inputShowDialog which allows the users to insert the name of the favorite and URL.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JButton.*;
import java.awt.Dimension.*;
import java.lang.RuntimeException.*;
import javax.swing.JOptionPane;
public class Favorites extends JFrame implements ActionListener{
//private JPanel panel1 = new Jpanel ("Add Favorite");
[Code]...
View Replies
View Related
Jun 21, 2014
Am trying to dynamically insert buttons (which will be presenting card in a frame) using grid layout.As shown image is getting inserted but its not fit in button.I tried Darryl's Stretch icon as well but of no support.
panel_playerCards.setLayout(new GridLayout(2,10, 0, 0));
for(int i=0;i<9;i++){
StretchIcon icon=new StretchIcon(UnoGui.class.getResource("/UnoColors/green/card10.png"));
btnArr[i].setIcon(icon);
btnArr[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
View Replies
View Related
Mar 30, 2015
I'm having issues with JButtons and painting methods, whenever I click a grid on the board, the buttons in the JPanel disappear. Here is the board class:
package GUI;
public class Board extends JPanel implements Observer {
/**
*
*/
// private static final long serialVersionUID = 1L;
private final int boardSizeX;
private final int boardSizeY;
private final int L = 20;
[Code] ....
I know the code is messy in places, this will be fixed later, just need some pointers on how to solve the JButton issue.
View Replies
View Related
May 9, 2015
I have to implement a face that smiles and frowns. I figured the easiest way to do this was to use JButtons. I also can't close the applet without hitting "end program". I was given a bit of code and had to add onto it. Here's what I have so far.
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.*;
public class AnimationFace extends JFrame {
public static final int WINDOW_WIDTH = 400;
public static final int WINDOW_HEIGHT = 400;
[Code] .....
It's 90% done but once I changed the face from smile to frown, it lost an eyeball.
View Replies
View Related
May 30, 2014
I am trying to program the A star algorithm using JButtons. Now I have some tweaking to do but I have to get past the errors first.
I am getting the following errors:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at AStar.findEndButton(AStar.java:40)
at Screen$1.actionPerformed(Screen.java:59)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
[Code] ....
In my AStar class you will see these buttons which is an instance of matrixButtons
MatrixButtons[][] current;
MatrixButtons[][] endButton;
MatrixButtons[][] startButton;
MatrixButtons[][] nextCurrent;
MatrixButtons[][] temp;
MatrixButtons bestNextMove;
I really need them to handle several different methods. A full example of the full program is below.
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Screen
[Code] ....
View Replies
View Related
Apr 5, 2014
I am trying to detect the collision between a moving Jbutton and stationary Jbuttons using Rectangle Intersection.
When I run the program I am getting a NullPointerException on line 'msg1.setBounds(new Rectangle(320,50,400,50));'.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class gamePanel01 extends JPanel implements KeyListener {
character ch1 = new character("Hero");
//Global Variables
JButton blocker01;
JButton blocker02;
[Code]...
View Replies
View Related