I write a program that calculates the resistance of the resistors connected parallel to
Now I have it and it displays two buttons in the center
package sample;
Import javax.swing. *;
Public Class Main {
Public Static Void Main (String [] Args) {
SwingUtilities.invokelater (new Runnable () {
@Override
Public void Run () {
Mainwindow Mainwindow = New Mainwindow ();
}
});
}
}
Package Sample;
Import javax.swing. *;
Import java.awt. *;
Import java.awt.event.ActionEvent;
Public Class Mainwindow EXTENDS JFrame {
Public Mainwindow () Throws HeadlessException {
Settitle ("Computing resistance from a heap of resistors");
setsize (600, 400);
SetLocation (300,200);
SetVisible (TRUE);
SETDEFAULTCLOSEOPERATION (exit_on_close);
SETLAYOUT (New FlowLayout ());
// Create a panel with the GRID buttons The table manager in the pnelle shows 1 line and 2 columns under the buttons
Jpanel panel1 = new jpanel (new gridlayout (1,2,5,0));
Panel1. Add (new jbutton ("OK"));
Panel1. Add (New Jbutton ("Cancel"));
// add panel
Add (Panel1);
}
}
I have two questions: how to shove boxlayout and why all the panels are created everywhere, and I just have
Add (Panel1); And should there be something like Panel.Add (Button)?
Answer 1, Authority 100%
- As you can see from discumination,
boxlayout
is used to build elements on each new row. It is perfect for the left column (text field and labels)
- panel is like a container (containing other components), which is convenient to post on the desired position. To do this, we can use three panels (can be with one, but with the larger number of elerts, you need a panel (it is easier to specify a position for one panel than several other hements)) placed in one row (for this we use
FlowLayout
).
In essence, we have such an indication of objects:
import javax.swing. *;
Import javax.swing.border.Emptyborder;
Import java.awt. *;
Public Class SO {
Public Static Void Main (String [] Args) {
JFrame Frame = New JFrame ();
frame.settitle ("Calculating resistance from a heap of resistors");
frame.setdefaultcloseoperation (jframe.exit_on_close);
frame.setsize (600, 400);
frame.getrootpane (). Setborder (New Emptyborder (15, 15, 15, 15));
frame.setvisible (TRUE);
frame.setlayout (new gridlayout ());
// Left Panel
Jpanel left = new jpanel ();
Left.Setlayout (New BoxLayout (Left, BoxLayout.page_axis));
// Central
Jpanel MID = new jpanel ();
Mid.Add (New Jbutton ("Button"));
// Right Panel
JPanel Right = new jpanel ();
right.add (new jlabel ("jlabel"));
JTEXTAREA TEXT = NEW JTEXTAREA ();
text.setmaximumsize (New Dimension (800, 50));
text.setlineWrap (TRUE);
text.setalignmentx (Component.left_alignment);
// Add labels and align in the left edge
JLABEL TEXT2 = NEW JLABEL ("TEXT2");
JLABEL TEXT3 = NEW JLABEL ("TEXT3");
JLabel Text4 = New Jlabel ("Text4");
text2.setalignmentx (Component.left_alignment);
text3.setalignmentx (component.left_alignment);
text4.setAlignmentX (Component.LEFT_ALIGNMENT);
left.add (text);
left.add (text2);
left.add (text3);
left.add (text4);
frame.add (left);
frame.add (mid);
frame.add (right);
}
}
Answer 2, authority 95%
Regarding just add – this is adding to the form.
Adding BoxLayout
BoxLayout boxLayout = new BoxLayout (panel1, BoxLayout.X_AXIS);
panel1.setLayout (boxLayout);