JButton on JFrame with Graphics [duplicate] - java

This question already has answers here:
Components in second JFrame not showing up
(2 answers)
Closed 7 years ago.
I'm trying to make a JFrame that will have graphics and 5 JButtons (which correspond with graphics on screen).
Yesterday my code ran, but now it is glitching and the graphics and buttons only appear when I am resizing the JFrame (pulling it with the cursor!). This is for a game I am making for a class project. If know what the problem may be/is, please tell me. I've been staring at this for so long and I think someone with fresh eyes or more skill than myself could see why the code is wrong. >.<
It has 2 classes, one for graphics and one with a main.
The class for the graphics:
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
import java.awt.Color;
import java.awt.Polygon;
public class drawingComponentMap extends JComponent {
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
drawingComponentMap DCM = new drawingComponentMap(); // this is used in
// the class
// below
Color door = new Color(255, 218, 185); // the graphics are making a
// house, hence the colours
Color glass = new Color(173, 216, 230);
Color mapGrass = new Color(144, 238, 144);
g.setColor(mapGrass);
g2.fillRect(0, 0, 500, 500);
// circles to correspond with buttons (as in each button reps a circle)
Color minigame = new Color(221, 160, 221);
g.setColor(minigame);
g2.fillOval(10, 20, 50, 50);
g2.fillOval(220, 70, 50, 50);
g2.fillOval(20, 200, 50, 50);
g2.fillOval(100, 300, 50, 50);
g2.fillOval(200, 200, 50, 50);
Color black = new Color(0, 0, 0);
g.setColor(black);
g2.drawOval(10, 20, 50, 50);
g2.drawOval(220, 70, 50, 50);
g2.drawOval(20, 200, 50, 50);
g2.drawOval(100, 300, 50, 50);
g2.drawOval(200, 200, 50, 50);
// the house graphic
Color walls = new Color(210, 105, 3);
g.setColor(walls);
g2.fillRect(300, 300, 150, 200);
Color roof = new Color(165, 42, 42);
g.setColor(roof);
g2.drawRect(300, 300, 150, 200);
int[] xPoints = { 300, 375, 450 };
int[] yPoints = { 300, 225, 300 };
Polygon imageTriangle = new Polygon(xPoints, yPoints, 3);
g2.fillPolygon(imageTriangle);
g.setColor(black);
g2.drawPolygon(imageTriangle);
g.setColor(glass);
g2.fillRect(380, 350, 50, 50);
g.setColor(door);
g2.fillRect(325, 450, 30, 50);
g.setColor(black);
g2.drawRect(325, 450, 30, 50);
g2.fillOval(330, 470, 5, 5);
g2.drawRect(380, 350, 50, 50);
g2.drawString("Where do you want to go?", 10, 400);
}
}
this is the class which uses the graphics, and where the jframe, jlabel, and jbuttons are made.
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Map {
JFrame window = new JFrame();
JPanel panel = new JPanel();
JButton button = new JButton();
public static void main(String[] args) {
JFrame window = new JFrame();
JPanel panel = new JPanel();
window.setSize(500, 500);
window.setTitle("Map");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
JButton button1 = new JButton("World 1");
JButton button2 = new JButton("World 2");
JButton button3 = new JButton("World 3");
JButton button4 = new JButton("World 4");
JButton button5 = new JButton("World 5");
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
panel.add(button5);
window.add(panel);
panel.setVisible(true);
drawingComponentMap DCM = new drawingComponentMap();
window.add(DCM);
}
}
Sorry about the formatting of the code/text portion of this question. I'm in class right now.

It's just a single statement issue - window.setVisible(true).
Move your window.setVisible(true) statement to the end. Your main method contents should now look like this (just for your reference):
JFrame window = new JFrame();
JPanel panel = new JPanel();
window.setSize(500, 500);
window.setTitle("Map");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button1 = new JButton("World 1");
JButton button2 = new JButton("World 2");
JButton button3 = new JButton("World 3");
JButton button4 = new JButton("World 4");
JButton button5 = new JButton("World 5");
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
panel.add(button5);
window.add(panel);
panel.setVisible(true);
drawingComponentMap DCM = new drawingComponentMap();
window.add(DCM);
window.setVisible(true);

