JButtons To Be In The Center Of JFrame
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
ADVERTISEMENT
Feb 10, 2014
I'm using the Spring Layout Manager to put a form in a JPanel and I'm trying to center the lines horizontally so they will stay centered when the window is resized. Nothing I've tried has worked, I can't find anything on the web, and even my fallback of seeing how Netbeans does it fails as Netbeans doesn't seem to support the Spring Layout.
View Replies
View Related
Jun 17, 2014
So here's my code as it is right now:
Java Code:
public class PrintHoledRectangle
{
public static void main(String[] args)
{
int width = Integer.parseInt(args[0]) / 2 * 2 + 1;
int height = Integer.parseInt(args[1]) / 2 * 2 + 1;
int centre = width * height / 2 + 1;
int index = 0;
[Code] ....
The width of the rectangle to be printed is given by the first argument, the height by the second
The centre tile ("[_]") of the rectangle (to be printed as " ") is given by the formula: ((width*height)/2)+1, assuming you count each tile left-to-right, top-to-bottom
My thinking is that the programme keeps a running count (seen as 'index') so that, when the centre tile is to be printed, it outputs " ", otherwise "[_]", hence the if-else statement
Currently, it prints "[_]" to infinity
View Replies
View Related
May 5, 2014
Normally, the height of the CENTER component (or position) is greater than that of the NORTH and SOUTH components in a BorderLayout.
Is it possible to increase the size of the SOUTH component and decrease the size of the CENTER position because the jcomponents I want to add in the SOUTH component are bigger? or shall I have to change the layout and use some other layout?
View Replies
View Related
Dec 8, 2014
In the documentation of StackPane i read: "The stackpane will attempt to resize each child to fill its content area. If the child could not be sized to fill the stackpane (either because it was not resizable or its max size prevented it) then it will be aligned within the area using the alignment property, which defaults to Pos.CENTER."
But as I add another Pane (Anchor- or BorderPane) this added Pane is shows always in left upper corner of the StackPane. How could I force my StackPane to center another AnchorPane?
View Replies
View Related
Sep 18, 2014
You have been given the job of writing a program to assist pilots who fly the Beechcraft Starship-‐‑1 executive aircraft.As with any airplane, it is essential that the pilot know the total weight of the loaded plane at takeoff and its center of gravity. If the plane weighs too much, it won'ʹt be able to lift off. If the center of gravity is outside the limits established for the plane, it might be impossible to control. Your task is to write a program that will determine the total weight and center of gravity of this plane
Inputs: The number of crew members, number of passengers, weight of the forward baggage compartment contents, aft baggage compartment contents weight, and the amount of fuel in gallons are to be accepted as INTEGER values in the order stated. Be sure the input values are within acceptable ranges
Outputs:You must echo the input values, annotated with their appropriate names,calculate and display the total weight, and the center of gravity.
View Replies
View Related
May 6, 2014
The goal is to desplays circles (max of 30) of fixed radius = 20, with the rightmost circle drawn in blue and left most circle in red and the rest are in white. The center is determined by the location of the point where the mouse is pressed. the number of circles is equivalent to number of mouse has been pressed_ the circles will be drawn only when you push the "draw" button.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.JPanel;
public class CirclePanel extends JPanel {
private final int WIDTH = 500, HEIGHT = 300;
private final int RDIUS = 20;
private final int MAX = 30;
[code]...
View Replies
View Related
May 1, 2015
Write a program that draws 20 circles, with the radius and location of each circle determined at random...... Two circles overlap if the distance between their center points is less than the sum of their radii...
There may be many problems with the code in general but what I'm struggling with is the distance and the totalradius portion. Visually, its inaccurate.
import java.awt.*;
import javax.swing.*;
public class CircleTest extends JPanel {
Circle []circles;
Circle []circleCenter;
Circle []all;
private int distance, totalradius, dx, dy;
private int radius,x,y;
[Code] ....
View Replies
View Related
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
View Related
Jun 22, 2014
So I have this line of code...
ioexception11.addChoosableFileFilter(new Jframe());
And when I compile it gives me...
error: constructor Jframe in class Jframe cannot be applied to given types;
ioexception11.addChoosableFileFilter(new Jframe());
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
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