progJ.pdf progJ-s.pdf progJ-e.pdf 記事一覧 |
![]() 下記プログラムの実行結果 |
progJ.pdf の[#68%1] の変更例を progJ-e.pdf に追加しました.
//変更後の App61.java
package myapp;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*; //***
class Fig61 extends JPanel{
Fig61( ){super( );
setPreferredSize(new Dimension(300, 200));
setBackground(Color.WHITE);
setBorder(new LineBorder(Color.BLUE, 2)); //***
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawLine(0,0,200,200);
g.setFont(new Font("MS ゴシック", 1, 24)); //***
g.setColor(Color.RED); //***
g.drawString("Hello", 50, 150);
g.setColor(Color.YELLOW); //***
g.fillOval(150, 50, 100, 50); //***
g.setColor(Color.BLACK); //***
g.drawOval(150, 50, 100, 50);
g.clearRect(200, 0, 20, 200); //***
}
}
public class App61 extends App52 {
JPanel p1;
App61( ){
super( ); setSize(350, 300);
p1 = new JPanel( );
Container cp = getContentPane( );
try{Thread.sleep(3000);}catch(Exception e){ } //***
tf1.setText("Hello"); //***
p1.add(new Fig61( )); cp.add(p1);
}
public static void main(String[ ] args) {
EventQueue.invokeLater(new Runnable( ) { //***
public void run( ) {new App61( );} //***
}); //***
}
}
//App52.java
package myapp;
import java.awt.*; import javax.swing.*;
import java.awt.event.*;
class App52 extends App50 implements ActionListener {
App52( ){super( );
b1.setText("hello");
/*------------------------------------
b1.setText("Dec2Bin");
tf1.setText("123");
------------------------------------*/
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
String s = tf1.getText( );
tf1.setText("Hello, " + s);
/*------------------------------------
int m, n = 0;
try{n = Integer.parseInt(s); s = "";}
catch(Exception ex){n = -1;}
if(n < 0){tf1.setText("?"); return;}
for(m = 1; m while(m > 1){
m /= 2;
if(n >= m){s = s + "1"; n = n - m;}
else{s = s + "0";}
}
tf1.setText(s);
------------------------------------*/
}
public static void main(String[ ] args) {
new App52( );
}
}
//App50.java
package myapp;
import java.awt.*; import javax.swing.*;
class App50 extends JFrame implements Runnable {
JTextField tf1; JButton b1;
App50( ){super("myApp");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(250, 105);
setLayout(new FlowLayout( ));
tf1 = new JTextField("tf1", 20);
b1 = new JButton("b1");
Container f0 = getContentPane( );
f0.add(tf1); f0.add(b1);
setVisible(true);
}
public void run( ){ }
public static void main(String[ ] args) {
App50 app = new App50( );
}
}