Related

JPanel paintComponent() won't draw in JFrame

Here I have a JPanel object which handles my 2048 game, but when I try to add it to a JFrame, the paintComponent() doesn't do anything.
I know it's not the implementation of paintComponent() as I've tried drawing very simple things (shown below) and I know the JPanel is working (because it prints out "test").
import javax.swing.JFrame;
public class GameEngine
{
public static void main(String[] args)
{
JFrame frame = new JFrame("2048");
frame.setSize(700, 700);
frame.setLocation(100, 50);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new newGamePanel());
frame.setVisible(true);
}
}
/******Different File******/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class newGamePanel extends JPanel{
Color[] colorOfTiles = new Color[8];
Color gridColor = new Color(187, 173, 160);
Color backTileColor = new Color(204, 192, 180);
Color twoFourText = new Color(238, 228, 218);
Color notTwoFourText = new Color(255,255,255);
int[][] tiles;
public newGamePanel(){
System.out.println("test");
colorOfTiles[1] = new Color(238, 228, 218);colorOfTiles[2] = new
Color(236, 224, 200);
colorOfTiles[3] = new Color(242, 177, 121);colorOfTiles[4] = new
Color(246, 141, 83);
colorOfTiles[5] = new Color(245, 124, 95);colorOfTiles[6] = new
Color(233, 89, 55);
colorOfTiles[7] = new Color(241, 208, 75);
setLayout(new BorderLayout());
setPreferredSize(new Dimension(700, 700));
setFont(new Font("Arial", Font.BOLD, 48));
setFocusable(true);
JPanel subpanel = new JPanel();
subpanel.setLayout(new GridLayout(4,4));
add(subpanel, BorderLayout.CENTER);
tiles = new int[4][4];
}
public void paintComponent(Graphics g){
g.setColor(Color.black);
g.fillRect(0, 0, 100 ,100);
}
//The rest of the methods are commented out
}
I expect a black, filled rectangle to be drawn, but nothing appears on the JFrame.

Objects disappear on resize and when the window is minimized

