Reputation: 165
people. Recently I started working on some simple games to advance in my knowledge of swing. I needed to use JButtons for one of my projects, so I did. Here's the problem...
When I opened up the window, instead of seeing the normal screen with loaded images, the whole screen was just the last button I added to the frame. I tried using the setSize()
function but the button still was super large and filled up the screen.
Code:
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import java.awt.Image;
import java.io.*;
import java.net.URL;
import javax.imageio.*;
import java.awt.image.*;
import java.awt.Font;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class engine {
public static int chips = 5000;
public static String c = chips+"";
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
GUI();
}
});
}
private static void GUI() {
JFrame f = new JFrame("Pinnacle Baccarat");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setPreferredSize(new Dimension(768, 512));
f.add(new p());
engine e2 = new engine();
JButton bet1 = new JButton("Bet 1 chip");
JButton bet2 = new JButton("Bet 10 chips");
JButton bet3 = new JButton("Bet 100 chips");
JButton bet4 = new JButton("Bet 1k chips");
JButton bet5 = new JButton("Bet 10k chips");
JButton bet6 = new JButton("Bet 100k chips");
if(e2.chips < 1){bet1.setEnabled(false);}
if(e2.chips > 0){bet1.setEnabled(true);}
if(e2.chips < 10){bet2.setEnabled(false);}
if(e2.chips > 9){bet2.setEnabled(true);}
if(e2.chips < 100){bet3.setEnabled(false);}
if(e2.chips > 99){bet3.setEnabled(true);}
if(e2.chips < 1000){bet4.setEnabled(false);}
if(e2.chips > 999){bet4.setEnabled(true);}
if(e2.chips < 10000){bet5.setEnabled(false);}
if(e2.chips > 9999){bet5.setEnabled(true);}
if(e2.chips < 100000){bet6.setEnabled(false);}
if(e2.chips > 99999){bet6.setEnabled(true);}
bet1.setPreferredSize(new Dimension(96, 32));
bet2.setPreferredSize(new Dimension(96, 32));
bet3.setPreferredSize(new Dimension(96, 32));
bet4.setPreferredSize(new Dimension(96, 32));
bet5.setPreferredSize(new Dimension(96, 32));
bet6.setPreferredSize(new Dimension(96, 32));
f.add(bet1);
f.add(bet2);
f.add(bet3);
f.add(bet4);
f.add(bet5);
f.add(bet6);
f.pack();
f.setVisible(true);
}
}
class p extends JPanel {
public p() {
}
public void paintComponent(Graphics g) {
engine eng = new engine();
super.paintComponent(g);
BufferedImage chip = null;
BufferedImage table = null;
try {
table = ImageIO.read(new File("bg.png"));
} catch (IOException e) {
}
try {
chip = ImageIO.read(new File("chip.png"));
} catch (IOException e) {
}
g.drawImage(table, 0, 0, null);
g.drawImage(chip, 4, 446, null);
g.setColor(new Color(255, 0, 0));
g.setFont(new Font("default", Font.BOLD, 20));
g.drawString(eng.c, 36, 467);
}
}
Can anybody help?
Edit: I also need to know how to place the buttons specifically in places.
Edit 2: After using f.setLayout(null)
, the buttons work but my paint method and the pictures are broken: they don't show. Can anyone help?
Upvotes: 0
Views: 86
Reputation: 110
Each newly created JButton will replace the last created button, and this is because of the default layout(which is BorderLayout) of JFrame and the JButton will have the center position. What you need to do, is to set the layout to another layout, which will allow you to arrange the JButtons is diffrent ways.
Read about it more here ---> http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
Upvotes: 0
Reputation: 550
The default layout manager for JFrame is the BorderLayout. This only allows one component in any of the five positions. By adding your buttons with no constraints they are added to the center position with each one replacing the one before.
Check Visual Guide to Layouts for more information.
edit to add some info: Also forget about the setXxxSize() methods and trying to manually place components. Layout managers will ignore the sizes you have set and will size and place your components for you. Learn the different layout managers and use combinations of them to achieve the layout you want.
Upvotes: 1
Reputation: 1126
Its because you have not specified a layout manager add this:
f.setLayout(new FlowLayout());
Also to learn how to set the positioning of stuff read about layout managers or use setLayout(null)
for your panel and then use the setBounds()
method to set the X and Y position of the element.
Upvotes: 0