import java.awt.BorderLayout; public class GUI implements ActionListener{ private int count = 0; private JLabel label; private JFrame frame; private JButton button; private JPanel panel; public GUI() { frame= new JFrame(); button = new JButton("Click Me if you see a zombie"); button.addActionListener(this); label = new JLabel("Number of zombies:"); panel = new JPanel(); panel.setBorder(BorderFactory.createEmptyBorder(30,30,10,30)); panel.setLayout(new GridLayout(0,1)); panel.add(button); panel.add(label); frame.add(panel,BorderLayout.CENTER); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("Zombie Spotter"); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { new GUI(); } @Override public void actionPerformed(ActionEvent e) { count++; label.setText("Number of zombies: " + count); } } |
ctG import javax.swing.JFrame; public class ZombieTacticGUIMain { public static void main(String[] args) { ZombieRosterGUI zombieRoster = new ZombieTacticGUIMain(); JFrame myFrame = zombieRoster.getFrame(); myFrame.setVisible(true); }br }br |
//=======================
//https://www.guru99.com/java-swing-gui.html
//======================== public class StRoseGUI { private JFrame frame = new JFrame("ST ROSE GUI"); private JMenuBar mbar = new JMenuBar(); private JMenu m1 = new JMenu("File"); private JMenu m2 = new JMenu("About"); private JMenuItem m11 = new JMenuItem("Import Input File"); private JMenuItem m12 = new JMenuItem("Display Roster"); private JMenuItem m13 = new JMenuItem("Export Roster"); private JMenuItem m14 = new JMenuItem("Exit"); private JPanel panel = new JPanel(); // the panel is not visible in output private JButton display = new JButton("Display Roster"); private JButton export = new JButton("Export Roster"); private JButton exit = new JButton("Exit"); private JTextArea textArea; private String fileName = null; private ArrayList Zombies zombietList = null; private String displayString = null; private double totalZombiesOnEarth = 0.0; public StRosteGUI () { //Creating the Frame frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(600, 600); //Creating the MenuBar and adding components mbar.add(m1); mbar.add(m2); m1.add(m11); m1.add(m12); m1.add(m13); m1.add(m14); //Creating the panel at bottom and adding components // Components Added using Flow Layout panel.add(display); panel.add(export); panel.add(exit); //Create a text area. textArea = new JTextArea(); textArea.setFont(new Font("Serif", Font.ITALIC, 16)); textArea.setLineWrap(true); textArea.setWrapStyleWord(true); JScrollPane areaScrollPane = new JScrollPane(textArea); areaScrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); areaScrollPane.setPreferredSize(new Dimension(250, 250)); areaScrollPane.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("Enter your text here"), BorderFactory.createEmptyBorder(5,5,5,5)), areaScrollPane.getBorder())); //Adding Components to the frame. frame.getContentPane().add(BorderLayout.SOUTH, panel); frame.getContentPane().add(BorderLayout.NORTH, mbar); frame.getContentPane().add(BorderLayout.CENTER, areaScrollPane); TextFieldHandler handler = new TextFieldHandler(); display.addActionListener(handler); export.addActionListener(handler); exit.addActionListener(handler); m11.addActionListener(handler); m12.addActionListener(handler); m13.addActionListener(handler); m14.addActionListener(handler); } public JFrame getFrame() { return frame; } |