JButton and JLabel dissappears when adding custom background - java

JButton and JLabel disappears when adding custom background. I don't see any problems in my program, but maybe you guys find an solution! I think it's only a little thing I forgot, but I can't figure it out.
Here's the code:
GameWindow.java:
setContentPane(new StartImagePanel(RollrackLogo));
out.println("adding JLWelcome");
JLWelcome.setText("Welcome to Rollrack, " + namewindow.name);
add(JLWelcome);
JLWelcome.setVisible(true);
out.println("JLWelcome added");
out.println("adding JBRandom");
JBRandom.setText("Random");
add(JBRandom);
JBRandom.setVisible(true);
out.println("added JBRandom");
The background appears perfect, but not the JButton and JLabel!
Code to the StartImagePanel.java:
public class StartImagePanel extends JComponent{
private Image image;
public StartImagePanel(Image image) {
this.image = image;
}
#Override
protected void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, null);
}
}

Your button and label are added to your GameWindow frame while they should be added to its contentPane, setContentPane(new StartImagePanel(RollrackLogo)); instead. That's why they are not showing, they are added to the frame.
Make a variable of the StartImagePanel and add the button and label to it and they should show up.
StartImagePanel contentPanel = new StartImagePanel(RollrackLogo);
setContentPane(contentPanel);
...
out.println("adding JLWelcome");
JLWelcome.setText("Welcome to Rollrack, " + namewindow.name);
contentPanel.add(JLWelcome);
JLWelcome.setVisible(true);
out.println("JLWelcome added");
out.println("adding JBRandom");
JBRandom.setText("Random");
contentPanel.add(JBRandom);
JBRandom.setVisible(true);
out.println("added JBRandom");
Answer dispute
The claims in the first paragraph are plain wrong. Here is source that proves it.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class AddToCustomContentPane {
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
// the GUI as seen by the user (without frame)
JPanel gui = new JPanel(new FlowLayout());
gui.setBorder(new EmptyBorder(2, 3, 2, 3));
gui.setBackground(Color.RED);
JFrame f = new JFrame("Demo");
f.setContentPane(gui);
// Acid test. Can we add buttons direct to the frame?
f.add(new JButton("Button 1"));
f.add(new JButton("Button 2"));
// Ensures JVM closes after frame(s) closed and
// all non-daemon threads are finished
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// See http://stackoverflow.com/a/7143398/418556 for demo.
f.setLocationByPlatform(true);
// ensures the frame is the minimum size it needs to be
// in order display the components within it
f.pack();
// should be done last, to avoid flickering, moving,
// resizing artifacts.
f.setVisible(true);
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
SwingUtilities.invokeLater(r);
}
}
Edit after the custom panel code was given
Here's a snippet that works to show both button and label on a black image background, I removed that was not needed (listeners).
public static void main(String[] v) {
class StartImagePanel extends JPanel {
private Image image;
public StartImagePanel(Image image) {
this.image = image;
}
#Override
protected void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, null);
}
}
class GameWindow extends JFrame{
public GameWindow() {
BufferedImage RollrackLogo;
RollrackLogo = new BufferedImage(400,200,BufferedImage.TYPE_INT_RGB);
final JButton JBRandom = new JButton();
final JLabel JLWelcome = new JLabel();
setDefaultCloseOperation(EXIT_ON_CLOSE);
StartImagePanel panel = new StartImagePanel(RollrackLogo);
setContentPane(panel);
setExtendedState(MAXIMIZED_BOTH);
setVisible(true);
JLWelcome.setText("Welcome to Rollrack");
panel.add(JLWelcome);
JLWelcome.setVisible(true);
JBRandom.setText("Random");
panel.add(JBRandom);
JBRandom.setVisible(true);
}
}
GameWindow window = new GameWindow();
window.pack();
window.setVisible(true);
}

