slasso 27 Share Posted September 19, 2017 I thought I would just share this because a lot of people use the GUI builders in the IDE. But doing something like this keeps the code clean and reusable. package gui; import javax.swing.*; import java.awt.*; import java.awt.event.ActionListener; /** * Created by slasso on 9/12/17. */ public class Components { private static final Font FONT = new Font("Tahoma", Font.PLAIN, 11); private JFrame frame; public Components(JFrame frame) { this.frame = frame; } public JSlider makeJSlider(int x, int y, int width, int height, int max, boolean isVisible) { JSlider slider = new JSlider(); slider.setPaintLabels(true); slider.setSnapToTicks(true); slider.setFont(FONT); slider.setValue(0); slider.setMaximum(max); slider.setVisible(isVisible); slider.setMajorTickSpacing(1); slider.setOpaque(false); slider.setBounds(x, y, width, height); frame.add(slider); return slider; } public JComboBox<String> makeJComboBox(int x, int y, int width, int height, String[] modelItems, ActionListener listener, boolean isVisible) { JComboBox<String> comboBox = new JComboBox<>(modelItems); comboBox.setFont(FONT); comboBox.setBounds(x, y, width, height); comboBox.setVisible(isVisible); if (listener != null) comboBox.addActionListener(listener); frame.add(comboBox); return comboBox; } public JLabel makeJLabel(int x, int y, int width, int height, String text, boolean isVisible) { JLabel label = new JLabel(text); label.setFont(FONT); label.setBounds(x, y, width, height); label.setVisible(isVisible); frame.add(label); return label; } public JButton makeJButton(int x, int y, int width, int height, String text, ActionListener listener, boolean isVisible) { JButton button = new JButton(); button.setFont(FONT); button.setText(text); button.setBounds(x, y, width, height); button.setVisible(isVisible); if (listener != null) button.addActionListener(listener); frame.add(button); return button; } public JCheckBox makeJCheckBox(int x, int y, int width, int height, String text, ActionListener listener, boolean isSelected, boolean isVisible) { JCheckBox checkBox = new JCheckBox(text); checkBox.setFont(FONT); checkBox.setBounds(x, y, width, height); checkBox.setSelected(isSelected); checkBox.setVisible(isVisible); if (listener != null) checkBox.addActionListener(listener); frame.add(checkBox); return checkBox; } } Every component has the same arguments basically so it's easy to use. To use this, create a Components object in your GUI class that extends JFrame. public class MyGUI extends JFrame { private Components components; private JButton startButton; public MyGUI() { this.components = new Components(this); initComponents(); } private void initComponents() { // make your components in here like this startButton = components.makeJButton(111, 216, 175, 55, "Start", e -> startButton(e), true); } private void startButton(ActionEvent e) { MainScript.init(); this.setVisible(false); } } The reason I return the component back to the JFrame is incase you need to manipulate the component afterwards. Such as disabling/hiding them, grabbing the selected item/text from them, etc. The start button isn't the best example of it and doesn't need a field for it but it's just there for reference. Link to comment Share on other sites More sharing options...
Donald Trump 333 Share Posted September 19, 2017 Thank you! Link to comment Share on other sites More sharing options...
Dorkinator 64 Share Posted September 19, 2017 You should probably use layout managers instead of trying to position each element with pixel locations. Link to comment Share on other sites More sharing options...
slasso 27 Author Share Posted September 19, 2017 You don't really need panels and layouts for simple guis in these scripts that dont have much components. Also swing sucks so why bother Link to comment Share on other sites More sharing options...
Dinh 496 Share Posted September 19, 2017 You should probably use layout managers instead of trying to position each element with pixel locations. You don't really need panels and layouts for simple guis in these scripts that dont have much components. Also swing sucks so why bother I totally agree with Dorkinator, layout managers are really simple to use. Especially Flow and BorderLayout should not be hard to get into Link to comment Share on other sites More sharing options...
Dorkinator 64 Share Posted September 20, 2017 You don't really need panels and layouts for simple guis in these scripts that dont have much components. Also swing sucks so why bother Even for simple UI's a layout manager will save you time. Especially if you want to add or change the UI. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.