Reputation: 415
I want a Panel of 12 buttons to show up in my GUI. But for some reason they are not showing up. I don't know what is wrong with my code right now. I haven't put anything in my action performed method yet, but I will (right now I am just focused on getting the buttons to show). So could someone tell me what's wrong? Thanks!
Edit* This is all that is in my GUI Class, I want two canvases of elevator objects to show (nothing is showing)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class GUI extends JFrame implements ActionListener
{
private static final Dimension PREF_SIZE = new Dimension(1000, 1000);
MyCanvas leftCanvas = new MyCanvas();
MyCanvas rightCanvas = new MyCanvas();
ArrayList<JButton> buttonList = new ArrayList<JButton>();
JPanel buttonPanel, leftPanel, rightPanel;
public GUI()
{
super("Elevators");
//setSize(800,800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//setVisible(true);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(1,3));
leftPanel = new JPanel();
leftPanel.add(leftCanvas);
rightPanel = new JPanel();
rightPanel.add(rightCanvas);
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(12,1));
buttonPanel.setSize(900,900);
add(mainPanel);
for(int i=0; i<12; i++)
{
buttonList.add(new JButton(""+i));
JButton btn = buttonList.get(i);
buttonPanel.add(btn);
}
mainPanel.add(buttonPanel, BorderLayout.CENTER);
mainPanel.add(leftPanel, BorderLayout.EAST);
mainPanel.add(rightPanel, BorderLayout.WEST);
createAndShowGui();
}
@Override
public Dimension getPreferredSize() {
return PREF_SIZE;
}
private static void createAndShowGui() {
UI frame = new UI();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
//public static void main(String[] args) {
//SwingUtilities.invokeLater(new Runnable() {
// public void run() {
// createAndShowGui();
// }
//});
//}
//public void paint(Graphics g)
//{
// }
public void actionPerformed(ActionEvent e)
{
}
}
Here is my Canvas class
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JButton;
public class MyCanvas extends Canvas
{
private Elevator e;
int xPos =0;
int yPos=0;
public MyCanvas()
{
setSize(600,600);
repaint();
}
public void paint(Graphics g)
{
g.setColor(Color.BLACK);
g.fillRect(xPos,yPos,100, 100);
}
public void actionPerformed(ActionEvent e)
{
repaint();
}
public void setElevator(Elevator ev)
{
e = ev;
}
}
Upvotes: 1
Views: 6237
Reputation: 347334
You overrode the frame's paint
method...
public void paint(Graphics g)
{
}
But failed to call super.paint(g)
....
One of the responsibilities of paint
is to paint the child components...
UPDATED
Also, the call to this.setLayout(new BorderLayout())
is messing with your layout as the constraints have being discarded, meaning that the layout manager doesn't know what should go where
UPDATED Example
I was able to produce this...
With this
public class Elevator02 {
public static void main(String[] args) {
new Elevator02();
}
public Elevator02() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
UI ui = new UI();
ui.setLocationRelativeTo(null);
}
});
}
public class UI extends JFrame implements ActionListener {
ArrayList<Button> buttonList = new ArrayList();
JPanel buttonPanel;
public UI() {
super("Elevators");
setSize(200, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(1, 3));
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(12, 1));
add(mainPanel);
for (int i = 0; i < 12; i++) {
buttonList.add(new Button("" + i));
buttonPanel.add(buttonList.get(i));
buttonList.get(i).addActionListener(this);
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPanel.add(buttonPanel);
}
// public void paint(Graphics g) {
// }
public void actionPerformed(ActionEvent e) {
}
}
}
Upvotes: 4
Reputation: 285430
Recommendations:
getPreferredSize()
returning the desired Dimension.pack()
after adding all components to the top-level window (here the JFrame) and before calling setVisible(true)
.For example,
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
public class UI extends JFrame {
private static final Dimension PREF_SIZE = new Dimension(800, 800);
private ArrayList<JButton> buttonList = new ArrayList<JButton>();
private JPanel buttonPanel;
public UI() {
super("Elevators");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(12, 1));
add(mainPanel);
for (int i = 0; i < 12; i++) {
buttonList.add(new JButton("" + i));
JButton btn = buttonList.get(i);
buttonPanel.add(btn);
}
mainPanel.add(buttonPanel, BorderLayout.CENTER);
}
@Override
public Dimension getPreferredSize() {
return PREF_SIZE;
}
private static void createAndShowGui() {
UI frame = new UI();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
which displays as,
Edit 2
Consider using BorderLayout for the overall GUI, the two elevator images could be placed in the BorderLayout.LINE_START and BorderLayout.LINE_END positions. The elevator JPanels that could use null layout to allow the elivator component to change position. The central JPanel could also use BoxLayout oriented in a BoxLayout.PAGE_AXIS (vertical).
Upvotes: 2
Reputation: 185
Edit:
Before (your provided code)
this.add(mainPanel);
for(int i=0; i<12; i++)
{
buttonList.add(new Button(""+i));
buttonPanel.add(buttonList.get(i));
buttonList.get(i).addActionListener(this);
}
this.setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPanel.add(buttonPanel, BorderLayout.CENTER);
After: (set layout of the frame before adding components to it)
this.setLayout(new BorderLayout()); // move this line
this.add(mainPanel);
for(int i=0; i<12; i++)
{
buttonList.add(new Button(""+i));
buttonPanel.add(buttonList.get(i));
buttonList.get(i).addActionListener(this);
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPanel.add(buttonPanel, BorderLayout.CENTER);
Upvotes: 3