I rather use an instance of a JFrame, instead of extending it, as #Andrew Thompson suggested in another question.
However, if you're extending it, it might be a good practice to call super() in the constructor.
Additionally, we may need to know what is going on in your StartImagePanel.
It seems, to me, to be the problem.
Ensure both your GameWindow and StartImagePanel extend properly their superclasses (call super();).
Ensure your StartImagePanel has a proper Layout.
Add your components before you set your frame visible. This also means you won't need JLWelcome.setVisible(true);.
Ensure that your code is executed in the EDT (Event-Dispatch Thread).
Example:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
#SuppressWarnings("serial")
public class GameWindow extends JFrame{
BufferedImage rollrackLogo;
JButton jbRandom;
JLabel jlWelcome;
public GameWindow() {
super();
jbRandom = new JButton("Random");
jlWelcome = new JLabel("Welcome to Rollrack, " +
namewindow.name);
rollrackLogo = new BufferedImage(400, 200,
BufferedImage.TYPE_INT_RGB);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setContentPane(new StartImagePanel(rollrackLogo));
// Add your components.
add(jlWelcome);
add(jbRandom);
addKeyListener(new KeyListener() {
#SuppressWarnings("static-access")
#Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == e.VK_ESCAPE){
System.exit(7);
}
}
#Override
public void keyReleased(KeyEvent arg0) {}
#Override
public void keyTyped(KeyEvent arg0) {}
});
// Pack, or otherwise set fullscreen.
pack();
// Now, set frame visible.
setVisible(true);
}
}
Edit: Now that you've posted the code for your StartImagePanel, I see that you're extending JComponent. Follow my previous advice, (call super), set a Layout, and extend JPanel instead.

Related

Is it a bad idea to call pack() in JFrame.invalidate?

I've built a simple gui that adds panels based on user input. My initial problem was that when the panel was added the frame did not resize. Because it was a jpanel object that handled the user input adding a new panel to itself and therefore could not 'see' the jframe (at least I couldn't find how it could) I couldn't work out how to call repaint() or revalidate() on the parent frame from within that object. However,through trial and error I did find that this worked
JFrame jFrame = new JFrame(title){
#Override
public void invalidate(
super.invalidate();
this.pack();
}
};
But because I don't really know what goes on behind the scenes with invalidate I want to know whether this is a good idea or not (It seems kinda sketchy). Any advice would be great, thanks.
EDIT
Hope this makes the problem a bit clearer
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class TestFrame {
JFrame jframe;
NewPanel jpanel;
public TestFrame(){
jframe = new JFrame(); // without above addition frame won't resize
jpanel = new NewPanel();
jframe.add(jPanel);
jframe.pack();
jframe.setVisible(true);
}
public static void main(String [] args){
TestFrame testframe= new TestFrame();
}
}
class NewPanel extends JPanel implements ActionListener{
public NewPanel(){
JTextField textField = new JTextField (10);
textField.addActionListener(this);
this.add(textField);
}
// Adds a label when action is performed on textfield
#Overide
public void actionPerformed(ActionEvent ae){
JPanel extraPanel = new JPanel();
extraPanel.add(new JLabel("hi"));
this.add(extraPanel);
this.revalidate(); this.repaint();
}
}
Your issue is not that revalidate() or repaint() aren't working.
The issue here is that your JFrame has been pack()ed already and thus it has a preferred size set. If you want to change its size you need to call pack() on it again. Not necessarily to call it on invalidate().
I made some changes to your code to compile (typos) and I came with this:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestFrame {
JFrame jframe;
NewPanel jpanel;
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new TestFrame());
}
public TestFrame(){
jframe = new JFrame(); // without above addition frame won't resize
jpanel = new NewPanel();
jframe.add(jpanel);
jframe.pack();
jframe.setVisible(true);
}
class NewPanel extends JPanel implements ActionListener{
public NewPanel(){
JTextField textField = new JTextField (10);
textField.addActionListener(this);
this.add(textField);
}
// Adds a label when action is performed on textfield
#Override
public void actionPerformed(ActionEvent ae){
System.out.println("WOLOLO");
JPanel extraPanel = new JPanel();
extraPanel.add(new JLabel("hi"));
this.add(extraPanel);
this.revalidate();
this.repaint();
jframe.pack();
}
}
}
Another way to solve this is to override getPreferredSize method from the JPanel:
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
And you can delete jframe.pack() in the previous code.

why doesnt setBackground immediately and consistently update background color in this simple program

