hello all
I found the sample code ,but do not understand how the author found the value of width and height
in the frame,anyone can tell me ?
frame.setSize(120, 285);
According to my count the value must be :
frame.setSize(110,250);
thank you
denny
import javax.swing.*;
import java.awt.Color;
public class Traffic_Lights{
public JPanel createContentPane (){
// We create a bottom JPanel to place everything on.
JPanel totalGUI = new JPanel();
totalGUI.setLayout(null);
// First JLabel, outputs "Red".
// Added to the 'textPanel' JPanel
JLabel redLabel = new JLabel("Red");
redLabel.setBackground(Color.black);//tak berpengaruh
redLabel.setLocation(0, 5);
redLabel.setSize(50, 40);
redLabel.setHorizontalAlignment(0);
// Second JLabel, outputs "Yellow"
JLabel yellowLabel = new JLabel("Yellow");
yellowLabel.setLocation(0, 5);
yellowLabel.setSize(50, 40);
yellowLabel.setHorizontalAlignment(0);
// Third JLabel, outputs "Green"
JLabel greenLabel = new JLabel("Green");
greenLabel.setLocation(0, 5);
greenLabel.setSize(50, 40);
greenLabel.setHorizontalAlignment(0);
// Creates a black panel to mimic the traffic lights
// and to hold the following panels.
JPanel trafficBox = new JPanel();
trafficBox.setLayout(null);
trafficBox.setLocation(10, 10);
trafficBox.setSize(90, 230);
trafficBox.setBackground(Color.black);
// The red light, with added label.
JPanel redPanel = new JPanel();
redPanel.setBackground(Color.red);
redPanel.setLayout(null);
redPanel.setLocation(20, 20);
redPanel.setSize(50, 50);
// The yellow light, with added label.
JPanel yellowPanel = new JPanel();
yellowPanel.setBackground(Color.yellow);
yellowPanel.setLayout(null);
yellowPanel.setLocation(20, 90);
yellowPanel.setSize(50, 50);
// The green light with added label.
JPanel greenPanel = new JPanel();
greenPanel.setBackground(Color.green);
greenPanel.setLayout(null);
greenPanel.setLocation(20, 160);
greenPanel.setSize(50, 50);
// The Labels are added to the coloured panels.
//redPanel.add(redLabel);
yellowPanel.add(yellowLabel);
greenPanel.add(greenLabel);
// The coloured panels are added to the black panel.
trafficBox.add(redPanel);
trafficBox.add(yellowPanel);
trafficBox.add(greenPanel);
// The black box is added to the rear JPanel.
totalGUI.add(trafficBox);
totalGUI.setBackground(Color.magenta);
// Return the JPanel to add it as the content pane.
totalGUI.setOpaque(true);
return totalGUI;
}
private static void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("[=] Triffic Lights [=]");
//Create and set up the content pane.
Traffic_Lights demo = new Traffic_Lights();
frame.setContentPane(demo.createContentPane());
// The other bits and pieces that make our program a bit more stable.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(120, 285);
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}