Created
January 7, 2016 15:06
-
-
Save AnnaBoro/7728ca9e6bcd302b8e63 to your computer and use it in GitHub Desktop.
StoreUI + GridBagLayout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lesson5_8.shop; | |
public class Launcher { | |
public static void main(String[] args) { | |
Store store = new Store(); | |
new StoreUI(store); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lesson5_8.shop; | |
import javax.swing.*; | |
import java.awt.*; | |
import java.text.NumberFormat; | |
public class StoreUI { | |
private Store store; | |
public StoreUI(Store store) { | |
this.store = store; | |
JFrame frame = new JFrame("STORE"); | |
frame.setMinimumSize(new Dimension(800, 600)); | |
frame.setLocation(300, 200); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.getContentPane().add(createSellingPanel()); | |
frame.pack(); | |
frame.setVisible(true); | |
} | |
private JPanel createSellingPanel() { | |
JPanel panel = new JPanel(); | |
panel.setBackground(Color.CYAN); | |
GridBagLayout gbl = new GridBagLayout(); | |
panel.setLayout(gbl); | |
GridBagConstraints c = new GridBagConstraints(); | |
c.anchor = GridBagConstraints.NORTHWEST; | |
c.fill = GridBagConstraints.NONE; | |
c.gridheight = 1; | |
c.gridwidth = 1; | |
c.gridx = GridBagConstraints.RELATIVE; | |
c.gridy = GridBagConstraints.RELATIVE; | |
c.insets = new Insets(10, 0, 0, 0); | |
c.ipadx = 0; | |
c.ipady = 0; | |
c.weightx = 0.0; | |
c.weighty = 0.0; | |
JLabel jName = new JLabel("Enter name: "); | |
JTextField jTField = new JTextField(30); | |
gbl.setConstraints(jName, c); | |
c.gridwidth = GridBagConstraints.REMAINDER; | |
gbl.setConstraints(jTField, c); | |
panel.add(jName); | |
panel.add(jTField); | |
JLabel jBirds = new JLabel("Birds"); | |
JPanel jPBirds = new JPanel(); | |
jPBirds.setBorder(BorderFactory.createLineBorder(Color.BLUE)); | |
GridLayout birdsLayout = new GridLayout(3, 0); | |
jPBirds.setLayout(birdsLayout); | |
JRadioButton bird1Button = new JRadioButton("bird1"); | |
JRadioButton bird2Button = new JRadioButton("bird2"); | |
JRadioButton bird3Button = new JRadioButton("bird3"); | |
ButtonGroup group = new ButtonGroup(); | |
group.add(bird1Button); | |
group.add(bird2Button); | |
group.add(bird3Button); | |
jPBirds.add(bird1Button); | |
jPBirds.add(bird2Button); | |
jPBirds.add(bird3Button); | |
c.gridwidth = 1; | |
gbl.setConstraints(jBirds, c); | |
c.gridwidth = GridBagConstraints.REMAINDER; | |
gbl.setConstraints(jPBirds, c); | |
panel.add(jBirds); | |
panel.add(jPBirds); | |
JLabel jNumber = new JLabel("Enter quantity: "); | |
NumberFormat nf = NumberFormat.getInstance(); | |
JFormattedTextField jTField2 = new JFormattedTextField(nf); | |
jTField2.setValue(1); | |
c.gridwidth = 1; | |
gbl.setConstraints(jNumber, c); | |
c.gridwidth = GridBagConstraints.REMAINDER; | |
gbl.setConstraints(jTField2, c); | |
panel.add(jNumber); | |
panel.add(jTField2); | |
JButton buyButton = new JButton("BUY"); | |
c.ipadx = 100; | |
c.anchor = GridBagConstraints.CENTER; | |
gbl.setConstraints(buyButton, c); | |
panel.add(buyButton); | |
return panel; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment