Let's say I would like to create a simple calculator. It consists of 3 fields. Text field to show result, field with checkboxes to select system and field with numbers.
What kind of component should I use for each element ? How can I position elements in my window ? How can I position elements inside component (ie checkboxes) ?
This is what I'm trying to achieve.
I would use
JTextField
for the number-windowJRadioButton
for the radio buttons, andJButton
for the buttons.The layout of the components should be deferred to a so called layout-manager. (Have a look at Using Layout Managers. In this case a GridLayout
and/or a GridBagLayout
would do fine.
This code should get you started:
import java.awt.*;
import javax.swing.*;
public class FrameTest {
public static void main(String[] args) {
final JFrame f = new JFrame("Frame Test");
JPanel panel = new JPanel(new GridBagLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1.0;
panel.add(new JTextField(), gbc);
JPanel numSysPanel = new JPanel(new GridLayout(1, 3));
numSysPanel.setBorder(BorderFactory.createTitledBorder("Number System"));
numSysPanel.add(new JRadioButton("oct"));
numSysPanel.add(new JRadioButton("dec"));
numSysPanel.add(new JRadioButton("hex"));
panel.add(numSysPanel, gbc);
JPanel buttons = new JPanel(new GridLayout(4, 4, 2, 2));
for (int i = 0; i < 16; i++)
buttons.add(new JButton("" + i));
panel.add(buttons, gbc);
f.setContentPane(panel);
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}