//MyFirstUI.java
package ui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyFirstUI extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
private JCheckBox checkbox;
private JTextField firstName;
private JTextField lastName;
public MyFirstUI() {
// Lets make it look nice
// This you can ignore / delete if you don't like it
// try {
// for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
// if ("Nimbus".equals(info.getName())) {
// UIManager.setLookAndFeel(info.getClassName());
// break;
// }
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
setTitle("My First UI");
// We create a panel which will hold the UI components
JPanel pane = new JPanel(new BorderLayout());
// We always have two UI elements (columns) and we have three rows
int numberOfRows = 3;
int numberOfColumns = 2;
pane.setLayout(new GridLayout(numberOfRows, numberOfColumns));
// create and attach buttons
// create a label and add it to the main window
JLabel firstNamelabel = new JLabel(" Firstname: ");
pane.add(firstNamelabel);
firstName = new JTextField();
pane.add(firstName);
JLabel lastNamelabel = new JLabel(" Lastname: ");
pane.add(lastNamelabel);
lastName = new JTextField();
pane.add(lastName);
JButton sayHello = new JButton("Say something");
pane.add(sayHello);
checkbox = new JCheckBox("Nice");
pane.add(checkbox);
// Add the pane to the main window
getContentPane().add(pane);
// Pack will make the size of window fitting to the compoents
// You could also use for example setSize(300, 400);
pack();
// Set a tooltip for the button
sayHello.setToolTipText("This button will say something really nice of something bad");
// sayHello need to do something
sayHello.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (!checkbox.isSelected()) {
JOptionPane.showMessageDialog(null, "I don't like you, "
+ firstName.getText() + " " + lastName.getText() + "!");
} else {
JOptionPane.showMessageDialog(null, "How are you, "
+ firstName.getText() + " " + lastName.getText() + "?");
}
}
}
//MainTester.java
package test;
import ui.MyFirstUI;
public class MainTester {
public static void main(String[] args) {
MyFirstUI view = new MyFirstUI();
view.setVisible(true);
}
}
No comments:
Post a Comment