// Fig. 12.27: PanelDemo.java

hundredcarriageΛογισμικό & κατασκευή λογ/κού

3 Νοε 2013 (πριν από 3 χρόνια και 7 μήνες)

117 εμφανίσεις

// Fig. 12.27: PanelDemo.java

// Using a JPanel to help lay out components.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;


public class PanelDemo extends JFrame {


private JPanel buttonPanel;


private JButton buttons[];



public
PanelDemo()


{


super( "Panel Demo" );



Container c = getContentPane();


buttonPanel = new JPanel();


buttons = new JButton[ 5 ];



buttonPanel.setLayout(


new GridLayout( 1, buttons.length ) );



for ( int i = 0; i

< buttons.length; i++ ) {


buttons[ i ] = new JButton( "Button " + (i + 1) );


buttonPanel.add( buttons[ i ] );


}



c.add( buttonPanel, BorderLayout.SOUTH );



setSize( 425, 150 );


show();


}



public

static void main( String args[] )


{


PanelDemo app = new PanelDemo();



app.addWindowListener(


new WindowAdapter() {


public void windowClosing( WindowEvent e )


{


System.exit( 0 );


}


}


);


}

}


/**************************************************************************


* (C) Copyright 1999 by Deitel & Associates, Inc. and Prentice Hall. *


* All Rights Reserved. *


* *


* DISCLAIMER: The authors and publisher of this book have used their *


* best efforts in preparing the book. These efforts include the *


* development, research, an
d testing of the theories and programs *


* to determine their effectiveness. The authors and publisher make *


* no warranty of any kind, expressed or implied, with regard to these *


* programs or to the documentation contained in these b
ooks. The authors *


* and publisher shall not be liable in any event for incidental or *


* consequential damages in connection with, or arising out of, the *


* furnishing, performance, or use of these programs. *


*************************************************************************/





















//TempConverter.java

/**


* Program ini mengubah temperatur dari Celsius ke Fahrenheit.


* @author


* @version


*/


import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.io.*;

import java.lang.*;


// Contoh ini menampilkan penggunaan JButton, JTextField

// dan JLabel.

public class TempConverter implements ActionListener

{


JFrame converterFrame;


JPanel converterPanel;


JTextField

tempCelsius;


JLabel celsiusLabel, fahrenheitLabel;


JButton convertTemp;



// Constructor


public TempConverter()


{


// Membuat frame dan container


converterFrame = new JFrame("Convert Temperature");



converterPanel = new JPanel();


converterPanel.setLayout(new GridLayout(2,2));



// Menambahkan widgets.


addWidgets();



// Menambahkan panel ke frame.


converterFrame.getContentPane().add(
converterPanel, BorderLayout.CENTER);



// Keluar jika window ditutup.


converterFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



// Tampilkan converter.


converterFrame.pack();


converterFrame.
setVisible(true);


}



// Membuat dan menambahkan widgets ke converter


private void addWidgets()


{


// Membuat widgets.


tempCelsius = new JTextField(2);


celsiusLabel = new JLabel("Celsius", SwingConstant
s.LEFT);


convertTemp = new JButton("Convert");


fahrenheitLabel = new JLabel("Fahrenheit", SwingConstants.LEFT);



// Mendengarkan events dari tombol/button Convert.


convertTemp.addActionListener(this);




// Menambahkan widgets ke container.


converterPanel.add(tempCelsius);


converterPanel.add(celsiusLabel);


converterPanel.add(convertTemp);


converterPanel.add(fahrenheitLabel);



celsiusLabel.se
tBorder(BorderFactory.createEmptyBorder(5,5,5,5));


fahrenheitLabel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));



}



// Implementasi interface ActionListener.


public void actionPerformed(ActionEvent event)


{



// Mengubah derajad Celcius ke double kemudian mengubahnya


// ke Fahrenheit.


int tempFahr = (int)((Double.parseDouble(tempCelsius.getText()))


* 1.8 + 32);



fahrenheitLabel
.setText(tempFahr + " Fahrenheit");



}



// main method


public static void main(String[] args)


{


// Setting tampilan


try


{


UIManager.setLookAndFeel(


UIManager.getCro
ssPlatformLookAndFeelClassName());


}


catch(ClassNotFoundException e){ }


catch(InstantiationException e){ }


catch(IllegalAccessException e){ }


catch(UnsupportedLookAndFeelException e){ }



TempConverter converter = new TempConverter();


}

}