public Btns(int x, int y) { this.setSize(x, y); this.setLayout(new FlowLayout()); this.add(pause); this.add(start); pause.addActionListener(this); start.addActionListener(this); pause.addKeyListener(this); start.addKeyListener(this); this.setFocusable(true); }
public void addFields() { // gbc.fill = GridBagConstraints.HORIZONTAL; gbc.ipadx = 20; content.add(new JLabel("Host", JLabel.CENTER), gbc); gbc.gridy++; content.add(new JLabel("Database", JLabel.CENTER), gbc); gbc.gridy++; content.add(new JLabel("Table", JLabel.CENTER), gbc); gbc.gridy++; content.add(new JLabel("Username", JLabel.CENTER), gbc); gbc.gridy++; content.add(new JLabel("Password", JLabel.CENTER), gbc); gbc.gridy = 0; gbc.gridx = 1; content.add(host, gbc); gbc.gridy++; content.add(database, gbc); gbc.gridy++; content.add(table, gbc); gbc.gridy++; content.add(username, gbc); gbc.gridy++; content.add(password, gbc); gbc.gridy++; login.setMnemonic(KeyEvent.VK_L); ActionListener act = new ActionListener() { public void actionPerformed(ActionEvent e) { if (e.getSource() == login) { if (username.getText().length() > 0) alive = false; else { showMessage("Username required", "At least enter a username :)"); } } if (e.getSource() == exit) { System.exit(0); } } }; login.addActionListener(act); exit.addActionListener(act); JPanel p = new JPanel(new FlowLayout()); p.add(login); p.add(exit); gbc.gridx = 0; gbc.gridwidth = 2; content.add(p, gbc); username.addKeyListener(this); password.addKeyListener(this); login.addKeyListener(this); }
/** * Make the UI for this widget. * * @param floatToolBar true if the toolbar should be floatable * @return UI as a Component */ private JComponent doMakeContents(boolean floatToolBar) { String imgp = "/auxdata/ui/icons/"; KeyListener listener = new KeyAdapter() { public void keyPressed(KeyEvent e) { if ((e.getSource() instanceof JComboBox)) { return; } int code = e.getKeyCode(); char c = e.getKeyChar(); if ((code == KeyEvent.VK_RIGHT) || (code == KeyEvent.VK_KP_RIGHT)) { if (e.isShiftDown()) { gotoIndex(anime.getNumSteps() - 1); } else { actionPerformed(CMD_FORWARD); } } else if ((code == KeyEvent.VK_LEFT) || (code == KeyEvent.VK_KP_LEFT)) { if (e.isShiftDown()) { gotoIndex(0); } else { actionPerformed(CMD_BACKWARD); } } else if (code == KeyEvent.VK_ENTER) { actionPerformed(CMD_STARTSTOP); } else if ((code == KeyEvent.VK_P) && e.isControlDown()) { actionPerformed(CMD_PROPS); } else if (Character.isDigit(c)) { int step = new Integer("" + c).intValue() - 1; if (step < 0) { step = 0; } if (step >= anime.getNumSteps()) { step = anime.getNumSteps() - 1; } gotoIndex(step); } } }; List buttonList = new ArrayList(); buttonList.add(timesCbx); // Update the list of times setTimesInTimesBox(); Dimension preferredSize = timesCbx.getPreferredSize(); if (preferredSize != null) { int height = preferredSize.height; if (height < 50) { JComponent filler = GuiUtils.filler(3, height); buttonList.add(filler); } } String[][] buttonInfo = { {"Go to first frame", CMD_BEGINNING, getIcon("Rewind")}, {"One frame back", CMD_BACKWARD, getIcon("StepBack")}, {"Run/Stop", CMD_STARTSTOP, getIcon("Play")}, {"One frame forward", CMD_FORWARD, getIcon("StepForward")}, {"Go to last frame", CMD_END, getIcon("FastForward")}, {"Properties", CMD_PROPS, getIcon("Information")} }; for (int i = 0; i < buttonInfo.length; i++) { JButton btn = GuiUtils.getScaledImageButton(buttonInfo[i][2], getClass(), 2, 2); btn.setToolTipText(buttonInfo[i][0]); btn.setActionCommand(buttonInfo[i][1]); btn.addActionListener(this); btn.addKeyListener(listener); // JComponent wrapper = GuiUtils.center(btn); // wrapper.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED)); btn.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED)); buttonList.add(btn); // buttonList.add(wrapper); if (i == 2) { startStopBtn = btn; } } JComponent contents = GuiUtils.hflow(buttonList, 1, 0); if (boxPanel == null) { boxPanel = new AnimationBoxPanel(this); if (timesArray != null) { updateBoxPanel(timesArray); } } boxPanel.addKeyListener(listener); if (!getBoxPanelVisible()) { boxPanel.setVisible(false); } contents = GuiUtils.doLayout(new Component[] {boxPanel, contents}, 1, GuiUtils.WT_Y, GuiUtils.WT_N); // GuiUtils.addKeyListenerRecurse(listener,contents); if (floatToolBar) { JToolBar toolbar = new JToolBar(JToolBar.HORIZONTAL); toolbar.setFloatable(true); contents = GuiUtils.left(contents); toolbar.add(contents); contents = toolbar; } updateRunButton(); madeContents = true; return contents; }
/** * Called by constructors to initialize the dialog. * * @since ostermillerutils 1.00.00 */ @Override protected void dialogInit() { if (labels == null) { setLocale(Locale.getDefault()); } name = new JTextField("", 20); pass = new JPasswordField("", 20); okButton = new JButton(labels.getString("dialog.ok")); cancelButton = new JButton(labels.getString("dialog.cancel")); nameLabel = new JLabel(labels.getString("dialog.name") + " "); passLabel = new JLabel(labels.getString("dialog.pass") + " "); super.dialogInit(); KeyListener keyListener = (new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ESCAPE || (e.getSource() == cancelButton && e.getKeyCode() == KeyEvent.VK_ENTER)) { pressed_OK = false; PasswordDialog.this.setVisible(false); } if (e.getSource() == okButton && e.getKeyCode() == KeyEvent.VK_ENTER) { pressed_OK = true; PasswordDialog.this.setVisible(false); } } }); addKeyListener(keyListener); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == name) { // the user pressed enter in the name field. name.transferFocus(); } else { // other actions close the dialog. pressed_OK = (source == pass || source == okButton); PasswordDialog.this.setVisible(false); } } }; GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); c.insets.top = 5; c.insets.bottom = 5; JPanel pane = new JPanel(gridbag); pane.setBorder(BorderFactory.createEmptyBorder(10, 20, 5, 20)); c.anchor = GridBagConstraints.EAST; gridbag.setConstraints(nameLabel, c); pane.add(nameLabel); gridbag.setConstraints(name, c); name.addActionListener(actionListener); name.addKeyListener(keyListener); pane.add(name); c.gridy = 1; gridbag.setConstraints(passLabel, c); pane.add(passLabel); gridbag.setConstraints(pass, c); pass.addActionListener(actionListener); pass.addKeyListener(keyListener); pane.add(pass); c.gridy = 2; c.gridwidth = GridBagConstraints.REMAINDER; c.anchor = GridBagConstraints.CENTER; JPanel panel = new JPanel(); okButton.addActionListener(actionListener); okButton.addKeyListener(keyListener); panel.add(okButton); cancelButton.addActionListener(actionListener); cancelButton.addKeyListener(keyListener); panel.add(cancelButton); gridbag.setConstraints(panel, c); pane.add(panel); getContentPane().add(pane); pack(); }
@SuppressWarnings({"unchecked", "rawtypes"}) private void initializeComponent() { // Initialize panels panel_1 = new JPanel(); panel_2 = new JPanel(); panel_3 = new JPanel(); panel_4 = new JPanel(); panel_5 = new JPanel(); panel_6 = new JPanel(); panel_7 = new JPanel(); // Initialize spinner spinnermodel = new SpinnerNumberModel(0, 0, 10, 1); spinner = new JSpinner(spinnermodel); // Initialize combo box searchcombobox = new JComboBox(); searchcombobox.addItem("All"); searchcombobox.addItem("By Name"); searchcombobox.addItem("By Guardian Name"); searchcombobox.addItem("By Course"); searchcombobox.addItem("Due Fees"); searchcombobox.setSelectedIndex(0); coursecombobox = new JComboBox(); coursecombobox.addItem("Select Course"); Database db = new Database(); String courses[] = db.getCourses(); if (courses != null) { for (String x : courses) { coursecombobox.addItem(x); } } // Initialize text field searchtextfield = new JTextField(10); addfeestextfield = new JTextField(8); // Initialize button searchbutton = new JButton("Search"); addfeesbutton = new JButton("Add fees"); addcoursebutton = new JButton("Add Course"); // Initialize check box allfieldcheckbox = new JCheckBox("Show all fields"); allfieldcheckbox.setSelected(true); // Initialize label totalstudentlabel = new JLabel("Total Student"); feespayedlabel = new JLabel("Fees Payed"); feesduelabel = new JLabel("Fees due"); totalfeeslabel = new JLabel("Total fees"); // Add Item listener to check box allfieldcheckbox.addItemListener(this); // Add Action Listener to button searchbutton.addActionListener(this); addfeesbutton.addActionListener(this); addcoursebutton.addActionListener(this); // Add Key Listener to button searchbutton.addKeyListener(this); addfeesbutton.addKeyListener(this); addcoursebutton.addKeyListener(this); }
CarRace() { f = new JFrame("Car Race"); f.setSize(600, 700); f.setLayout(null); f.setBounds(250, 10, 600, 700); background = new JLabel(new ImageIcon("road.jpg")); background.setBounds(0, 0, 600, 700); f.add(background); car = new JLabel(new ImageIcon("red.png")); car.setBounds(120, 500, 120, 70); background.add(car); position = 1; one = new JLabel(new ImageIcon("pink.png")); one.setBounds(120, 100, 120, 70); background.add(one); two = new JLabel(new ImageIcon("yellow.png")); two.setBounds(240, 100, 120, 70); background.add(two); three = new JLabel(new ImageIcon("blue.png")); three.setBounds(360, 100, 120, 70); background.add(three); one.setVisible(false); two.setVisible(false); three.setVisible(false); start = new JButton("Start"); start.setBounds(240, 600, 100, 25); background.add(start); start.addKeyListener(this); start.addActionListener(this); score = new JLabel("score " + scoreValue); score.setBounds(430, 20, 130, 35); background.add(score); score.setFont(new Font("", Font.BOLD, 25)); score.setForeground(Color.WHITE); speed = new JLabel("speed"); speed.setBounds(40, 20, 160, 35); background.add(speed); speed.setFont(new Font("", Font.BOLD, 25)); speed.setForeground(Color.WHITE); result = new JLabel(); result.setBounds(180, 250, 400, 40); result.setVisible(false); result.setForeground(Color.WHITE); result.setFont(new Font(" ", Font.BOLD, 40)); background.add(result); x = 120; t = new Timer(x, this); f.setVisible(true); }