This. метод() и объект. setContentPane() - Java
Формулировка задачи:
Здравствуйте, имею два вопроса по Java:
1) Имеется такой вот код:
Ближе к концу присутствует такая строчка:
Так вот, вопрос в следуещем: почему взято имено в виде аргумента
, а не:
Пробовал сам тестировать - окно стает не таким(кое чего нету), как надо. Почему так и, если можно, обьяснить как оно работает?! Спасибо!
2) Что бы не спамить на форуме, спрошу тут еще одно: как работает и почему this.метод:
в коде
Если бы было через:
, то ясно, а вот с
import javax.swing.*;
import java.awt.GridLayout;
import java.awt.BorderLayout;
public class Calculator {
// Declaration of all calculator's components.
JPanel windowContent;
JTextField displayField;
JButton button0;
JButton button1;
JButton button2;
JButton button3;
JButton button4;
JButton button5;
JButton button6;
JButton button7;
JButton button8;
JButton button9;
JButton buttonPoint;
JButton buttonEqual;
JPanel p1;
// Constructor creates the components in memory
// and adds the to the frame using combination of
// Borderlayout and Gridlayout
Calculator(){
windowContent= new JPanel();
// Set the layout manager for this panel
BorderLayout bl = new BorderLayout();
windowContent.setLayout(bl);
// Create the display field and place it in the
// North area of the window
displayField = new JTextField(30);
windowContent.add("North",displayField);
// Create buttons using constructor of the
// class JButton that takes the label of the
// button as a parameter
button0=new JButton("0");
button1=new JButton("1");
button2=new JButton("2");
button3=new JButton("3");
button4=new JButton("4");
button5=new JButton("5");
button6=new JButton("6");
button7=new JButton("7");
button8=new JButton("8");
button9=new JButton("9");
buttonPoint = new JButton(".");
buttonEqual=new JButton("=");
// Create the panel with the GridLayout
// that will contain 12 buttons - 10 numeric
// ones, and buttons with the point and the
// equal sign
p1 = new JPanel();
GridLayout gl =new GridLayout(4,3);
p1.setLayout(gl);
// Add window controls to the panel p1
p1.add(button1);
p1.add(button2);
p1.add(button3);
p1.add(button4);
p1.add(button5);
p1.add(button6);
p1.add(button7);
p1.add(button8);
p1.add(button9);
p1.add(button0);
p1.add(buttonPoint);
p1.add(buttonEqual);
// Add the panel p1 to the center area
// of the window
windowContent.add("Center",p1);
//Create the frame and set its content pane
JFrame frame = new JFrame("Calculator");
frame.setContentPane(windowContent);
// set the size of the window to be big enough
// to accomodate all controls
frame.pack();
// Finally, display the window
frame.setVisible(true);
}
public static void main(String[] args) {
Calculator calc = new Calculator();
}
}frame.setContentPane(windowContent);
windowContent из windowContent = new JPanel();
p1 из p1 = new JPanel();
this.setLayout(gb);
// Set the GridBagLayout for the window’s content pane GridBagLayout gb = new GridBagLayout(); this.setLayout(gb);
JPanel blabla = new JPanel(); GridBagLayout gb = new GridBagLayout(); blabla.setLayout(gb);
this.
накладочка. Спасибо за помощь! P.S.: таки не там создал, перенесите тему по возможности в правильный раздел. Извиняюся!Решение задачи: «This. метод() и объект. setContentPane()»
textual
Листинг программы
windowContent= new JPanel(); // создание основы
//установка принципа размещения компонентов
BorderLayout bl = new BorderLayout();
windowContent.setLayout(bl);
//создание еще одной панели со своим принципом размещения
p1 = new JPanel();
GridLayout gl =new GridLayout(4,3);
p1.setLayout(gl);
//Размещение новой панели p1 с кнопочками, как один из элементов на основной панели windowContent.
windowContent.add("Center",p1);