Java Gui Application Example
Review:silahkansaja
//AWT based application
import java.awt.*;
import java.awt.event.*;
class MainWindow extends Frame{
Label title;
CelciusPanel cp;
DebitCalculatorPanel dcp;
public MainWindow(){
setLayout(null);
title = new Label("Temperature converter");
add(title);
title.setLocation(10,60);
title.setSize(200,30);
cp = new CelciusPanel();
dcp = new DebitCalculatorPanel();
add(cp);
add(dcp);
dcp.setVisible(false);
//menu
MenuBar mb = new MenuBar();
setMenuBar(mb);
Menu menu, calculator;
mb.add(menu = new Menu("menu"));
MenuItem celcius, debit,exit;
menu.add(calculator = new Menu("calculator"));
calculator.add(celcius = new MenuItem("Convert Calcius to Fahrenheit"));
calculator.add(debit = new MenuItem("Debit Calculator"));
menu.add(exit = new MenuItem("exit"));
celcius.addActionListener(new MenuActionListener(this,1));
debit.addActionListener(new MenuActionListener(this,2));
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
/*((Frame)
((MenuBar)
((Menu)
((MenuItem)
ae.getSource()).getParent()).getParent()).getParent()).setVisible(false);
*/
setVisible(false);
System.exit(0);
}
});
addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent we){
setVisible(false);
System.exit(0);
}
});
setVisible(true);
}
}
class MenuActionListener implements ActionListener{
MainWindow mw;
int mn;
public MenuActionListener(MainWindow mw, int mn){
this.mw = mw;
this.mn = mn;
}
public void actionPerformed(ActionEvent ae){
if(!mw.cp.isVisible() && mn==1){
mw.title.setText("Temperature Converter");
mw.dcp.setVisible(false);
mw.cp.setVisible(true);
mw.repaint();
}
if(!mw.dcp.isVisible() && mn==2){
mw.title.setText("Debit Calculator");
mw.cp.setVisible(false);
mw.dcp.setVisible(true);
mw.repaint();
}
}
}
class DebitCalculatorPanel extends Panel{
TextField surfaceArea;
TextField velocity;
Label result;
Button calculate;
public DebitCalculatorPanel(){
setLayout(new GridLayout(4,3,30,4));
setLocation(40,100);
setSize(300,100);
surfaceArea = new TextField(5);
velocity = new TextField(5);
result = new Label("result");
calculate = new Button("Calculate");
add(new Label("Surface Area(m^2) :"));
add(surfaceArea);
add(new Label("Velocity(m/s) :"));
add(velocity);
add(new Label("result :"));
add(result);
add(calculate);
calculate.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ai){
Double s = Double.parseDouble(surfaceArea.getText());
Double v = Double.parseDouble(velocity.getText());
Double r = s*v;
result.setText(r+" m^3");
}
});
}
}
class CelciusPanel extends Panel{
TextField inCelcius;
Label outFahrenheit;
Button calculate;
public CelciusPanel(){
setLayout(new GridLayout(3,2,30,8));
setLocation(40,100);
setSize(300,100);
inCelcius = new TextField(5);
outFahrenheit = new Label("output");
calculate = new Button("Calculate");
add(new Label("input Celcius degree: "));
add(inCelcius);
add(new Label("Fahrenheit degree: "));
add(outFahrenheit);
add(calculate);
calculate.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
Double d = Double.parseDouble(inCelcius.getText());
d = d*1.8 + 32;
outFahrenheit.setText(d+" F");
}
});
}
}
public class GUIApp{
public static void main(String args[]){
MainWindow mw = new MainWindow();
mw.setSize(new Dimension(500, 300));
mw.setLocation(20,20);
mw.setTitle("An AWT-Based Application");
mw.setVisible(true);
}
}
Java Gui Application Example Originally published in Shvoong: http://www.shvoong.com/internet-and-technologies/software/2062114-java-gui-application-example/
Review:silahkansaja
//AWT based application
import java.awt.*;
import java.awt.event.*;
class MainWindow extends Frame{
Label title;
CelciusPanel cp;
DebitCalculatorPanel dcp;
public MainWindow(){
setLayout(null);
title = new Label("Temperature converter");
add(title);
title.setLocation(10,60);
title.setSize(200,30);
cp = new CelciusPanel();
dcp = new DebitCalculatorPanel();
add(cp);
add(dcp);
dcp.setVisible(false);
//menu
MenuBar mb = new MenuBar();
setMenuBar(mb);
Menu menu, calculator;
mb.add(menu = new Menu("menu"));
MenuItem celcius, debit,exit;
menu.add(calculator = new Menu("calculator"));
calculator.add(celcius = new MenuItem("Convert Calcius to Fahrenheit"));
calculator.add(debit = new MenuItem("Debit Calculator"));
menu.add(exit = new MenuItem("exit"));
celcius.addActionListener(new MenuActionListener(this,1));
debit.addActionListener(new MenuActionListener(this,2));
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
/*((Frame)
((MenuBar)
((Menu)
((MenuItem)
ae.getSource()).getParent()).getParent()).getParent()).setVisible(false);
*/
setVisible(false);
System.exit(0);
}
});
addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent we){
setVisible(false);
System.exit(0);
}
});
setVisible(true);
}
}
class MenuActionListener implements ActionListener{
MainWindow mw;
int mn;
public MenuActionListener(MainWindow mw, int mn){
this.mw = mw;
this.mn = mn;
}
public void actionPerformed(ActionEvent ae){
if(!mw.cp.isVisible() && mn==1){
mw.title.setText("Temperature Converter");
mw.dcp.setVisible(false);
mw.cp.setVisible(true);
mw.repaint();
}
if(!mw.dcp.isVisible() && mn==2){
mw.title.setText("Debit Calculator");
mw.cp.setVisible(false);
mw.dcp.setVisible(true);
mw.repaint();
}
}
}
class DebitCalculatorPanel extends Panel{
TextField surfaceArea;
TextField velocity;
Label result;
Button calculate;
public DebitCalculatorPanel(){
setLayout(new GridLayout(4,3,30,4));
setLocation(40,100);
setSize(300,100);
surfaceArea = new TextField(5);
velocity = new TextField(5);
result = new Label("result");
calculate = new Button("Calculate");
add(new Label("Surface Area(m^2) :"));
add(surfaceArea);
add(new Label("Velocity(m/s) :"));
add(velocity);
add(new Label("result :"));
add(result);
add(calculate);
calculate.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ai){
Double s = Double.parseDouble(surfaceArea.getText());
Double v = Double.parseDouble(velocity.getText());
Double r = s*v;
result.setText(r+" m^3");
}
});
}
}
class CelciusPanel extends Panel{
TextField inCelcius;
Label outFahrenheit;
Button calculate;
public CelciusPanel(){
setLayout(new GridLayout(3,2,30,8));
setLocation(40,100);
setSize(300,100);
inCelcius = new TextField(5);
outFahrenheit = new Label("output");
calculate = new Button("Calculate");
add(new Label("input Celcius degree: "));
add(inCelcius);
add(new Label("Fahrenheit degree: "));
add(outFahrenheit);
add(calculate);
calculate.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
Double d = Double.parseDouble(inCelcius.getText());
d = d*1.8 + 32;
outFahrenheit.setText(d+" F");
}
});
}
}
public class GUIApp{
public static void main(String args[]){
MainWindow mw = new MainWindow();
mw.setSize(new Dimension(500, 300));
mw.setLocation(20,20);
mw.setTitle("An AWT-Based Application");
mw.setVisible(true);
}
}
Java Gui Application Example Originally published in Shvoong: http://www.shvoong.com/internet-and-technologies/software/2062114-java-gui-application-example/
Tidak ada komentar:
Posting Komentar