i need some help with this code.
I already have the functions of paint the objects and clear the objects from the window but how i can make that when i make a resize or when i minimize the windows the objects doesn't disappear?
This is the code i have at the moment:
public class miClass implements ActionListener{
JFrame ventana;
JPanel panel;
JButton p,c;
Graphics g;
Image img;
Font font1,font2,font3;
public miClass(){
ventana = new JFrame("Aplicacion.");
p = new JButton("P");
c = new JButton("C");
panel = new JPanel();
ventana.setLayout(null);
ventana.setBounds(100,100,600,600);
ventana.getContentPane().add(panel);
ventana.add(p);
p.addActionListener(this);
c.addActionListener(this);
p.setBounds(20,20,120,45);
ventana.add(c);
c.setBounds(200,20,120,45);
ventana.setFocusable(true);
ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ventana.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
g = ventana.getGraphics();
Toolkit tool = Toolkit.getDefaultToolkit();
img = tool.getImage("prueba.png");
g.drawImage(img,0,100,null);
font1 = new Font("Helvetica",Font.PLAIN,22);
g.setFont(font1);
g.drawString("Hola", 100, 300);
font2 = new Font("TimesRoman",Font.BOLD,20);
g.setFont(font2);
g.drawString("Mundo", 100, 340);
font3 = new Font("Courier",Font.BOLD+Font.ITALIC,25);
g.setFont(font3);
g.drawString("WASAAAA!", 100, 400);
g.setColor(Color.green);
g.drawOval(300, 200, 150, 100);
g.setColor(Color.red);
g.drawArc(200, 400, 250, 64, 135, 46);
g.setColor(Color.blue);
g.drawLine(400, 200, 150, 100);
g.setColor(Color.magenta);
g.drawRect(300, 250, 160, 50);
g.setColor(Color.cyan);
g.fillRect(100,400,20,240);
g.setColor(Color.lightGray);
g.fillOval(100,340,14,30);
if(e.getSource() == c){ //Clean all objects on the window//
g.clearRect(0,100,900,800);
}
}
public static void main(String args[]){
miClass GUI = new miClass();
}
}
Your frame is reset to its initial state when it is resized, and your code only redraws it when a button is clicked, not when it is resized. The paintComponent method on any subclass of Component is called after the parent frame is resized, so you can fix this issue by overriding that method.
package SO;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class miClass implements ActionListener {
JFrame ventana;
JPanel panel;
JButton p, c;
Graphics g;
Image img;
Font font1, font2, font3;
public miClass() {
ventana = new JFrame("Aplicacion.");
p = new JButton("P");
c = new JButton("C");
panel = new JPanel() {
#Override
public void paintComponent(Graphics g) {
// your stuff
font1 = new Font("Helvetica", Font.PLAIN, 22);
g.setFont(font1);
g.drawString("THIS GETS REDRAWN", 100, 300);
}
};
panel.setSize(400, 400);
ventana.setLayout(null);
ventana.setBounds(100, 100, 600, 600);
ventana.getContentPane().add(panel);
ventana.add(p);
p.addActionListener(this);
c.addActionListener(this);
p.setBounds(20, 20, 120, 45);
ventana.add(c);
c.setBounds(200, 20, 120, 45);
ventana.setFocusable(true);
ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ventana.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
g = panel.getGraphics();
Toolkit tool = Toolkit.getDefaultToolkit();
img = tool.getImage("prueba.png");
g.drawImage(img, 0, 100, null);
font1 = new Font("Helvetica", Font.PLAIN, 22);
g.setFont(font1);
g.drawString("Hola", 100, 300);
font2 = new Font("TimesRoman", Font.BOLD, 20);
g.setFont(font2);
g.drawString("Mundo", 100, 340);
font3 = new Font("Courier", Font.BOLD + Font.ITALIC, 25);
g.setFont(font3);
g.drawString("WASAAAA!", 100, 400);
g.setColor(Color.green);
g.drawOval(300, 200, 150, 100);
g.setColor(Color.red);
g.drawArc(200, 400, 250, 64, 135, 46);
g.setColor(Color.blue);
g.drawLine(400, 200, 150, 100);
g.setColor(Color.magenta);
g.drawRect(300, 250, 160, 50);
g.setColor(Color.cyan);
g.fillRect(100, 400, 20, 240);
g.setColor(Color.lightGray);
g.fillOval(100, 340, 14, 30);
if (e.getSource() == c) { // Clean all objects on the window//
g.clearRect(0, 100, 900, 800);
}
}
public static void main(String args[]) {
miClass GUI = new miClass();
}
}

Java - Car Park system with GUI