I have a very simple program that ask user to click one of four panels that match its background color to that of the bigger panel(aka displaypanel), which randomly set its background color as one of the four.If the wrong panel is clicked Joptionpane comes out; displaypanels background is temporarily set to black. If clicked yes, displaypanel should re set its background color. The problem is I dont alway see the background being updated.Instead sometimes the background stays black. ...and what is more confusing is while it stays black if you drag another window over the window of this program if you see color partially being updated as moves over the display panel or just switching to a different window and refoucs then you see the complete updated background color.
so why is the setMethod called but only occasionally executed by whatever the paint method is behind the scenes? and why casting other windows or frames make it visible? does it have anything to do with the mouse click event being processed?
I appreciate any explanation for all of this,thanks guys
public class MainPanel extends JPanel{
Subpanel panel1;
Subpanel panel2;
Subpanel panel3;
Subpanel panel4;
Subpanel displaypanel;
Color[] color; //stores all the colors that display on the subpanels
public static void main(String[] args) {
JFrame window=new JFrame("This is a test");
window.setContentPane(new MainPanel());
window.setLocation(100,30);
window.setSize(600,500);
window.setVisible(true);
}
public MainPanel(){
setLayout(new FlowLayout(FlowLayout.CENTER,80,30));
panel1= new Subpanel();
panel2= new Subpanel();
panel3= new Subpanel();
panel4= new Subpanel();
//the big sub panel
displaypanel= new Subpanel();
color=new Color[4];
color[0]=Color.BLUE;
color[1]=Color.RED;
color[2]=Color.YELLOW;
color[3]=Color.GREEN;
setBackground(Color.GRAY);
displaypanel.setBackground(displayRandomColor());
panel1.setBackground(color[0]);
panel2.setBackground(color[1]);
panel3.setBackground(color[2]);
panel4.setBackground(color[3]);
displaypanel.setPreferredSize(new Dimension(400,250));
panel1.setPreferredSize(new Dimension(70,70));
panel2.setPreferredSize(new Dimension(70,70));
panel3.setPreferredSize(new Dimension(70,70));
panel4.setPreferredSize(new Dimension(70,70));
add(displaypanel);
add(panel1);
add(panel2);
add(panel3);
add(panel4);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
}
public Color displayRandomColor(){
Color i=Color.WHITE;
switch ((int)(Math.random()*4)+1){
case 1:
i= Color.YELLOW;
break;
case 2:
i= Color.BLUE;
break;
case 3:
i= Color.GREEN;
break;
case 4:
i= Color.RED;
}
return i;
}
public class Subpanel extends JPanel{
public Subpanel(){
this.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent evt){
Component source=(Component)evt.getSource();
if((source.getBackground()).equals(displaypanel.getBackground())){
//do nothing for this test..
}
else{
displaypanel.setBackground(Color.BLACK);
//ask user to reset the background color
//**the following 2 lines introduces the problem
if(JOptionPane.showOptionDialog(null,"click Yes to see a new Color","Incorrect",JOptionPane.YES_NO_OPTION,JOptionPane.PLAIN_MESSAGE,null,null,null)==JOptionPane.YES_OPTION){
displaypanel.setBackground(displayRandomColor());
}
}
}
});
} //end of constructor
public void paintComponent(Graphics g){
super.paintComponent(g);
}
}
}
Your program works reasonably well when synchronized correctly. A few notes:
Initialize objects once, as early in program execution as practical.
Use an instance of Random to get random integers; note the much simpler implementation of displayRandomColor().
private Color displayRandomColor() {
return color[r.nextInt(color.length)];
}
Swing GUI objects should be constructed and manipulated only on the event dispatch thread.
Don't use setPreferredSize() when you really mean to override getPreferredSize()], as suggested here.
Use layouts and pack() the enclosing window.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class MainPanel extends JPanel {
private static final Random r = new Random();
private final JPanel displayPanel;
private Color[] color = {Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW};
private Subpanel panel1 = new Subpanel(color[0]);
private Subpanel panel2 = new Subpanel(color[1]);
private Subpanel panel3 = new Subpanel(color[2]);
private Subpanel panel4 = new Subpanel(color[3]);
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame window = new JFrame("This is a test");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.add(new MainPanel());
window.pack();
window.setLocationRelativeTo(null);
window.setVisible(true);
}
});
}
public MainPanel() {
setLayout(new BorderLayout());
setBackground(Color.GRAY);
//the big sub panel
displayPanel = new JPanel() {
#Override
public Dimension getPreferredSize() {
return new Dimension(320, 240);
}
};
displayPanel.setBackground(displayRandomColor());
//the control panel
JPanel p = new JPanel();
p.add(panel1);
p.add(panel2);
p.add(panel3);
p.add(panel4);
add(displayPanel, BorderLayout.CENTER);
add(p, BorderLayout.SOUTH);
}
private Color displayRandomColor() {
return color[r.nextInt(color.length)];
}
public class Subpanel extends JPanel {
public Subpanel(Color color) {
this.setBackground(color);
this.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent evt) {
Component source = (Component) evt.getSource();
if ((source.getBackground()).equals(displayPanel.getBackground())) {
System.out.println(source.getBackground());
} else {
displayPanel.setBackground(Color.BLACK);
if (JOptionPane.showOptionDialog(null,
"Click Yes to see a new Color", "Incorrect",
JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE,
null, null, null) == JOptionPane.YES_OPTION) {
displayPanel.setBackground(displayRandomColor());
}
}
}
});
}
#Override
public Dimension getPreferredSize() {
return new Dimension(70, 70);
}
}
}

