So here is what I have so far, I will be adding a lot more in but here is my so called skeleton. What I am having trouble doing is deciding what layout I should use, if I should just make my own, and how will I go about doing that?
Some information about my creation will be useful: I am going to have multiple tags and input spaces to the right of them on the left-most area of the GUI, on the bottom right there will be a button, and on the top right there will be a textbox that will constantly stream information of what the bot is doing.
Please help me to find a layout I can use and tell me how to easily implement it (FYI I am using Ubuntu).
import java.awt.*;
import javax.swing.*;
public class dogedice {
public static void createWindow() {
JFrame frame = new JFrame("Framework Michael+Dalin");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel textLabel = new JLabel("Welcome to the framework of our future bots!");
JLabel textLabel2 = new JLabel("Username");
JTextField textField = new JTextField("Username");
JPanel panel = new JPanel();// Any new parts must be added here!
panel.add(textLabel);
panel.add(textField);
panel.add(textLabel2);
textLabel.setPreferredSize(new Dimension(800, 750));
frame.add(panel);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
createWindow();
}
}
First take a look at the Visual Guide to Layout Managers . Then, you'll notice that with a little creativity, you can achieve just about any desired layout either with just one layout manager or a combination of several for all your components.
Based on your description, you could use a BorderLayout
with a JPanel
in each of the quadrants.
FlowLayout
could be used with a right alignment. GridBagLayout
could be used to create two columns, one for the tag (which I assumed was a JLabel
) and one for an input space ( JTextField
). JTextArea
encapsulated in a JScrollPanel
) Here's an example:
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
public class LayoutExample1 extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField textField;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LayoutExample1 frame = new LayoutExample1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public LayoutExample1() {
setTitle("Title of GUI");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.WEST);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[]{0, 0};
gbl_panel.rowHeights = new int[]{0, 0};
gbl_panel.columnWeights = new double[]{0.0, 1.0};
gbl_panel.rowWeights = new double[]{0.0, Double.MIN_VALUE};
panel.setLayout(gbl_panel);
JLabel lblTag = new JLabel("Tag 1");
GridBagConstraints gbc_lblTag = new GridBagConstraints();
gbc_lblTag.insets = new Insets(0, 0, 0, 5);
gbc_lblTag.anchor = GridBagConstraints.EAST;
gbc_lblTag.gridx = 0;
gbc_lblTag.gridy = 0;
panel.add(lblTag, gbc_lblTag);
textField = new JTextField();
GridBagConstraints gbc_textField = new GridBagConstraints();
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridx = 1;
gbc_textField.gridy = 0;
panel.add(textField, gbc_textField);
textField.setColumns(10);
JPanel panel_1 = new JPanel();
contentPane.add(panel_1, BorderLayout.SOUTH);
panel_1.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
JButton btnConfirm = new JButton("Confirm");
panel_1.add(btnConfirm);
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
JTextArea textArea = new JTextArea("I am a bot and I will tell you what I am doing to your systems!");
textArea.setColumns(20);
scrollPane.setViewportView(textArea);
pack();
}
}
If the UI is that simple, put all the tags and inputs in a JPanel with GridLayout and put it in the center of a mainPanel with BorderLayout.
If you are interested in a more complex layout and want to learn how to use one, I recommend http://miglayout.com/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.