Im looking a little bit of help here.
I have been given the task of creating a car park management system with a GUI interface as opposed to a console interface. The system has 15 spaces - 5 of those spaces being large can only fit 'large' vehicles - the other 10 are regular sized spaces. Whether they are large or not is determined by the parking attendant in the car park.
Upon entry to the car park, the attendant is required to fill in information about the car (reg number of vehicle, and to click a Yes or No radio button for whether or not the car is of 'high value' or not, or also if the car is a 'large vehicle')
I am currently trying to build the interface which when the program is run at first displays the car park (spaces will change from red to green when a space becomes available, and vice versa) which i have drawn using Graphics2D. (At this point i am not sure how to make this appear in a JFrame or whatever i need to use) Under the drawn car park with changing colours, i will include 3 JButtons - one to 'Add a Car', 'Remove a Car' and to 'Search for a Car.' I am unsure how to make the 'carPark' component and the afore mentioned JButtons to the window which will appear when it is run.
Consequently when i click these buttons a new window will appear displaying a form relevant to each button (add, remove and search)
What must i do to get the carpark and buttons to appear in the same window.
Here is the code i have created thus far - although it is very untidy and in pretty much one class i know whats going on and plan to split it up once i have everthing working. (I know this isnt efficient for writing code):
package carparksystem;
import javax.swing.*;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;
import java.awt.Color;
public class DrawCarPark extends javax.swing.JFrame
{
public DrawCarPark()
{
JButton addACar = new JButton("Add Car");
JButton removeACar = new JButton("Remove Car");
JButton searchCars = new JButton("Search Cars");
JFrame frame = new JFrame();
frame.add(new CarPark());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Car Park System");
frame.setSize(435, 600);
frame.setVisible(true);
frame.setResizable(false);
//frame.add(addACar);
//frame.add(removeACar);
//frame.add(searchCars);
GroupLayout mainLayout = new GroupLayout(getContentPane()); //chosen to display components in group layout
getContentPane().setLayout(mainLayout);
mainLayout.setAutoCreateGaps(true);
mainLayout.setAutoCreateContainerGaps(true);
mainLayout.setHorizontalGroup(mainLayout.createSequentialGroup()
.addGroup(mainLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(frame)
.addGroup(mainLayout.createSequentialGroup()
.addGroup(mainLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(addACar))
.addGroup(mainLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(removeACar))
.addGroup(mainLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(searchCars))))
);
mainLayout.setVerticalGroup(mainLayout.createSequentialGroup()
.addGroup(mainLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(frame)
.addGroup(mainLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(addACar)
.addComponent(removeACar)
.addComponent(searchCars)))
);
//addCarForm();
//removeCarForm();
//searchCarForm();
}
private void addCarForm()
{
/*JPanel panel = new JPanel();
//frame.add(panel);
panel.setSize(450, 650);
panel.setVisible(true);
panel.getSize();*/
JLabel regNumLabel = new JLabel("Registration Number:");
JLabel highValLabel = new JLabel("High Value?");
JLabel largeLabel = new JLabel("Large Vehicle?");
JRadioButton btnYesHighVal = new JRadioButton("Yes", false);
JRadioButton btnNoHighVal = new JRadioButton("No", true);
JRadioButton btnYesLarge = new JRadioButton("Yes", false);
JRadioButton btnNoLarge = new JRadioButton("No", true);
ButtonGroup highVal = new ButtonGroup(); //allows just one radio button from the group to be selected
highVal.add(btnYesHighVal);
highVal.add(btnNoHighVal);
ButtonGroup largeCar = new ButtonGroup(); //allows just one radio button from the group to be selected
largeCar.add(btnYesLarge);
largeCar.add(btnNoLarge);
JTextField regNumField = new JTextField();
JButton addCar = new JButton(" Add ");
JButton addCancel = new JButton("Cancel");
GroupLayout addLayout = new GroupLayout(getContentPane()); //chosen to display components in group layout
getContentPane().setLayout(addLayout);
addLayout.setAutoCreateGaps(true);
addLayout.setAutoCreateContainerGaps(true);
addLayout.setHorizontalGroup(addLayout.createSequentialGroup()
.addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(regNumLabel)
.addComponent(highValLabel)
.addComponent(largeLabel))
.addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(regNumField)
.addGroup(addLayout.createSequentialGroup()
.addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(btnYesHighVal)
.addComponent(btnYesLarge))
.addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(btnNoHighVal)
.addComponent(btnNoLarge))))
.addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(addCar)
.addComponent(addCancel))
);
addLayout.setVerticalGroup(addLayout.createSequentialGroup()
.addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(regNumLabel)
.addComponent(regNumField)
.addComponent(addCar))
.addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(addLayout.createSequentialGroup()
.addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(highValLabel)
.addComponent(btnYesHighVal)
.addComponent(btnNoHighVal))
.addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(largeLabel)
.addComponent(btnYesLarge)
.addComponent(btnNoLarge)))
.addComponent(addCancel))
);
setSize(375, 150);
setTitle("Add Car");
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
private void removeCarForm()
{
JLabel regNumLabel = new JLabel("Registration Number:");
JTextField regNumField = new JTextField();
JButton removeCar = new JButton("Remove");
JButton removeCancel = new JButton("Cancel");
GroupLayout removeLayout = new GroupLayout(getContentPane());
getContentPane().setLayout(removeLayout);
removeLayout.setAutoCreateGaps(true);
removeLayout.setAutoCreateContainerGaps(true);
removeLayout.setHorizontalGroup(removeLayout.createSequentialGroup()
.addGroup(removeLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(regNumLabel))
.addGroup(removeLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(regNumField))
.addGroup(removeLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(removeCar)
.addComponent(removeCancel))
);
removeLayout.setVerticalGroup(removeLayout.createSequentialGroup()
.addGroup(removeLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(regNumLabel)
.addComponent(regNumField)
.addComponent(removeCar))
.addGroup(removeLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(removeCancel))
);
setSize(375, 150);
setTitle("Search Car");
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
private void searchCarForm()
{
JLabel regNumLabel = new JLabel("Registration Number:");
JTextField regNumField = new JTextField();
JButton searchCar = new JButton("Search");
JButton searchCancel = new JButton("Cancel");
GroupLayout searchLayout = new GroupLayout(getContentPane());
getContentPane().setLayout(searchLayout);
searchLayout.setAutoCreateGaps(true);
searchLayout.setAutoCreateContainerGaps(true);
searchLayout.setHorizontalGroup(searchLayout.createSequentialGroup()
.addGroup(searchLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(regNumLabel))
.addGroup(searchLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(regNumField))
.addGroup(searchLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(searchCar)
.addComponent(searchCancel))
);
searchLayout.setVerticalGroup(searchLayout.createSequentialGroup()
.addGroup(searchLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(regNumLabel)
.addComponent(regNumField)
.addComponent(searchCar))
.addGroup(searchLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(searchCancel))
);
setSize(375, 150);
setTitle("Remove Car");
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[]args)
{
DrawCarPark carPark = new DrawCarPark();
carPark.setVisible(true);
}
}
Also here is my main menu class with the carPark i have drawn:
package carparksystem;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JButton;
public class CarPark extends javax.swing.JPanel
{
private void draw(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
//Draw carpark boundary
g2.drawLine(20, 20, 400, 20);
g2.drawLine(20, 450, 20, 20);
g2.drawLine(20, 450, 400, 450);
g2.drawLine(400, 20, 400, 140);
g2.drawLine(400, 330, 400, 450);
g2.drawString("ENTER", 370, 180); //labels
g2.drawString("EXIT", 375, 300);
g2.setColor(Color.WHITE); //attendants box
g2.fill3DRect(330, 200, 70, 70, true);
g2.setColor(Color.BLACK);
g2.drawString("Attendant", 338, 230);
g2.drawString("Station", 345, 245);
g2.drawRect(330, 200, 70, 70);
g2.setColor(Color.GREEN);
g2.fillRect(40, 40, 50, 100); //8 first row spaces
g2.fillRect(110, 40, 50, 100); //7
g2.fillRect(180, 40, 50, 100); //6
g2.fillRect(250, 60, 40, 80); //2
g2.fillRect(310, 60, 40, 80); //1
g2.setColor(Color.BLACK); //drawRect palces a black border around shape
g2.drawRect(40, 40, 50, 100);
g2.drawRect(110, 40, 50, 100);
g2.drawRect(180, 40, 50, 100);
g2.drawRect(250, 60, 40, 80);
g2.drawRect(310, 60, 40, 80);
g2.setColor(Color.GREEN); //second row spaces
g2.fillRect(90, 195, 40, 80); //13
g2.fillRect(150, 195, 40, 80); //12
g2.fillRect(210, 195, 40, 80); //11
g2.fillRect(270, 195, 40, 80); //3
g2.setColor(Color.BLACK);
g2.drawRect(90, 195, 40, 80);
g2.drawRect(150, 195, 40, 80);
g2.drawRect(210, 195, 40, 80);
g2.drawRect(270, 195, 40, 80);
g2.setColor(Color.GREEN); //3rd row spaces
g2.fillRect(30, 330, 40, 80); //15
g2.fillRect(90, 330, 40, 80); //14
g2.fillRect(150, 330, 50, 100); //10
g2.fillRect(220, 330, 50, 100); //9
g2.fillRect(290, 330, 40, 80); //5
g2.fillRect(350, 330, 40, 80); //4
g2.setColor(Color.BLACK);
g2.drawRect(30, 330, 40, 80);
g2.drawRect(90, 330, 40, 80);
g2.drawRect(150, 330, 50, 100);
g2.drawRect(220, 330, 50, 100);
g2.drawRect(290, 330, 40, 80);
g2.drawRect(350, 330, 40, 80);
g2.drawString("1", 328, 105); //place labels on each shape
g2.drawString("2", 268, 105);
g2.drawString("3", 288, 240);
g2.drawString("4", 368, 375);
g2.drawString("5", 308, 375);
g2.drawString("6", 203, 95);
g2.drawString("7", 133, 95);
g2.drawString("8", 63, 95);
g2.drawString("9", 242, 385);
g2.drawString("10", 168, 385);
g2.drawString("11", 225, 240);
g2.drawString("12", 165, 240);
g2.drawString("13", 105, 240);
g2.drawString("14", 103, 375);
g2.drawString("15", 43, 375);
}
#Override
public void paintComponent(Graphics g)
{
draw(g);
}
}
Thanks in advance if anyone is willing to help.
The answer to the actual question you've asked is "use a layout manager in a JFrame and/or JPanel to position components, but think you already know that. You've used a GroupLayout manager in one of your two JFrames.
I think your problem may be that you have two of them. You declare a JFrame variable in DrawCarPark, and that class also extends JFrame. You probably don't need or want both, and it may be confusing you -- if you expect things to show up in the declared JFrame, the JFrame methods you call without reference to that instance will pass the compiler since DrawCarPark extends JFrame, but they won't be doing what you want. If you want one window with all your GUI elements in it, get rid of either the variable or the extension to JFrame.
I might suggest using a different layout manager for the remaining JFrame -- its default is BorderLayout, and it seems to me that might be better for what you want. Put your buttons into a JPanel (using whatever other layout manager you want, GroupLayout or whatever), and put that panel in the NORTH section of the BorderLayout. Then put your CarPark panel into the CENTER of that BorderLayout, and the resulting JFrame will have both.
Good luck with it.