Why does the background image only appear after the window is resized?

I'm creating a simple window with a background image by Swing java library.
The problem is :background image appear only when you resize window.
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.LayoutManager;
import java.awt.MediaTracker;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.Toolkit;
public class StartWindow {
JFrame frame;
private JButton button;
private JButton button2;
public void CreateStartWindow() {
frame = Window.createwindow();
Container container = frame.getContentPane();
JpanelStart panel = new JpanelStart();
container.add(panel);
this.button = new JButton("Start");
this.button2 = new JButton("Classifica");
panel.add(button);
panel.add(button2);
}
public void addActionListener(ActionListener al) {
this.button.addActionListener(al);
this.button2.addActionListener(al);
}
public void chiudi() {
frame.dispose();
}
}
class JpanelStart extends JPanel {
private Image img;
private String path_img="img/sfondo.jpg";
public JpanelStart(){
img = Toolkit.getDefaultToolkit().createImage(path_img);
loadImage(img);
}
private void loadImage(Image img) {
try {
MediaTracker track = new MediaTracker(this);
track.addImage(img, 0);
track.waitForID(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
#Override
protected void paintComponent(Graphics g){
setOpaque(false);
g.drawImage(img,0, 0, null);
super.paintComponent(g);
}
}
Window
public class Window extends JFrame {
public static JFrame createwindow() {//fare singleton
JFrame frame = new JFrame("Battaglia navale");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(640, 640);
frame.setVisible(true);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(((int)dim.getWidth()-(int)frame.getWidth())/2,
((int)dim.getHeight()-(int)frame.getHeight())/2);
return frame;
}
;
}
If possible, call setVisible on the frame after you've added the components to it, otherwise you'll need to use revalidate and repaint.
Don't call super.paintComponent after you've painted something, as it's likely to clear Graphics context
Don't change the state of any component from with the paintComponent method, call setOpaque is bad from within the paint method is a bad idea, as the Graphics context has already being prepared assuming that the component was opaque
As has already being suggest, you should be passing this as the last parameter to drawImage, especially because of the way you load the image. Personally, I prefer to use ImageIO to load images, as it provides more details when the image fails to load....
In this line...
g.drawImage(img,0, 0, null);
... you are passing null as the ImageObserver, so your component is not told to repaint when the image is loaded. You ought to pass this.

In Java, every time I add more than one thing to a JPanel, the second thing is not created?

I create a JPanel and added a few simple buttons with listeners attached to them. Then I decided to add an Image to the background of my panel, so I switched my JPanel to an ImagePanel. The buttons were working on JPanel, but now that I added a bunch of code for the background image to be displayed, the buttons no longer show. I did not change of any of the button adding code so I'm very confused as to why the buttons no longer show. This also happened in my separate GameFrame class. I added 2 rectangle components to a panel, then 3 buttons. For that panel, only the buttons show, despite the rectangles working before the buttons were added. Can I only have one type of JComponent per panel or something? I really do not understand why it's doing this. Thank you for your time.
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
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 TitleFrame extends JFrame
{
private ImagePanel panel;
private JButton mage;
private JButton rogue;
private JButton warrior;
private Image image;
public TitleFrame()
{
JFrame frame = new JFrame();
frame.setSize(1024, 768);
frame.setTitle("Title Screen");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
createMageButton();
createRogueButton();
createWarriorButton();
ImagePanel panel = new ImagePanel(new ImageIcon("C:/Users/Derek Reitz/Documents/Eclipse Projects/GameApp/src/background.jpg").getImage());
panel.add(mage);
panel.add(rogue);
panel.add(warrior);
panel.paintComponent(frame.getGraphics());
frame.getContentPane().add(panel);
}
private void createRogueButton() {
rogue = new JButton("Select Rogue");
class AddButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
moveToNextFrame('r');
}
}
ActionListener listener = new AddButtonListener();
rogue.addActionListener(listener);
}
private void createWarriorButton() {
warrior = new JButton("Select Warrior");
class AddButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
moveToNextFrame('w');
}
}
ActionListener listener = new AddButtonListener();
warrior.addActionListener(listener);
}
private void createMageButton() {
mage = new JButton("Select Mage");
class AddButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
moveToNextFrame('m');
}
}
ActionListener listener = new AddButtonListener();
mage.addActionListener(listener);
}
public void moveToNextFrame(char c)
{
GameFrame game = new GameFrame(c);
}
class ImagePanel extends JPanel
{
private Image img;
public ImagePanel(Image img) {
this.img = img;
Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
}
public void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, null);
}
}
}
You need to use a LayoutManager.
You should then be able to add your ImagePanel and buttons to the contentPane and have them all layed out and visible.
Try the follwoing:
JFrame frame = new JFrame();
frame.setTitle("Title Screen");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createMageButton();
createRogueButton();
createWarriorButton();
ImagePanel panel = new ImagePanel(new ImageIcon(".../background.jpg").getImage());
panel.setLayout(new FlowLayout());
panel.add(mage);
panel.add(rogue);
panel.add(warrior);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
You set as layout null. That is a special case where absolute positions of the components are accepted. So use `setBounds(x, y, width, height). Better still use a real layout.
Another remark, you can take the image from the class path, say from out of the resulting .jar file):
URL url = getClass().getResource("/background.jpg");
... new ImageIcon(url);

Why does my JFrame stay empty, if I subclass JPanel and JFrame?

I'm trying to write custom JFrame and JPanel for my Java application. Currently, I just want to have a JPanel with a start button in the very middle of the screen. So, here's the code I have:
package gui;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
#SuppressWarnings("serial")
public class SubitizingFrame extends JFrame implements KeyListener {
public SubitizingFrame() {
super("Subitizing");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addKeyListener(this);
add(new LaunchPanel());
pack();
setVisible(true);
}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_F5)
System.out.println("F5 pressed");
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
}
and here is my panel:
package gui;
import instructions.Settings;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
#SuppressWarnings("serial")
public class LaunchPanel extends JPanel implements ActionListener {
private JButton startButton;
public LaunchPanel() {
int width = Settings.getScreenSizeX(), height = Settings.getScreenSizeY();
setPreferredSize(new Dimension(width, height));
setLayout(null);
startButton = new JButton("Start");
startButton.setLocation((width/2) - (startButton.getWidth()/2), (height/2) - (startButton.getHeight()/2));
add(startButton);
}
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
But when the application launches, I don't see anything. Just a big gray screen.
Do not use a null layout. If you simply use the default layout manager of JPanel (i.e. FlowLayout), the JButton with "automagically" be placed in the center. Also, in order to place the JFrame in the middle of the screen, invoke setLocationRelativeTo(null).
Since it's hard to tell what you mean by "screen", this example shows how you center a JButton in a JPanel in a JFrame, that is then centered on the monitor.
public final class CenterComponentsDemo {
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI(){
final JFrame frame = new JFrame("Center Components Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new ButtonPane());
frame.setSize(new Dimension(300, 100)); // Done for demo
//frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static class ButtonPane extends JPanel{
public ButtonPane(){
super();
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
setBackground(Color.PINK);
final JButton button = new JButton("Start");
button.setAlignmentX(Component.CENTER_ALIGNMENT);
add(Box.createVerticalGlue());
add(button);
add(Box.createVerticalGlue());
}
}
}
Recommendations:
Avoid using null layout as this makes your app difficult to upgrade and maintain and makes it potentially very ugly or even non-usable on boxes with different OS's or screen resolutions.
If you have your JPanel use a GridBagLayout and add a single component to it without using GridBagConstraints, it will be placed in the center of the JPanel.
You almost never have to or should extend JFrame and only infrequently need to extend JPanel. Usually it's better to enhance your GUI classes through composition rather than inheritance.
Avoid having your "view" or gui classes implement your listener interfaces. This is OK for "toy" programs, but as soon as your application gains any appreciable size or complexity, this gets hard to maintain.
If you don't use any LayoutManager (which btw you probably should), then you'll need to set the size of the panel as well (along with its position).
Although we strongly recommend that you use layout managers, you can perform layout without them. By setting a container's layout property to null, you make the container use no layout manager. With this strategy, called absolute positioning, you must specify the size and position of every component within that container. One drawback of absolute positioning is that it does not adjust well when the top-level container is resized. It also does not adjust well to differences between users and systems, such as different font sizes and locales.
From: http://download.oracle.com/javase/tutorial/uiswing/layout/using.html
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class LaunchPanel extends JPanel {
private JButton startButton;
public LaunchPanel() {
int width = 200, height = 100;
setPreferredSize(new Dimension(width, height));
setLayout(new GridBagLayout());
startButton = new JButton("Start");
add(startButton);
setBorder( new LineBorder(Color.RED, 2));
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JOptionPane.showMessageDialog(null, new LaunchPanel());
}
});
}
}
addKeyListener(this);
Don't use KeyListeners. Swing was designed to be used with Key Bindings. Read the section from the Swing tutorial on How to Use Key Bindings for more information.
The tutorial also has a section on Using Layout Manager which you should read. You should not create GUI's with a null layout.

Categories

Resources