import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
import UDGUI.MenuC;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class SwingT extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private JLabel jLabel;
private JTextField jTextField1, jTextField2;
private JButton jButton;
private JCheckBox jBox;
private JRadioButton jRadioButton1, jRadioButton2;
private ButtonGroup bGroup;
private String rt;
private JSlider jSlider;
public SwingT() {
this.setSize(500, 200);
// this.getContentPane().setLayout(null);
this.setLayout(null);
this.add(getJLabel(), null);
this.add(getJTextField(), null);
this.add(getJButton(), null);
this.add(getJCheckbox1(), null);
this.add(getjTextField2(), null);
getButtonGroup();
this.add(getJRadioButton1(), null);
this.add(getJRadioButton2(), null);
this.add(getJSlider(), null);
this.setTitle("HelloWorld");
}
private JLabel getJLabel() {
if (jLabel == null) {
jLabel = new JLabel();
jLabel.setBounds(114, 60, 53, 18);
jLabel.setText("Name:");
}
return jLabel;
}
private JTextField getJTextField() {
if (jTextField1 == null) {
jTextField1 = new JTextField();
jTextField1.setBounds(176, 60, 160, 20);
JSlider jSlider = new JSlider(JSlider.HORIZONTAL, 0, 100, 10);
jTextField1.add(jSlider);
}
return jTextField1;
}
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(200, 150, 71, 27);
jButton.setText("OK");
jButton.setActionCommand("Click");
jButton.addActionListener(this);
}
return jButton;
}
private JCheckBox getJCheckbox1() {
if (jBox == null) {
jBox = new JCheckBox();
jBox.setText("JCheckbox");
jBox.setBounds(100, 90, 100, 20);
}
return jBox;
}
private JRadioButton getJRadioButton1() {
if (jRadioButton1 == null) {
jRadioButton1 = new JRadioButton();
jRadioButton1.setText("jRadioButton1");
jRadioButton1.setBounds(200, 90, 110, 20);
}
return jRadioButton1;
}
private JRadioButton getJRadioButton2() {
if (jRadioButton2 == null) {
jRadioButton2 = new JRadioButton();
jRadioButton2.setText("jRadioButton2");
jRadioButton2.setBounds(320, 90, 110, 20);
}
return jRadioButton2;
}
private JSlider getJSlider() {
if (jSlider == null) {
jSlider = new JSlider();
jSlider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
jTextField2.setText(jSlider.getValue() + "");
jTextField2.setBackground(new Color(Integer
.parseInt(jTextField2.getText()), 255, 255));
}
});
jSlider.setMinimum(0);
jSlider.setMaximum(255);
jSlider.setValue(125);
jSlider.setBounds(100, 120, 110, 20);
}
return jSlider;
}
private JTextField getjTextField2() {
if (jTextField2 == null) {
jTextField2 = new JTextField();
jTextField2.setBounds(220, 120, 50, 20);
}
return jTextField2;
}
private ButtonGroup getButtonGroup() {
bGroup = new ButtonGroup();
bGroup.add(getJRadioButton1());
bGroup.add(getJRadioButton2());
return bGroup;
}
void justify() {
Enumeration<AbstractButton> en = bGroup.getElements();
while (en.hasMoreElements()) {
AbstractButton ab = en.nextElement();
if (ab.isSelected()) {
rt = ab.getText();
}
}
}
public static void main(String[] args) {
SwingT w = new SwingT();
new MenuC(w);
}
public void actionPerformed(ActionEvent e) {
justify();
// this.setVisible(false);
this.dispose();
String text = jTextField1.getText();
String command = e.getActionCommand();
if (command.equals("Click")) {
if (jBox.isSelected()) {
new Alert(text + "==" + jBox.getText() + "==" + "==" + rt
+ "==" + jSlider.getValue()).ShowAlert();
} else {
new Alert(text + "==" + jSlider.getValue()).ShowAlert();
}
}
}
}
====================================================================================================
package UDGUI;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class MenuC {
JFrame jFrame = new JFrame();
public MenuC(JFrame jFrame) {
this.jFrame = jFrame;
createMenu();
}
private void createMenu() {
JMenuBar jBar = new JMenuBar();
JMenu jMenu0 = new JMenu("File");
JMenu jMenu = new JMenu("New");
JMenuItem jMenuItem = new JMenuItem();
jMenuItem.setText("New File");
jMenu.add(jMenuItem);
jFrame.setLayout(null);
jBar.setBounds(0, 0, 500, 20);
jMenu0.add(jMenu);
jBar.add(jMenu0);
// jBar.add(jMenu);
jFrame.add(jBar);
jFrame.pack();
jFrame.setSize(500, 300);
jFrame.setResizable(true);
jFrame.setLocation(400, 300);
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
====================================================================================================
package UDGUI;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Alert implements ActionListener {
private String name;
private JButton jButton;
private JLabel jLabel;
JFrame jFrame;
Alert(String text) {
this.name = text;
}
void ShowAlert() {
jLabel = new JLabel("Welcome, " + name);
jLabel.setBounds(30, 10, 400, 20);
jButton = new JButton("OK");
jButton.setActionCommand("OK");
jButton.addActionListener(this);
jButton.setBounds(120, 40, 60, 20);
System.out.println(name);
jFrame = new JFrame("AlertJFrame");
jFrame.setSize(300, 120);
jFrame.setLocation(400, 300);
jFrame.setAlwaysOnTop(true);
jFrame.setLayout(null);
jFrame.add(jLabel);
jFrame.add(jButton);
jFrame.setResizable(false);
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
String c = e.getActionCommand();
if (c.equals("OK")) {
System.out.println("Close...");
jFrame.dispose();
}
}
}