Position glCanvas alongside a JPanel containing buttons

I'd like to have the JButton panel consume ~30% of the frames horizontal space.
And glCanvas on the right side taking up the rest of the frames space.
How can I achieve this layout?
Currently:
Main.java
GLProfile profile = GLProfile.get(GLProfile.GL2);
GLCapabilities capabilities = new GLCapabilities(profile);
GLCanvas glcanvas = new GLCanvas(capabilities);
glcanvas.addGLEventListener(new GameRenderer());
glcanvas.setSize(100, 100);
JFrame frame = new JFrame("Tool");
JPanel panel = new JPanel();
JPanel cpanel=new JPanel();
panel.setLayout(null);
cpanel.setLayout(null);
JButton ButtonBR = new JButton("1");
JButton ButtonE = new JButton("2");
JButton ButtonR = new JButton("3");
JButton ButtonC = new JButton("4");
JButton ButtonT = new JButton("5");
JButton ButtonCR = new JButton("6");
ButtonBR.setBounds(10, 30, 150, 40);
ButtonE.setBounds(10, 80, 150, 40);
ButtonR.setBounds(10, 130, 150, 40);
ButtonC.setBounds(10, 180, 150, 40);
ButtonT.setBounds(10, 230, 150, 40);
ButtonCR.setBounds(10, 450, 150, 40);
cpanel.add(glcanvas);
panel.add(ButtonBR);
panel.add(ButtonE);
panel.add(ButtonR);
panel.add(ButtonC);
panel.add(ButtonT);
panel.add(ButtonCR);
frame.add(cpanel);
frame.add(panel);
frame.setSize(Toolkit.getDefaultToolkit().getScreenSize().width, Toolkit.getDefaultToolkit().getScreenSize().height);
frame.setVisible(true);
Animator animator = new Animator(glcanvas));
animator.start();
Consider using a MigLayout as such:
import javax.swing.JFrame;
import javax.swing.JLabel;
import net.miginfocom.swing.MigLayout;
import javax.swing.JTextField;
public class Main extends JFrame {
private static final long serialVersionUID = 1L;
public Main() {
getContentPane().setLayout(new MigLayout("", "[grow 30][grow 70]", "[]"));
JLabel label = new JLabel("30%");
getContentPane().add(label, "cell 0 0");
JTextField textField = new JTextField("70%");
getContentPane().add(textField, "cell 1 0,growx");
pack();
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
}

why isn't my Jlabels or Jpanels showing?

i've added a title to my Jframe, and now its blocked everything else, what have I done??
public class addressbook
{
public JFrame frame;
public JButton btnadd, btndelete, btnsave, btnprev, btnnext;
public JPanel panel, pTitle;
public JTextField txtname, txtaddress, txthomeno, txtmobno;
public JLabel JlbName , JlbHtn, JlbMtn, JlbAddress, lblTitle;
public addressbook() {
//sets window
frame = new JFrame();
frame.setTitle("Address Book");
frame.setSize(450, 580);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//sets up panel
panel = new JPanel();
panel.setLayout(null);
frame.getContentPane().add(panel);
pTitle = new JPanel();
pTitle.setLayout(new FlowLayout(FlowLayout.CENTER));
lblTitle = new JLabel("Bournemouth University Address Book");
pTitle.add(lblTitle);
frame.add(pTitle);
//Labels
JlbName = new JLabel("Name:");
JlbName.setBounds(10, 50, 100, 20);
panel.add(JlbName);
JlbHtn = new JLabel("Home Number:");
JlbHtn.setBounds(10, 90, 150, 20);
panel.add(JlbHtn);
JlbMtn = new JLabel("Mobile Number:");
JlbMtn.setBounds(10, 130, 200, 20);
panel.add(JlbMtn);
JlbAddress = new JLabel("Address:");
JlbAddress.setBounds(10, 170, 250, 20);
panel.add(JlbAddress);
//Text Fields
txtname = new JTextField("Name");
txtname.setBounds(120, 50, 200, 20);
panel.add(txtname);
txthomeno = new JTextField("Home Number");
txthomeno.setBounds(120, 90, 200, 20);
panel.add(txthomeno);
txtmobno = new JTextField("Mob Number");
txtmobno.setBounds(120, 130, 200, 20);
panel.add(txtmobno);
txtaddress = new JTextField("Address");
txtaddress.setBounds(120, 170, 250, 20);
panel.add(txtaddress);
frame.setVisible(true);
//Buttons && Button Functions
btnadd = new JButton("Add", new ImageIcon("../files/add.png"));
btnadd.setBounds(180, 350, 100, 50);
btnadd.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent event)
{
}});
panel.add(btnadd);
btndelete = new JButton("Delete", new ImageIcon("../files/delete2.png"));
btndelete.setBounds(180, 450, 100, 50);
btndelete.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent event)
{
}});
panel.add(btndelete);
btnsave = new JButton("Save", new ImageIcon("../files/save.png"));
btnsave.setBounds(180, 400, 100, 50);
btnsave.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent event)
{
}});
panel.add(btnsave);
btnprev = new JButton(new ImageIcon("../files/left.png"));
btnprev.setBounds(180, 300, 100, 50);
btnprev.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent event)
{
}});
panel.add(btnprev);
btnnext = new JButton(new ImageIcon("../files/right.png"));
btnnext.setBounds(180, 250, 100, 50);
btnnext.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent event)
{
}});
panel.add(btnnext);
frame.setVisible(true);
panel.setVisible(true);
}
If you are not using a LayoutManager on purpose (and it looks that way) make sure that you set a location and a size for your component.
pTitle.setLocation(100, 100);
pTitle.setSize(100, 100);
But you should rather remove this line
panel.setLayout(null);
and replace it with something like this:
panel.setLayout(new BorderLayout());
Also, don’t forget to add your pTitle to panel.
Add the label to the panel, don't create a new Panel for it
lblTitle = new JLabel("Bournemouth University Address Book");
lblTitle.setBounds(100, 0, 400, 20);
panel.add(lblTitle);
Try adding your JPanel pTitle to frame.getContentPane() or to the JPanel panel
Edit
Instead of
frame.add(pTitle);
do:
frame.getContentPanel().add(pTitle);
If this very quick fix doesn't help, stick with the answer you've already accepted.
You have to set a LayoutManager for your frame:
frame = new JFrame();
frame.getContentPane().setLayout(FooLayout());

Categories

Resources